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

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.

Replies are listed 'Best First'.
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'
      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.


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

        Heck, it even seems trollish

      I just tried that, but now get:
      Can't locate 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{''}=__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'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://959073]
[LanX]: ellipse aka ... aka yadda yadda is a new command
[LanX]: sopw!
[1nickt]: Sorry kgb can't help there, not a perltidy user.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2017-11-18 18:19 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.