Skip to content

Instantly share code, notes, and snippets.

@dolmen
Created October 27, 2010 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolmen/648949 to your computer and use it in GitHub Desktop.
Save dolmen/648949 to your computer and use it in GitHub Desktop.
Test a trick: local alias for a global class
# 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