latest switch/say/~~
[p5sagit/p5-mst-13.2.git] / lib / if.pm
CommitLineData
cd16c24c 1package if;
2
fae40608 3$VERSION = '0.05';
cd16c24c 4
5sub work {
6 my $method = shift() ? 'import' : 'unimport';
fae40608 7 die "Too few arguments to `use if' (some code returning an empty list in list context?)"
8 unless @_ >= 2;
cd16c24c 9 return unless shift; # CONDITION
a3e5cfd4 10
11 my $p = $_[0]; # PACKAGE
b9761643 12 (my $file = "$p.pm") =~ s!::!/!g;
2f761939 13 require $file; # Works even if $_[0] is a keyword (like open)
a3e5cfd4 14 my $m = $p->can($method);
15 goto &$m if $m;
cd16c24c 16}
17
18sub import { shift; unshift @_, 1; goto &work }
19sub unimport { shift; unshift @_, 0; goto &work }
20
211;
22__END__
23
24=head1 NAME
25
26if - C<use> a Perl module if a condition holds
27
28=head1 SYNOPSIS
29
30 use if CONDITION, MODULE => ARGUMENTS;
31
32=head1 DESCRIPTION
33
34The construct
35
36 use if CONDITION, MODULE => ARGUMENTS;
37
38has no effect unless C<CONDITION> is true. In this case the effect is
39the same as of
40
41 use MODULE ARGUMENTS;
42
fae40608 43Above C<< => >> provides necessary quoting of C<MODULE>. If not used (e.g.,
44no ARGUMENTS to give), you'd better quote C<MODULE> yourselves.
45
cd16c24c 46=head1 BUGS
47
48The current implementation does not allow specification of the
49required version of the module.
50
51=head1 AUTHOR
52
53Ilya Zakharevich L<mailto:perl-module-if@ilyaz.org>.
54
55=cut
56