| 1 | package Sub::Prepend; | ||||||||||
| 2 | 6 | ▶ | use 5.006001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 6 | $VERSION = 0.01; | |||||||||
| 5 | |||||||||||
| 6 | 6 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 6 | @ISA = Exporter::; | |||||||||
| 8 | 6 | @EXPORT_OK = qw/ prepend /; | |||||||||
| 9 | 6 | $EXPORT_TAGS{ALL} = \@EXPORT_OK; | |||||||||
| 10 | |||||||||||
| 11 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 6 | ▶ | use Scalar::Util 1.11 'set_prototype'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use Symbol qw/ qualify_to_ref qualify /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 6 | ▶ | use vars qw/ $CALLER /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 6 | *CALLER = \1; | |||||||||
| 19 | |||||||||||
| 20 | 8 | ▶ | sub prepend { | ||||||||
|
Subroutine
prepend: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 21 | 8 | my ($name, $code) = @_; | |||||||||
| 22 | |||||||||||
| 23 | 8 | my $glob = qualify_to_ref($name => scalar caller); | |||||||||
| 24 | |||||||||||
| 25 | 8 | ▶ | defined &$glob | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | or croak(sprintf __PACKAGE__ . ": Subroutine &%s not defined", qualify($name => scalar caller)); | ||||||||||
| 27 | |||||||||||
| 28 | 7 | my $orig = \&$glob; | |||||||||
| 29 | |||||||||||
| 30 | my $prepender = set_prototype( | ||||||||||
| 31 | 7 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 7 | &$code; # Don't use parenthesis! | |||||||||
| 33 | 7 | goto &$orig; | |||||||||
| 34 | }, | ||||||||||
| 35 | 7 | prototype $orig | |||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | { | ||||||||||
| 39 | 6 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 7 | *$glob = $prepender; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 7 | return; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 6 | 1; | |||||||||
| 47 | |||||||||||
| 48 | __END__ | ||||||||||