File | /usr/share/perl/5.10/if.pm |
Statements Executed | 24 |
Total Time | 6.93e-05 seconds |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
2 | 2 | 1 | 22µs | 22µs | import | if::
0 | 0 | 0 | 0s | 0s | unimport | if::
0 | 0 | 0 | 0s | 0s | work | if::
Line | Stmts. | Exclusive Time | Avg. | Code |
---|---|---|---|---|
1 | package if; | |||
2 | ||||
3 | 1 | 700ns | 700ns | $VERSION = '0.05'; |
4 | ||||
5 | sub work { | |||
6 | 2 | 2µs | 800ns | my $method = shift() ? 'import' : 'unimport'; |
7 | 2 | 1µs | 500ns | die "Too few arguments to `use if' (some code returning an empty list in list context?)" |
8 | unless @_ >= 2; | |||
9 | 2 | 500ns | 250ns | return unless shift; # CONDITION |
10 | ||||
11 | 2 | 1µs | 500ns | my $p = $_[0]; # PACKAGE |
12 | 2 | 9µs | 4µs | (my $file = "$p.pm") =~ s!::!/!g; |
13 | 2 | 6µs | 3µs | require $file; # Works even if $_[0] is a keyword (like open) |
14 | 2 | 30µs | 15µs | my $m = $p->can($method); # spent 10µs making 2 calls to UNIVERSAL::can, avg 5µs/call |
15 | 2 | 4µs | 2µs | goto &$m if $m; |
16 | } | |||
17 | ||||
18 | 6 | 12µs | 2µs | # spent 22µs within if::import which was called 2 times, avg 11µs/call:
# once (14µs+0s) at line 12 of /usr/local/lib/perl/5.10.0/Moose/Object.pm
# once (9µs+0s) at line 13 of /usr/local/lib/perl/5.10.0/Moose/Object.pm |
19 | sub unimport { shift; unshift @_, 0; goto &work } | |||
20 | ||||
21 | 1 | 4µs | 4µs | 1; |
22 | __END__ | |||
23 | ||||
24 | =head1 NAME | |||
25 | ||||
26 | if - 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 | ||||
34 | The construct | |||
35 | ||||
36 | use if CONDITION, MODULE => ARGUMENTS; | |||
37 | ||||
38 | has no effect unless C<CONDITION> is true. In this case the effect is | |||
39 | the same as of | |||
40 | ||||
41 | use MODULE ARGUMENTS; | |||
42 | ||||
43 | Above C<< => >> provides necessary quoting of C<MODULE>. If not used (e.g., | |||
44 | no ARGUMENTS to give), you'd better quote C<MODULE> yourselves. | |||
45 | ||||
46 | =head1 BUGS | |||
47 | ||||
48 | The current implementation does not allow specification of the | |||
49 | required version of the module. | |||
50 | ||||
51 | =head1 AUTHOR | |||
52 | ||||
53 | Ilya Zakharevich L<mailto:perl-module-if@ilyaz.org>. | |||
54 | ||||
55 | =cut | |||
56 |