Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: Creating a global function

by broquaint (Abbot)
on Sep 07, 2004 at 04:51 UTC ( #388931=note: print w/ replies, xml ) Need Help??

in reply to Re: Creating a global function
in thread Creating a global function

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


Comment on Re^2: Creating a global function
Select or Download Code
Replies are listed 'Best First'.
Re^3: Creating a global function
by ambrus (Abbot) on Sep 10, 2004 at 08:12 UTC

    I've read somewhere that although AUTOLOAD is currently always searched through the inheritance tree, in future perl versions this will happen only when you are AUTOLOADing a method call, not a simple function call. Thus, this solution may not work in future versions. Am I right here?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://388931]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2015-11-25 11:16 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (674 votes), past polls