2021/02/05

Writing a Perl Core Feature - part 2: warnings.pm

Index | < Prev | Next >

Ever since Perl version 5.18, newly added features are initially declared as experimental. This gives time for them to be more widely tested and used in practice, so that the design can be further refined and changed if necessary. In order to achieve this for a new feature our next step will be to add a warning to warnings.pm.

Similar to the named feature in feature.pm this file also isn't edited directly, but instead is maintained by a regeneration script; this one called regen/warnings.pl.

For example, the isa feature added a new warning here: (github.com/Perl/perl5).

--- a/regen/warnings.pl
+++ b/regen/warnings.pl
@@ -16,7 +16,7 @@
 #
 # This script is normally invoked from regen.pl.
 
-$VERSION = '1.45';
+$VERSION = '1.46';
 
 BEGIN {
     require './regen/regen_lib.pl';
@@ -117,6 +117,8 @@ my $tree = {
                                     [ 5.029, DEFAULT_ON ],
                                 'experimental::vlb' =>
                                     [ 5.029, DEFAULT_ON ],
+                                'experimental::isa' =>
+                                    [ 5.031, DEFAULT_ON ],
                         }],
 
         'missing'       => [ 5.021, DEFAULT_OFF],

This change simply adds another entry into the list of defined warnings. It has a name, a Perl version from which it appears, and is declared to be on by default (as all "experimental" warnings should be). We also have to bump the version number because that is the value inserted into the generated warnings.pm file.

For adding a new warning to go along with our banana feature, we follow a similar process to what we did for the named feature bit. We edit the regeneration file to make a similar change to the one seen above, then run the script to have it generate the required files.

leo@shy:~/src/bleadperl/perl [git]
$ nvim regen/warnings.pl 

leo@shy:~/src/bleadperl/perl [git]
$ perl regen/warnings.pl 
Changed: warnings.h lib/warnings.pm

As before, we can see that it has generated the new lib/warnings.pm Perl pragma file, and also a header file for compiling the interpreter itself. Take a look at these files now to get a feel for what's there.

In particular, the items of note are:

  • The generated warnings.pm file includes changes to the documented list of known warning categories.
  • A new WARN_EXPERIMENTAL__BANANA macro has been created in the warnings.h file. We shall be seeing this used soon.

Now that we have both the named feature and the experimental warning we can check that the experimental pragma module can enable it:

leo@shy:~/src/bleadperl/perl [git]
$ make -j4 perl
...

leo@shy:~/src/bleadperl/perl [git]
$ ./perl -Ilib -ce 'use experimental "banana";'
-e syntax OK

We're now one step closer to being able to actually start implementing this feature.

Index | < Prev | Next >

No comments:

Post a Comment