Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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
HTH

_________
broquaint


Comment on Re^2: Creating a global function
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-07-24 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls