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

Creating a global function

by Anonymous Monk
on Sep 06, 2004 at 15:23 UTC ( [id://388819]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if it is possible to create a global function.
(By global I mean a function that is available in all namespaces)

What I would like to be able to do is:

use Some::Module 'function'; function "..."; package Another::Module; function "..."; package Yet::Another::Module; function "..."; ...

Is this possible? (without importing the function in each package?)

The modules/packages in which the modules is used is not know in front so it is not possible to export them at the start...

So far I've tried to export the function to the CORE::GLOBAL package and to the CORE package but this did not gave the result I want...

Anyone know if this is possible? and/or where I can read about it? and/or how to do it?

Replies are listed 'Best First'.
Re: Creating a global function
by ambrus (Abbot) on Sep 06, 2004 at 18:26 UTC

    Yes. The trick is that the name must start with a control character, which you must write with the caret-notation in perl code, and also you have to use braces with multi-character names. Symbols starting with a control character are implicitly pu tin the main package. For example:

    #!perl -w package Foo; *{^_greet} = sub { print "hello, "; }; *^E = sub { print "world\n"; }; package Bar; &{^_greet}(); &^E();

    Note that the symbols starting with punctation or control characters (except for those starting with ^_) are reserved for future versions of perl, perl already uses subs starting with open parens.

    Also you can use the shorthand ::foo() for main::foo() which is not much longer than foo() but is much uglier.

    The reason why there are no global functions of simpler names in perl is because it could shadow the similarly called function of any package.

    (Update: finished broken sentence noted by bart.)

Re: Creating a global function
by Zaxo (Archbishop) on Sep 06, 2004 at 15:36 UTC

    I'd prefer to address the function under its fully qualified name, my $foo = Some::Module::function(@bar); That will work from anywhere and does not clutter up other namespaces.

    If you still want to do that, subclass Exporter in Some::Module and place 'function' in @EXPORT to get the function by default, or @EXPORT_OK to get it by the syntax in your example. To make it available everywhere at the same time, import it at top level, into main::.

    After Compline,
    Zaxo

      I'm aware of that but that's not good... I would like to leave out the modules name.
Re: Creating a global function
by rinceWind (Monsignor) on Sep 06, 2004 at 15:41 UTC
    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

      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.

      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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found