Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Creating a global function

by ambrus (Abbot)
on Sep 06, 2004 at 18:26 UTC ( #388847=note: print w/replies, xml ) Need Help??

in reply to Creating a global function

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.)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2021-12-06 06:26 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (31 votes). Check out past polls.