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

Looking at code in pmd_grapher in the CPAN Module::Dependency I find the following:
use strict; use Module::Dependency::Grapher; *Module::Dependency::Grapher::TRACE = \*TRACE;
I'm puzzled by the use of globs here. Examining the code shows that only &TRACE is used, not %TRACE, $TRACE or @TRACE. I would have expected to see
use strict; use Module::Dependency::Grapher qw(&TRACE);
Is there something fancy the author is doing with globs here? Is there some practical difference?

Replies are listed 'Best First'.
Re: Importing functions into packages - globs vs use
by davorg (Chancellor) on Nov 30, 2006 at 16:42 UTC

    They're doing different things.

    The first one takes the TRACE glob from the current package and installs a reference to it into the Module::Dependency::Grapher package. This effectively pushes your TRACE subroutine (and other objects) into the Module::Dependency::Grapher package.

    The second one imports the TRACE subroutine from Module::Dependency::Grapher into the current package (assuming that Module::Dependency::Grapher is using Exporter).

    So they are the complete opposite of each other.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Ahhh! many thanks. That explains some other strange-seeming behavior that I was not figuring out myself. And sheds light on why &Module::Dependency::Grapher::TRACE was defined, but as a no-op.
        Doing the whole glob (instead of overwriting the CODE slot) prevents a 'redefined' warning, ( I think. Confirmed. )
Re: Importing functions into packages - globs vs use
by themage (Friar) on Nov 30, 2006 at 16:42 UTC
    hi throop,

    The author is doing exactly the oposite to what you are expecting.

    What that code do is redefine the sub TRACE in Module::Dependency::Grapher with the one in your local scope. After this code, every call made to TRACE inside Module::Dependency::Grapher will call your local TRACE, not the original one defined in Module::Dependency::Grapher.

    Using use Module::Dependency::Grapher qw(TRACE); you import Module::Dependency::Grapher::TRACE to your local scope, that can then be called with TRACE(...).

    TheMage
    Talking Web
Re: Importing functions into packages - globs vs use
by brian_d_foy (Abbot) on Dec 01, 2006 at 09:34 UTC

    That code is doing a little bit of black magic to replace part of a module without changing the module source. I talk about that the Dynamic Subroutines chapter of Mastering Perl (and it shows up in a few other chapters too). It can be a very handy way to fix a module and not see your changes disappear when an admin updates the module.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review