Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Module Style and Usage

by Smylers (Pilgrim)
on Sep 07, 2005 at 13:23 UTC ( #489862=note: print w/replies, xml ) Need Help??

in reply to Module Style and Usage

Hi. You're right that option 3 is poor from a documentation point of view — the person reading the main script and encountering the timestamp function can't see where it's defined.

But you don't have to go as far as option 2 and fully qualify the name every time. Just change the use statement to:

use MyModule qw<timestamp>;

and then it's clear exactly where that function is coming from.


Replies are listed 'Best First'.
Re^2: Module Style and Usage
by Tanktalus (Canon) on Sep 07, 2005 at 14:30 UTC

    Side note: just because you can't see where it's coming from isn't always a bad thing. It makes it easier to adjust the behaviour.

    Option 1: Say you have a MyModule with timestamp in it. Now a new corporate standard says all company-owned modules should be in the Example namespace. All you need to do is change the modules themselves (including moving them to the Example subdirectory) and the "use" statements. The rest of the code will work as-is. (This is also a reason to use __PACKAGE__ when referring to one's own package rather than hardcoding the name again and again - the other reason is to aid in cut&paste from module to module.)

    Option 2: Say you're using CPANModule's timestamp. And you find a bug. So you go write MyCPANModule, reimplementing a timestamp that works, and all you need to do is change the use statement. You don't need to go changing all the code.

    Option 3: Say you're using CPANModule's foo function. Which calls CPANModule2's timestamp - which is broken or just not working quite the way you want it to. If CPANModule is calling "timestamp" rather than "CPANModule2::timestamp", then you can just "use CPANModule" and then play with the symbol table and "import" your own timestamp function into CPANModule's namespace with whatever modifications you want, which will overwrite the import from CPANModule2 since the import from CPANModule2 will happen during compilation and yours will happen during runtime. (Technically, it could be during compilation as well, but after the compilation of CPANModule and thus also after compilation of CPANModule2.)

    Part of the idea of not naming your namespaces is to allow perl to actually Do What You Mean. Even if it's not your code ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://489862]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-06-23 21:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.