Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Correct syntax for using $INC to keep modules in same file

by perl-diddler (Chaplain)
on Jun 25, 2012 at 01:00 UTC ( #978097=note: print w/replies, xml ) Need Help??

in reply to Re^2: can't import using exporter
in thread can't import using exporter

Actually, it needs to be
package FOO::BAR; BEGIN {::$INC{FOO/}=1};
You can't use __PACKAGE__ because it would be FOO::BAR -- not a pathname, and there is no file, so '1' (true) is fine.

looks all better if you turn it into a 'use mem' statement' by adding a dummy package 'mem' first:

{package mem; [#1] BEGIN{$::INC{''}=1} #1: '#' present if in separate file 1}
After that's defined, then you can simply:
{ package Dbg; use mem &{sub(){$::INC{''}=1}}; use warnings; use strict; our @EXPORT; our $FRE; use mem &{sub(){$FRE = qr{:([^:]+)$} } }; use mem &{sub(){our @EXPORT = qw(Tracing Dumping Trackback DDump TPe +) } };
Note the '@EXPORT line -- this is required to make EXPORT work in 5.14 as one would expect. I don't remember this being the case at some previous point in time. But this is what was wrong with the original program. @EXPORTS in the original and 'Exporter' (maybe Exporter was changed), don't work at 'BEGIN/start' time, so if you use strict; nothing you export will be considered "legal"...

my 'use mem' module (all 2-3 lines of it!), is short for memorize this NOW for use in following code, or for the INC statements, use the memory cache for this routine and don't go to disk unnecessarily.

FWIW -- the original prog mentioned here, now works (though it looks quite different).

The single ~1600 line program has 17 classes and included 3 packages that can easily be re-integrated to run as 1 prog/file.

Replies are listed 'Best First'.
Re: Correct syntax for using $INC to keep modules in same file
by Anonymous Monk on Jun 26, 2012 at 05:16 UTC


    Correcting tobyink seems redundant after corion/chromatic showed you, and after tobyink corrected himself

    Heck, it even seems trollish

      Corion and chromatic showed me what?

      You are partly right about tobyink -- but even with the spelling corrected the module he suggested didn't compile on 5.14 on my system. Chromatic's showed me that he could tell me to split my 1 program into 20-30 separate files -- which would be pretty clear -- wasn't something I was about to do -- I want to use small classes, some maybe 5-7 lines long to define typed data, I don't want to separate each class definition out into a a separate file. It wouldn't work for me.

      Only when a module is *stable* -- and not being developed any more, and is called for in multiple programs, is it time, IMO, to split it out into it's own module.

      Otherwise it adds unnecessary complexity.. Corion went on about spelling errors long after they were corrected and still didn't work. His example using BEGIN throws away much compile time checking and introduces duplicate declarations that *DO* get out of sync when they are done -- (I've seen others complain about it on the perl todo list, and I've had the experience myself).

      So an anonymous twit intimating posting offtopic aspersions seems alot closer fit to a troll than anything I wrote. Nerdbutt! ;-)

        I don't want to separate each class definition out into a a separate file.

        If you were truly using classes, you wouldn't have this problem. You're not using classes. You're reinventing modules, and that badly.

        Otherwise it adds unnecessary complexity.
        use mem &{sub(){$::INC{''}=1}};

        That's necessary complexity? Reimplementing something Perl already knows how to do for you—in an ugly and incomplete fashion—is both necessary and sufficiently simple?

        Even the syntax use mem 'Dbg'; would be far less blepharitic—no less obnoxious, but at least it would keep the ugliness to only one spot in your code.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2018-06-22 10:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.