http://www.perlmonks.org?node_id=141723

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

Here's one that caused me to sit back and go, "Hmmmm...".

Reading through some well-distributed code I found this near the top of the main script:

use lib qw(./include); use Cwd; # avoid needing to refer to SnortSnarf packages as SnortSnarf::*, even + if # that is where they really are: sub BEGIN { push(@INC,map("$_/SnortSnarf",grep(-d "$_/SnortSnarf",@INC +))); } use SnortFileInput; use HTMLMemStorage; use HTMLAnomMemStorage; use SnortRules; etc. etc.
Now, although I have never seen the little meme in that BEGIN block before, I understand what it is doing. (That's not the question.) My question is this: "is this a Good Practice?" What is the point?

So far as I can see, this bit of source-hiding simply saves a few keystrokes near the top of the file. And if done to access it could lead to some maintainance annoyances. Imagine adding several module paths to @INC and then trying to find the modules later: "Let's see, I wonder if the module referred to in 'use Parser' is XML::Parser or HTML::Parser..."

It's cute. It's slick. But is it Good?

David

Update: Thanks to the responders below for confirming my misgivings. I'm open to learn new approaches, but this one felt wrong from the get-go.

Replies are listed 'Best First'.
Re: Hiding the Module 'Path'
by wog (Curate) on Jan 26, 2002 at 08:12 UTC
    I would say this is not good. Not only is there the namespace conflicts issue you mentioned, but there is also an issue of how use works. use MODULE; is equivilent to BEGIN { require MODULE; MODULE->import(); }. This shortcut makes it so use will not be able to run the import method on the module in question, which can greatly affect how a module works. (I am, of course, assuming that these modules being "short-cutted" are really define packages whose name begins with SnortSnarf:: or whatever.)
Re: Hiding the Module 'Path'
by n3dst4 (Scribe) on Jan 26, 2002 at 17:00 UTC
    Surely it's more arse-ache to write that BEGIN block than to add SnortSnarf:: to the use's?

    It certainly is cute and slick, though. In that respect, it's much like a baby seal covered in crude oil.

(jptxs)Re: Hiding the Module 'Path'
by jptxs (Curate) on Jan 26, 2002 at 22:31 UTC
    strikes me that this isn't even cute and slick. what is the reason for hiding this? I have to assume someone using this would be aware of the requirement for these modules, or, at least, that there is a directory structure that this tar file they've unpacked has created. If they have been curious and competent enough to take on reading or modifing the source, why would we assume the need to hide these paths from them? Sure it's a nice trick, but a nice trick for no reason can't be a good idea...

    We speak the way we breathe. --Fugazi

Re: Hiding the Module 'Path'
by Aristotle (Chancellor) on Jan 27, 2002 at 01:04 UTC

    I have to agree with wog and jptxs. I'm speculating that whoever did this was not aware of any but the most obvious implications. That's hair-raisingly false laziness. Conventions form over time for good reason. Challenging them may lead to better solutions, but one shouldn't do so mindlessly. I often find myself enlightened and sticking to a convention after questioning it even if I thought it really stupid before.

    Finally, I think the fact that everyone here disagrees with that code and even you yourself have doubts is a pretty clear indication of its worth.

    Makeshifts last the longest.