| 1 | package if; | ||
| 2 | |||
| 3 | $VERSION = '0.0606'; | ||
| 4 | |||
| 5 | sub work { | ||
| 6 | my $method = shift() ? 'import' : 'unimport'; | ||
| 7 | unless (@_ >= 2) { | ||
| 8 | my $type = ($method eq 'import') ? 'use' : 'no'; | ||
| 9 | die "Too few arguments to '$type if' (some code returning an empty list in list context?)" | ||
| 10 | } | ||
| 11 | return unless shift; # CONDITION | ||
| 12 | |||
| 13 | my $p = $_[0]; # PACKAGE | ||
| 14 | (my $file = "$p.pm") =~ s!::!/!g; | ||
| 15 | require $file; # Works even if $_[0] is a keyword (like open) | ||
| 16 | my $m = $p->can($method); | ||
| 17 | goto &$m if $m; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub import { shift; unshift @_, 1; goto &work } | ||
| 21 | sub unimport { shift; unshift @_, 0; goto &work } | ||
| 22 | |||
| 23 | 1; | ||
| 24 | __END__ |