kcott has asked for the wisdom of the Perl Monks concerning the following question:
I encountered a problem when using the if pragma and modules with an empty import list. I suspect this is a bug or, at the very least, a limitation which should be documented. Before I raise a bug report, any feedback on this issue would be welcome (can you reproduce it? have I missed something? anything else?). To demonstrate, here's some examples using warnings for the module.
Firstly, results with no list and an empty list are as expected:
$ perl -Mstrict -E 'use warnings; my $x; say $x+1' Use of uninitialized value $x in addition (+) at -e line 1. 1 $ perl -Mstrict -E 'use warnings (); my $x; say $x+1' 1
Adding the if pragma into the mix, and the empty list acts like no list:
$ perl -Mstrict -E 'use if 1==1, warnings => (); my $x; say $x+1' Use of uninitialized value $x in addition (+) at -e line 1. 1
Of course, a workaround is straightforward (but not what I'm looking for):
$ perl -Mstrict -E 'BEGIN { require warnings if 1==1 } my $x; say $x+1 +' 1
Running the code through B::Deparse shows the syntax is fine; however, using the code it produces doesn't resolve the problem:
$ perl -MO=Deparse,-p -e 'use if 1==1, warnings => ()' use if (1, 'warnings', ()); -e syntax OK $ perl -Mstrict -E 'use if (1, "warnings", ()); my $x; say $x+1' Use of uninitialized value $x in addition (+) at -e line 1. 1
I've used warnings because it was handy for illustrating the problem (I don't actually have any real code with 'use warnings ();'). Here's a few more examples, all of which abort compilation when the if pragma is used.
$ perl -wMstrict -E 'use open ()' $ perl -wMstrict -E 'use if 1==1, open => ()' open: needs explicit list of PerlIO layers at -e line 1. BEGIN failed--compilation aborted at -e line 1. $ perl -wMstrict -E 'use feature ()' $ perl -wMstrict -E 'use if 1==1, feature => ()' No features specified at -e line 1. BEGIN failed--compilation aborted at -e line 1. $ perl -wMstrict -E 'use List::Util ()' $ perl -wMstrict -E 'use if 1==1, List::Util => ()' Bareword "List::Util" not allowed while "strict subs" in use at -e lin +e 1. Execution of -e aborted due to compilation errors.
I've looked at the source code for the if pragma. It's actually fairly short (without the POD) so I've posted it here for reference. This version (0.0606) is what's used in the latest production (5.24.1) and development (5.25.11) Perls.
package if; $VERSION = '0.0606'; sub work { my $method = shift() ? 'import' : 'unimport'; unless (@_ >= 2) { my $type = ($method eq 'import') ? 'use' : 'no'; die "Too few arguments to '$type if' (some code returning an empty + list in list context?)" } return unless shift; # CONDITION my $p = $_[0]; # PACKAGE (my $file = "$p.pm") =~ s!::!/!g; require $file; # Works even if $_[0] is a keyword (like open) my $m = $p->can($method); goto &$m if $m; } sub import { shift; unshift @_, 1; goto &work } sub unimport { shift; unshift @_, 0; goto &work } 1;
As you can see, goto &NAME is used in all subroutines. I'm fairly sure that the empty import list gets lost in 'goto &work' because '@_' gets flattened:
$ perl -wMstrict -E '@_ = (1, 2, ()); say 0+@_' 2
This results in all statements like this:
use if CONDITION, MODULE => ();
effectively becoming this:
use if CONDITION, MODULE;
without MODULE being quoted.
— Ken
|
|---|