Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: can't import using exporter

by chromatic (Archbishop)
on Mar 12, 2012 at 07:05 UTC ( #959073=note: print w/ replies, xml ) Need Help??


in reply to can't import using exporter

Do you mean:

package Transcode_plug { use Debug qw{$Filename2Fields}; use Vars; ... }

Let use handle the import for you, as it'll load the package and perform the importing during compilation time. Also remember the sigil.


Improve your skills with Modern Perl: the free book.


Comment on Re: can't import using exporter
Select or Download Code
Re^2: can't import using exporter
by tobyink (Abbot) on Mar 12, 2012 at 09:31 UTC

    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'
      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'
      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2014-08-22 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (156 votes), past polls