I would like to conditionally download a package if the program name is a test script ending in .t.
However, I encountered an error in which to use if it fails when the condition is a regular expression. I tested this in Perl 5.10 and 5.16.
Below is my test script ending in .t:
use v5.10;
BEGIN { say "\$0 is '$0'" }
use if $0 =~ /\.t\z/, 'List::Util', ('pairmap');
say "List::Util is " . ( $INC{"List/Util.pm"} ? '' : 'NOT ' ) . 'included';
Outputs:
$ ./test.t
$0 is './test.t'
List::Util is included
However, the same file with the .pl extension does not work:
$ ./test.pl
$0 is './test.pl'
Can't locate pairmap.pm in @INC (@INC contains: /usr/lib64/perl5/5.10.0 /usr/lib64/perl5 /usr/local/share/perl5/x86_64-linux-thread-multi /usr/local/share/perl5 /usr/local/lib64/perl5 /usr/share/perl5 /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/share/perl5/if.pm line 13.
BEGIN failed--compilation aborted at ./test.pl line 7.
I can force the code to work if I double-tap the regular expression or change it to substr comparison:
use if !!( $0 =~ /\.t\z/ ), 'List::Util', ('pairmap');
use if substr( $0, -2 ) eq '.t', 'List::Util', ('pairmap');
Outputs:
$ ./test.pl
$0 is './test.pl'
List::Util is NOT included
Is this a known bug? If so, in which version has it been fixed?
source
share