| 1 | 1 | ▶ | use 5.012; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package namespace::lexical; | ||||||||||
| 6 | |||||||||||
| 7 | our $AUTHORITY = 'cpan:TOBYINK'; | ||||||||||
| 8 | our $VERSION = '0.002'; | ||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use Package::Stash (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=10.5
| |||||||||||
| 13 | 0 | ▶ | if (eval { require Lexical::Sub; 1 }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | 0 | *_LEXICAL_SUB_CLASS = sub () { 'Lexical::Sub' }; | |||||||||
| 15 | 0 | *_LEXICAL_SUB_IMPORT_METHOD = sub () { 'import' }; | |||||||||
| 16 | } | ||||||||||
| 17 | else { | ||||||||||
| 18 | 1 | require Lexical::Importer; | |||||||||
| 19 | 0 | *_LEXICAL_SUB_CLASS = sub () { 'Lexical::Importer' }; | |||||||||
| 20 | 0 | *_LEXICAL_SUB_IMPORT_METHOD = sub () { '_import_lex_sub' }; | |||||||||
| 21 | } | ||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | sub import { | ||||||||||
| 25 | my $class = shift; | ||||||||||
| 26 | my $for = caller; | ||||||||||
| 27 | my $stash = 'Package::Stash'->new($for); | ||||||||||
| 28 | my $subs = $stash->get_all_symbols('CODE'); | ||||||||||
| 29 | for my $name (sort keys %$subs) { | ||||||||||
| 30 | $class->lexicalize($stash, $name, $subs->{$name}); | ||||||||||
| 31 | } | ||||||||||
| 32 | return; | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub lexicalize { | ||||||||||
| 36 | my $class = shift; | ||||||||||
| 37 | my ($stash, $name, $coderef) = @_; | ||||||||||
| 38 | if (defined $stash) { | ||||||||||
| 39 | $stash = 'Package::Stash'->new($stash) unless ref $stash; | ||||||||||
| 40 | $coderef ||= $stash->get_symbol('&'.$name); | ||||||||||
| 41 | $stash->remove_symbol('&'.$name); | ||||||||||
| 42 | } | ||||||||||
| 43 | die "coderef plz" unless defined $coderef; | ||||||||||
| 44 | _LEXICAL_SUB_CLASS->${\ _LEXICAL_SUB_IMPORT_METHOD }($name, $coderef); | ||||||||||
| 45 | return; | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 1; | ||||||||||
| 49 | |||||||||||
| 50 | __END__ | ||||||||||