in reply to Creating a global function

I think what you are after is the UNIVERSAL:: name space, not CORE::GLOBAL. CORE::GLOBAL is used to override built-ins if I remember correctly.

Update: PodMaster is perfectly correct in that UNIVERSAL does not get exported into every name space. I was just suggesting that functions could be made available (via inheritance) to all packages, with UNIVERSAL.

--
I'm Not Just Another Perl Hacker

Replies are listed 'Best First'.
Re^2: Creating a global function
by PodMaster (Abbot) on Sep 06, 2004 at 15:45 UTC
    No. Every class isa UNIVERSAL, but declaring a sub in the UNVERSAL namespace doesn't export it into every namespace.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re^2: Creating a global function
by broquaint (Abbot) on Sep 07, 2004 at 04:51 UTC
    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

      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?