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

Adding Global Keyword?

by Sec (Scribe)
on Feb 17, 2006 at 14:58 UTC ( #530960=perlquestion: print w/replies, xml ) Need Help??
Sec has asked for the wisdom of the Perl Monks concerning the following question:

I have been trying to use a new global keyword in a script, but have failed so far. My try so far.
package CORE::GLOBAL; use subs qw(dwim); sub dwim { print "dwim\n"; } package test; dwim();
But if I try to do that with an pre existing keyword it works fine:
package CORE::GLOBAL; use subs qw(warn); sub warn { print "dwim\n"; } package test; warn();
Can anybody help?

Replies are listed 'Best First'.
Re: Adding Global Keyword?
by Corion (Pope) on Feb 17, 2006 at 15:07 UTC

    You don't want to do that.

    As you've made it clear in the CB that this is out of curiosity only, here is a way to add new "global" functions to everywhere:

    package UNIVERSAL; sub AUTOLOAD{print $AUTOLOAD}; package main; Test(); &{'Just another Perl Hacker'}();

    You should add some checking that you raise errors for "keywords" you don't know.

    Also interesting to you might be

Re: Adding Global Keyword?
by dragonchild (Archbishop) on Feb 17, 2006 at 15:24 UTC
    What's wrong with following 50 years of good coding practices and adding a single line to each package that uses your dwim() to explicitly import it? Otherwise, you're creating a maintenance nightmare for the poor schlup who has to read your code in 6 months.

    Remember, what you think of as a good idea right now will be cursed by the guy who reads it. 90% of the time, that guy will be you. If you want to do something because it's "clever", remember that's code for "stupid".

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      The question came from a friend, who wanted to use it just temporarily for debugging. He didn't want to import his "Debugging module" in every single package.
      I know that in this case "Module::debug()" works. I looked into this problem out of sheer curiosity. It feels strangely unperlish to me that perl allows me to override existing builtin keywords, but not add my own.
        Look into if and conditional compilation using constants.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        I'd cheat.
        sub what::s { ... debug code here... }
        Then later, anywhere...
        what's($foo); what's($bar);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://530960]
Approved by Corion
Front-paged by Courage
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2017-03-29 00:13 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (343 votes). Check out past polls.