in reply to Re^2: Detecting an imported function (exclude time)
in thread Detecting an imported function

Since BEGIN-phase processing is when subs get defined, and the processing goes top-to-bottom, put your BEGIN block after the subs.
use warnings; use strict; BEGIN { print "Before: $_\n" for grep /f/, keys %{main::}; } sub foo { print "I am defined\n"; } BEGIN { print "After: $_\n" for grep /f/, keys %{main::}; }

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^4: Detecting an imported function (exclude time)
by Ovid (Cardinal) on Nov 17, 2005 at 17:35 UTC

    Programmers traditionally put their "use" statements near the top of a package. If they do that and put the BEGIN near the bottom, I'm back where I started. Having an arbitrary convention to handle this problem breaks as soon as someone forgets/ignores the convention.

    package Trait::Foo; use strict; use warnings; use Some::Other::Module::Which::Exports::Stuff; sub foo { ... } sub bar { ... } BEGIN { # S:O:M:W:E:S's exported stuff will get picked up # here. That's bad. }

    Otherwise, you're saying subs first, then BEGIN blocks and then other "use" statements. That basically means telling trait authors to write their traits upside down.

    Cheers,
    Ovid

    New address of my CGI Course.