diff options
author | Nicholas Clark <nick@ccl4.org> | 2009-09-13 16:30:18 +0100 |
---|---|---|
committer | Nicholas Clark <nick@ccl4.org> | 2009-09-13 16:30:18 +0100 |
commit | 00c2f0c97c1956309eb5a69763c48663bca3a767 (patch) | |
tree | c4f78439bbdbf32c4cc92664269ad2651435ee30 /ext | |
parent | 359ab69c2e6e9e71dc5be4023a8ee521e8795dac (diff) | |
download | perl-00c2f0c97c1956309eb5a69763c48663bca3a767.tar.gz |
Move if from lib to ext.
Diffstat (limited to 'ext')
-rw-r--r-- | ext/.gitignore | 1 | ||||
-rw-r--r-- | ext/if/if.pm | 56 | ||||
-rw-r--r-- | ext/if/t/if.t | 37 |
3 files changed, 94 insertions, 0 deletions
diff --git a/ext/.gitignore b/ext/.gitignore index 2f4f2597cd..56e1abeb83 100644 --- a/ext/.gitignore +++ b/ext/.gitignore @@ -46,6 +46,7 @@ ppport.h /Filter-Util-Call/Makefile.PL /Hash-Util-FieldHash/Makefile.PL /I18N-LangTags/Makefile.PL +/if/Makefile.PL /IO-Zlib/Makefile.PL /IPC-Cmd/Makefile.PL /IPC-Open2/Makefile.PL diff --git a/ext/if/if.pm b/ext/if/if.pm new file mode 100644 index 0000000000..5f6bcc8ae1 --- /dev/null +++ b/ext/if/if.pm @@ -0,0 +1,56 @@ +package if; + +$VERSION = '0.05'; + +sub work { + my $method = shift() ? 'import' : 'unimport'; + die "Too few arguments to `use if' (some code returning an empty list in list context?)" + unless @_ >= 2; + 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; +__END__ + +=head1 NAME + +if - C<use> a Perl module if a condition holds + +=head1 SYNOPSIS + + use if CONDITION, MODULE => ARGUMENTS; + +=head1 DESCRIPTION + +The construct + + use if CONDITION, MODULE => ARGUMENTS; + +has no effect unless C<CONDITION> is true. In this case the effect is +the same as of + + use MODULE ARGUMENTS; + +Above C<< => >> provides necessary quoting of C<MODULE>. If not used (e.g., +no ARGUMENTS to give), you'd better quote C<MODULE> yourselves. + +=head1 BUGS + +The current implementation does not allow specification of the +required version of the module. + +=head1 AUTHOR + +Ilya Zakharevich L<mailto:perl-module-if@ilyaz.org>. + +=cut + diff --git a/ext/if/t/if.t b/ext/if/t/if.t new file mode 100644 index 0000000000..badab64f04 --- /dev/null +++ b/ext/if/t/if.t @@ -0,0 +1,37 @@ +#!./perl + +my $t = 1; +print "1..5\n"; +sub ok { + print "not " unless shift; + print "ok $t # ", shift, "\n"; + $t++; +} + +my $v_plus = $] + 1; +my $v_minus = $] - 1; + +unless (eval 'use open ":std"; 1') { + # pretend that open.pm is present + $INC{'open.pm'} = 'open.pm'; + eval 'sub open::foo{}'; # Just in case... +} + + +ok( eval "use if ($v_minus > \$]), strict => 'subs'; \${'f'} = 12" eq 12, + '"use if" with a false condition, fake pragma'); + +ok( eval "use if ($v_minus > \$]), strict => 'refs'; \${'f'} = 12" eq 12, + '"use if" with a false condition and a pragma'); + +ok( eval "use if ($v_plus > \$]), strict => 'subs'; \${'f'} = 12" eq 12, + '"use if" with a true condition, fake pragma'); + +ok( (not defined eval "use if ($v_plus > \$]), strict => 'refs'; \${'f'} = 12" + and $@ =~ /while "strict refs" in use/), + '"use if" with a true condition and a pragma'); + +# Old version had problems with the module name `open', which is a keyword too +# Use 'open' =>, since pre-5.6.0 could interpret differently +ok( (eval "use if ($v_plus > \$]), 'open' => IN => ':crlf'; 12" || 0) eq 12, + '"use if" with open'); |