Created
October 27, 2010 12:42
-
-
Save dolmen/648949 to your computer and use it in GitHub Desktop.
Test a trick: local alias for a global class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Could be used for a future version of POE::Component::Logger to avoid polluting the global namespace. | |
use feature 'switch'; | |
#use strict; | |
# Global Logger | |
sub Logger::log | |
{ | |
print "global: $_[1]\n"; | |
} | |
BEGIN { | |
$INC{'My/Logger.pm'} = __FILE__; | |
{ | |
package My::Logger; | |
our $VERSION = '1.00'; | |
sub VERSION | |
{ | |
print "VERSION @_\n"; | |
1 | |
} | |
sub log | |
{ | |
print "local: $_[1]\n"; | |
} | |
print __PACKAGE__."\n"; | |
sub import | |
{ | |
print "import from ".(scalar caller(0))."\n"; | |
# Add a local 'Logger' function which expands to the package name | |
# From the caller, "Logger->log(...)" | |
# will be expanded to "'My::Logger'->log(...)" | |
*{caller(0).'::Logger'} = sub () { __PACKAGE__ }; | |
} | |
} | |
# use the module | |
given($ARGV[0]) { | |
when (1) { eval "use My::Logger"; } | |
when (2) { eval "use My::Logger ()"; } | |
when (3) { eval "require My::Logger"; } | |
when (4) { eval "use My::Logger '1.01'"; } | |
} | |
die $@ if $@; | |
} | |
Logger->log('ok'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment