Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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


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]
[Corion]: Test...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-06-23 16:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.