Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: Correct syntax for using $INC to keep modules in same file

by perl-diddler (Hermit)
on Oct 13, 2012 at 22:36 UTC ( #998880=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Correct syntax for using $INC to keep modules in same file
in thread can't import using exporter

Reading through old stuff...(I sometimes do)...

I do not understand what you are trying to say.

use mem, which for simple assignments, I've found can be more easily simplified to:

use mem ($::INC{'Dbg.pm'}=1);
is a substitute for the UGLY BEGIN block. If there is a more structured way to do it that is prettier, please tell me, but I find BEGIN blocks to be ugly -- I don't like all caps keywords -- they are hard to type and bother my RSI when I do it. So I tend to find lower cost workarounds.

But the bit you said about "use mem 'Dbg'" -- I don't understand -- are you thinking or saying that such would be a substitute for the previously mentioned code or that I should write a separate module to make it so? If it is the latter, yeah, I could, but its a tradeoff. Any module that doesn't have mem in it's library path can recreated it in 1 line:

BEGIN{$::INC{mem.pm}=1}
After that, I can use mem and anything on it's parameter line will be evaluated at BEGIN time without all those ugly BEGIN blocks.

The most common usage I have for use mem (besides assigning to INC so a module in the same file doesn't have to be loaded), is to assign to ISA and EXPORT at BEGIN time, so various modules, including Exporter will work correctly.

Exporter won't work fully if you don't do that. It will include the functions you want -- but not their types. I try to always use typed functions. You only get the benefit of that typing if you have your EXPORT list included at BEGIN time.... usemem does that.

So I'm not sure what you are suggesting I write a module that would have to be included to provide syntactic sugar so it looks nicer? (not necessarily a bad thing, but right now utility beats pretty... pretty comes after it's working -- and I do go back and pretty up code...but usually on a 2nd revision...1st revision is to get it working)...

Is that wrong, or what are you suggesting?


Comment on Re^4: Correct syntax for using $INC to keep modules in same file
Select or Download Code
Re^5: Correct syntax for using $INC to keep modules in same file
by chromatic (Archbishop) on Oct 14, 2012 at 04:13 UTC

    Something like this:

    package mem; sub import { my (undef, $package) = @_; $package =~ s!::!/!g; $INC{$package . '.pm'} = 1; }

    As long as that appears logically before any use mem 'foo'; statements in your program, everything should work correctly. It's slightly more typing than your implementation of mem, but it keeps all of the details of the implementation in one place.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-08-20 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (110 votes), past polls