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

I've got a few debugging subroutines that I'd like to be TRULY global - that is, to be automatically exported into every package, so I can call them without a package prefix. This is for development only and I'm willing to resort to hackery to make it work.

So at the beginning of my application I do

   #!/usr/bin/perl
   use Debug;

and then in some module

   package SomeModule;
   dp $foo;

where 'dp' is one of the subroutines auto-exported by Debug. Note that SomeModule did not have to say 'use Debug' or 'Debug::dp'.

I figured this had to exist in CPAN, but several searches did not turn it up.

My initial idea was for Debug to

Yes, this is the height of Laziness, as I could just type Debug::dp everywhere (or even d::dp if I named the package d.) But I figure with the saved keystrokes I might make up the development time by the end my programming life.

Thanks for any tips!

Jon

  • Comment on Truly global subroutines - Auto-exporting symbols into all packages

Replies are listed 'Best First'.
Re: Truly global subroutines - Auto-exporting symbols into all packages
by ikegami (Patriarch) on Dec 04, 2009 at 18:13 UTC

    Why all the confusion to avoid use Debug;? It's a bad idea.

    If I'd want to do it, I'd just create the sub in the main namespace and use :: in front of the sub name to look into that namespace.

    package Debug; sub ::dp { print "Moo!\n"; } package test; ::dp;
    $ perl a.pl Moo!

    If you're truly insane, you could take advantage of the fact that certain symbol default to the main namespace instead of the current namespace.

    package main; sub _ { print "Foo!\n" } *% = sub { print "Bar!\n" }; package test; _(); &%();
    $ perl a.pl Foo! Bar!

    Basically, any of the special symbol that's allowed as one-character var names (see perlvar) is can be used that way.

    The catch is that except for _, they're not normally allowed as identifiers. You'll need to use the &name(...) syntax to call them, and you'll need to assign to the glob to define them.

    But that's a bad idea.

Re: Truly global subroutines - Auto-exporting symbols into all packages
by dirving (Friar) on Dec 04, 2009 at 18:42 UTC
    There are a lot of reasons why this is a really bad idea:
    • Modules can no longer be used independently of the main program, since they require a declaration of use Debug somewhere. This kind of defeats the point of modules: having a chunk of code that can be re-used in different contexts.
    • Modules not written by you that include a sub-routine called dp now fail if they are loaded before Debug, since their dp will be over-written when Debug is loaded. This is going to be a nightmare to trace down, because when you put together a minimal test case for the module it is going to work fine.
    • It makes maintenance hard, since there is no way for the maintenance programmer to know where dp is coming from.

    The one second it takes to type 'use Debug;' is smaller by orders of magnitude than the amount of thinking and typing time required to implement even the most trivial module. If you are really worried about it, create a module skeleton that includes all the declarations you want and get your text editor to use it as a template for any new .pm files you create.

    -- David Irving
Re: Truly global subroutines - Auto-exporting symbols into all packages
by AnomalousMonk (Archbishop) on Dec 04, 2009 at 18:50 UTC

    As others have pointed out, such a subversion of Perl's namespace management is a Bad Idea.

    ... this is the height of Laziness ...

    It's more like employment insurance, for it will guarantee you (or those who follow you) many happy, billable hours of debugging.

    ... I figure with the saved keystrokes I might make up the development time by the end my programming life.

    OTOH, after a few 3 AM debug sessions, you may want to end your programming life by your own hand!

Re: Truly global subroutines - Auto-exporting symbols into all packages
by MidLifeXis (Monsignor) on Dec 04, 2009 at 18:17 UTC

    If you don't do a use Debug in your module, how can you ensure that dp is available to your module? In other words, if this module is used by a different program, will this (hidden) prerequisite cause the next person to use your module to use your name in an unseemly context?

    I figure with the saved keystrokes I might make up the development time by the end my programming life.

    .... and replace it with the search time required to look for where the dp function is defined, or if you have the right one.

    --MidLifeXis