Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Creating a global function

by broquaint (Abbot)
on Sep 07, 2004 at 04:51 UTC ( [id://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

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-23 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found