You're on the right track
use Carp 'croak';
sub UNIVERSAL::AUTOLOAD {
my($m) = $UNIVERSAL::AUTOLOAD =~ /::(\w+)$/;
croak "Undefined subroutine &$UNIVERSAL::AUTOLOAD called"
unless $m eq 'global';
print "this sub really is global\n";
}
global();
{
package foo;
global();
}
__output__
this sub really is global
this sub really is global
Although there's the obvious caveat that the global behaviour is no longer honoured if the current package already implements
AUTOLOAD and doesn't delegate to
UNIVERSAL::AUTOLOAD upon failure. That and it's fairly hideous. I'd personally opt for defining a subroutine in
main then just calling it the lazy way e.g
sub main::global {
print "this isn't quite global\n";
}
::global();
{
package foo;
::global();
}
__output__
this isn't quite global
this isn't quite global