Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: can't import using exporter

by tobyink (Canon)
on Mar 12, 2012 at 09:31 UTC ( [id://959097]=note: print w/replies, xml ) Need Help??


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

But "use" requires the package to be defined in an appropriately named separate file. If the Debug package is defined in the same file as Transcode_plug, then it needs to be import.

It might be a good idea to do the import in a BEGIN block though, to ensure it happens at the same stage that use would have done it.

The other alternative (as discussed in another thread recently) would be for the Debug package to include the following line:

BEGIN { $INC{+__PACKAGE__}=__FILE__ }

This will allow Transcode_plug to use Debug even though Debug isn't defined in a separate file.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Correct syntax for using $INC to keep modules in same file
by perl-diddler (Chaplain) on Jun 25, 2012 at 01:00 UTC
    Actually, it needs to be
    package FOO::BAR; BEGIN {::$INC{FOO/BAR.pm}=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{'mem.pm'}=1} #1: '#' present if in separate file 1}
    After that's defined, then you can simply:
    { package Dbg; use mem &{sub(){$::INC{'Dbg.pm'}=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.

      ....

      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! ;-)

Re^3: can't import using exporter
by perl-diddler (Chaplain) on Mar 12, 2012 at 17:52 UTC
    I just tried that, but now get:
    Can't locate Debug.pm in @INC (@INC contains: /usr/lib/perl5/site_perl +/5.14.2/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.14.2 /us +r/lib/perl5/vendor_perl/5.14.2/x86_64-linux-thread-multi /usr/lib/per +l5/vendor_perl/5.14.2 /usr/lib/perl5/5.14.2/x86_64-linux-thread-multi + /usr/lib/perl5/5.14.2 /usr/lib/perl5/site_perl/5.14.2/x86_64-linux-t +hread-multi /usr/lib/perl5/site_perl/5.14.2 /usr/lib/perl5/site_perl +.) at /Audio/scripts/cnvWav2Flac line 107. Transcode_plug::BEGIN() called at /Audio/scripts/cnvWav2Flac l +ine 107 eval {...} called at /Audio/scripts/cnvWav2Flac line 107 BEGIN failed--compilation aborted at /Audio/scripts/cnvWav2Flac line 1 +07.
    Here, BTW, is the complete prog. Tried to post it earlier as a response, but it wouldn't let me create a response -- only add to the original post...

      Sorry, what you actually want it something like:

      BEGIN { $INC{'Debug.pm'}=__FILE__ }

      Or, if you've got Module::Runtime, then:

      BEGIN { $INC{module_notional_filename(__PACKAGE__)}=__FILE__ }

       

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        Well first I got
        > cnvWav2Flac Undefined subroutine &Debug::module_notational_filename called at /Aud +io/scripts/cnvWav2Flac line 85. Debug::BEGIN() called at /Audio/scripts/cnvWav2Flac line 85 eval {...} called at /Audio/scripts/cnvWav2Flac line 85 BEGIN failed--compilation aborted at /Audio/scripts/cnvWav2Flac line 8 +5.
        Then reading manpage, it appeared, I needed to specifically include it... however, in doing so:
        > cnvWav2Flac "module_notational_filename" is not exported by the Module::Runtime mo +dule Can't continue after import errors at /usr/lib/perl5/site_perl/5.14.2/ +Module/Runtime.pm line 154. Module::Runtime::import('Module::Runtime', 'module_notational_ +filename') called at /Audio/scripts/cnvWav2Flac line 5 main::BEGIN() called at /Audio/scripts/cnvWav2Flac line 5 eval {...} called at /Audio/scripts/cnvWav2Flac line 5 BEGIN failed--compilation aborted at /Audio/scripts/cnvWav2Flac line 5 +.> cnvWav2Flac "module_notational_filename" is not exported by the Module::Runtime mo +dule Can't continue after import errors at /usr/lib/perl5/site_perl/5.14.2/ +Module/Runtime.pm line 154. Module::Runtime::import('Module::Runtime', 'module_notational_ +filename') called at /Audio/scripts/cnvWav2Flac line 5 main::BEGIN() called at /Audio/scripts/cnvWav2Flac line 5 eval {...} called at /Audio/scripts/cnvWav2Flac line 5 BEGIN failed--compilation aborted at /Audio/scripts/cnvWav2Flac line 5 +.
        I didn't have Module::Runtime, so it was just loaded and installed from CPAN. Am I doing something wrong, or is this a bug in the module meant to work around various bugs?

        Reading the manpage, several bugs that I didn't know about, but that might explain why various programs have been breaking at each upgrade as things are 'fixed'... lexical leaks and such that I didn't know about might have caused things to work earlier, but cause failure once fixed.

        Not sure -- speculation -- I have about 10 progs or so, I've encountered so far that have stopped working as I upgraded from 5.10->12->14, actually 1 of those broke from 5.8->5.9, but given the fast pace of updates, I didn't get 12 installed until 14 was out. So I have several programs that are broken now, and are on a 'to be looked at' queue, as time allows competing with current priorities....*sigh*..

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found