Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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]
[Corion]: Heh. Highly confusing. I use /qr in my web app as an entry point to generate QR code images. So in my code I now have '/qr/:name/:key' - "But why would I single-quote a malformed regular expression?!" ;-)
[Corion]: Also, I need to decide on whether to do the user management in SQLite or do it in a JSON structure instead...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-03-19 12:40 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.