diff options
author | Florian Ragwitz <rafl@debian.org> | 2010-09-16 01:25:06 +0200 |
---|---|---|
committer | Florian Ragwitz <rafl@debian.org> | 2010-09-16 01:25:06 +0200 |
commit | f4842d441b5a6d61b4c55f27158b8e7cefa26488 (patch) | |
tree | b396f50c49ee2f2c9d2f288a6c8d38735ca7f283 /dist | |
parent | 8d12641540cbc9e9b74b952d35095f2fc4907f4e (diff) | |
download | perl-f4842d441b5a6d61b4c55f27158b8e7cefa26488.tar.gz |
if.pm is upstream blead
Diffstat (limited to 'dist')
-rw-r--r-- | dist/if/if.pm | 56 | ||||
-rw-r--r-- | dist/if/t/if.t | 37 |
2 files changed, 93 insertions, 0 deletions
diff --git a/dist/if/if.pm b/dist/if/if.pm new file mode 100644 index 0000000000..5f6bcc8ae1 --- /dev/null +++ b/dist/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/dist/if/t/if.t b/dist/if/t/if.t new file mode 100644 index 0000000000..badab64f04 --- /dev/null +++ b/dist/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'); |