| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package as; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # make sure we have version info for this module | 
| 4 |  |  |  |  |  |  | $VERSION= '0.12'; | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # be as strict and verbose as possible | 
| 7 | 1 |  |  | 1 |  | 72211 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 8 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # modules that we need | 
| 11 | 1 |  |  | 1 |  | 5 | use Carp qw( croak ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 53 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # hash containing already aliased modules | 
| 14 |  |  |  |  |  |  | my %ALIASED; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # make sure this is done before anything else | 
| 17 |  |  |  |  |  |  | BEGIN { | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # allow dirty stuff happening without anyone complaining about it | 
| 20 | 1 |  |  | 1 |  | 6 | no strict 'refs'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 21 | 1 |  |  | 1 |  | 7 | no warnings 'redefine'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 488 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =for Explanation: | 
| 24 |  |  |  |  |  |  | We want to take over the standard -require- functionality so that we | 
| 25 |  |  |  |  |  |  | can inject ourselves in the -use- processing and to install our own | 
| 26 |  |  |  |  |  |  | "import" functionality.  If there is no previous custom installed | 
| 27 |  |  |  |  |  |  | -require- handler yet, then we don't bother calling it, which should | 
| 28 |  |  |  |  |  |  | have some execution benefits. | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =cut | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 1 |  |  | 1 |  | 6 | my $old= \&CORE::GLOBAL::require; | 
| 33 | 1 |  |  |  |  | 2 | eval { $old->() }; | 
|  | 1 |  |  |  |  | 16 |  | 
| 34 | 1 | 50 |  |  |  | 7 | $old= undef if $@ =~ m#CORE::GLOBAL::require#; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # install our own -require- handler | 
| 37 |  |  |  |  |  |  | *CORE::GLOBAL::require = sub { | 
| 38 | 33 |  |  | 33 |  | 14810 | my $file= $_[0]; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | # perform what was originally expected | 
| 41 | 33 |  |  |  |  | 51 | my $return; | 
| 42 | 33 | 50 |  |  |  | 212 | if ($old) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 43 | 0 |  |  |  |  | 0 | ($return)= eval { $old->($file) }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # seems to be a version check | 
| 47 |  |  |  |  |  |  | elsif ( $file =~ m#^v?[\d\.]+$# ) { | 
| 48 | 2 |  |  |  |  | 4 | ($return)= eval { CORE::require( 0 + $file ) }; # needs num value | 
|  | 2 |  |  |  |  | 36 |  | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | # no special -require- action needed, already loaded before | 
| 52 |  |  |  |  |  |  | elsif ( $INC{$file} ) { | 
| 53 | 24 |  |  |  |  | 38 | $return= 1; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # first time -require- | 
| 57 |  |  |  |  |  |  | else { | 
| 58 | 7 |  |  |  |  | 15 | ($return)= eval { CORE::require($file) }; | 
|  | 7 |  |  |  |  | 4618 |  | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # something wrong, cleanup and bail out | 
| 62 | 33 | 100 |  |  |  | 1517 | if ($@) { | 
| 63 | 1 |  |  |  |  | 10 | $@ =~ s#(?: in require)? at /?(?:\w+/)*as\.pm line \d+.\s+##s; | 
| 64 | 1 |  |  |  |  | 96 | croak $@; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # not requiring a module, we're done | 
| 68 | 32 |  |  |  |  | 59 | my $module= shift; | 
| 69 | 32 | 100 |  |  |  | 179 | return $return if $module !~ s#\.pm$##; | 
| 70 | 30 |  |  |  |  | 63 | $module =~ s#/#::#g; | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | # there's an "import" already, embed it | 
| 73 | 30 | 100 |  |  |  | 244 | if ( my $import= $module->can('import') ) { | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | # install our own importer | 
| 76 | 29 |  |  |  |  | 214 | *{ $module . '::import' }= sub { | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # we need to do aliasing: do it and remove them params | 
| 79 | 39 | 100 | 100 | 39 |  | 396 | if ( @_ >= 3 and $_[-2] eq 'as' ) { | 
| 80 | 4 |  |  |  |  | 34 | my ( undef, $alias )= splice @_, -2; | 
| 81 | 4 |  |  |  |  | 11 | _alias( $module, $alias ); | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | # hopefully keep same scope as caller | 
| 85 | 37 | 50 |  |  |  | 2713 | goto &$import if @_; | 
| 86 | 29 |  |  |  |  | 141 | }; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | # no import to embed, simply install our own | 
| 91 |  |  |  |  |  |  | else { | 
| 92 | 1 |  |  |  |  | 3 | *{ $module . '::import' }= \&_import; | 
|  | 1 |  |  |  |  | 5 |  | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | # really done now | 
| 96 | 30 |  |  |  |  | 122 | return $return; | 
| 97 | 1 |  |  |  |  | 59 | }; | 
| 98 |  |  |  |  |  |  | }     #BEGIN | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | # satisfy -require- | 
| 101 |  |  |  |  |  |  | 1; | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | #--------------------------------------------------------------------------- | 
| 104 |  |  |  |  |  |  | # | 
| 105 |  |  |  |  |  |  | # Internal subroutines | 
| 106 |  |  |  |  |  |  | # | 
| 107 |  |  |  |  |  |  | #--------------------------------------------------------------------------- | 
| 108 |  |  |  |  |  |  | # _alias | 
| 109 |  |  |  |  |  |  | # | 
| 110 |  |  |  |  |  |  | # Perform the actual stash aliasing | 
| 111 |  |  |  |  |  |  | # | 
| 112 |  |  |  |  |  |  | #  IN: 1 original class name | 
| 113 |  |  |  |  |  |  | #      2 alias class name | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub _alias { | 
| 116 | 4 |  |  | 4 |  | 8 | my ( $module, $alias )= @_; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | # allow dirty stuff happening without anyone complaining about it | 
| 119 | 1 |  |  | 1 |  | 4 | no strict 'refs'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 268 |  | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | # make sure we're not treading on already taken territory | 
| 122 | 4 | 100 |  |  |  | 5 | if ( %{ $alias . '::' } ) { | 
|  | 4 |  |  |  |  | 18 |  | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | # alias already used, bail out if not same | 
| 125 | 3 | 100 |  |  |  | 10 | if ( my $old= $ALIASED{$alias} ) { | 
| 126 | 2 | 100 |  |  |  | 105 | croak | 
| 127 |  |  |  |  |  |  | "Cannot alias '$alias' to '$module': already aliased to '$old'" | 
| 128 |  |  |  |  |  |  | if $old ne $module; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | # not aliased yet, but, but, but... | 
| 132 |  |  |  |  |  |  | else { | 
| 133 | 1 |  |  |  |  | 203 | croak "Cannot alias '$alias' to '$module': already taken"; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | # perform the actual stash aliasing and remember it | 
| 138 | 2 |  |  |  |  | 4 | *{ $alias . '::' }= *{ $module . '::' }; | 
|  | 2 |  |  |  |  | 25 |  | 
|  | 2 |  |  |  |  | 4 |  | 
| 139 | 2 |  |  |  |  | 10 | $ALIASED{$alias}= $module; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 2 |  |  |  |  | 8 | s#::#/#g foreach ( $module, $alias ); | 
| 142 | 2 |  |  |  |  | 8 | $INC{"$alias.pm"}= $INC{"$module.pm"}; | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 2 |  |  |  |  | 3 | return; | 
| 145 |  |  |  |  |  |  | }    #_alias | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | #--------------------------------------------------------------------------- | 
| 148 |  |  |  |  |  |  | # _import | 
| 149 |  |  |  |  |  |  | # | 
| 150 |  |  |  |  |  |  | # Generic importer, same for all modules that didn't have an import yet | 
| 151 |  |  |  |  |  |  | # | 
| 152 |  |  |  |  |  |  | #  IN: 1 class | 
| 153 |  |  |  |  |  |  | #      2..N parameters | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | sub _import { | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | # nothing to be done | 
| 158 | 1 | 50 | 33 | 1 |  | 517 | return if @_ < 3 or $_[-2] ne 'as'; | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | # perform the alias | 
| 161 | 0 |  |  |  |  |  | _alias( $_[0], $_[-1] ); | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 0 |  |  |  |  |  | return; | 
| 164 |  |  |  |  |  |  | }    #_import | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | #--------------------------------------------------------------------------- | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | __END__ |