Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: Module Inheritance

by broquaint (Abbot)
on Apr 29, 2004 at 14:05 UTC ( [id://349132]=note: print w/replies, xml ) Need Help??


in reply to Re: Module Inheritance
in thread Module Inheritance

It's not. It's not true that you should use require to prevent import to be called. It's just one way of doing so.
I said 'should' as it implied advice, as I was also aware of your use approach. And (to me) it's nothing to do with preventing calling import. I'm of the opinion require is appropriate because we just want to bring in Exporter's code, we're not concerned with its import method and whether it's called or not at that point, and generally the distinction between run-time and compile-time is irrelevant when one is using Exporter (since it'll generally be wrapped in an implicit BEGIN block when the module is useed) so nothing is gained by use (or lost by it, as I said, c'est la vie).
To me it smells like cargo-cult.
Perhaps, but seeing as how I know what I'm doing when I say require Exporter and why I'm choosing not to use Exporter (), that kinda stops cargo-cult smell right about there.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re(3+): Module Inheritance
by bart (Canon) on Apr 29, 2004 at 15:00 UTC
    For those of you who are confused by all this (and who wouldn't be): A used module's "runtime" is still the main program's "compile time". At the time that the main program invokes import, the module's top level code will already have been run.

    Try this for a demo. Save it into 3 files, as indicated:

    # Module Foo2, file Foo2.pm package Foo2; warn "module Foo2's run time"; BEGIN { warn "module Foo2's compile time"; } use Foo1; BEGIN { warn "module Foo2's compile time"; } sub import { warn "$_[0]\->import() called from " . caller; } warn "module Foo2's run time"; 1; # Module Foo1, file Foo1.pm package Foo1; warn "module Foo1's run time"; BEGIN { warn "module Foo1's compile time"; } sub import { warn "$_[0]\->import() called from " . caller; } warn "module Foo1's run time"; 1; # main script warn "Main script, runtime"; BEGIN { warn "Main script compile time"; } use Foo2; warn "Main script, runtime"; BEGIN { warn "Main script compile time"; }
    Result:
    Main script compile time at test.pl line 4.
    module Foo2's compile time at Foo2.pm line 4.
    module Foo1's compile time at Foo1.pm line 4.
    module Foo1's run time at Foo1.pm line 3.
    module Foo1's run time at Foo1.pm line 8.
    Foo1->import() called from Foo2 at Foo1.pm line 6.
    module Foo2's compile time at Foo2.pm line 6.
    module Foo2's run time at Foo2.pm line 3.
    module Foo2's run time at Foo2.pm line 10.
    Foo2->import() called from main at Foo2.pm line 8.
    Main script compile time at test.pl line 7.
    Main script, runtime at test.pl line 3.
    Main script, runtime at test.pl line 6.
    

    As you can see, the "compile time" and "run time" messages are clearly intertwined. But one thing is for sure: import is called after the runtime for a module is completed, after the second warning for "runtime", at the end of the script, lines 8 and 10 respectively.

    Conclusing: use Exporter(); or require Exporter;, it doesn't actually matter.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found