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

Re: How to export a package sitting lower in a module?

by kennethk (Monsignor)
on Apr 22, 2013 at 14:59 UTC ( #1029900=note: print w/ replies, xml ) Need Help??


in reply to How to export a package sitting lower in a module?

use is an execution of require and import wrapped in a BEGIN block. Because you have package pack_B in the same file as package pack_A, you are failing the require. So the solution is to add the export text (as Corion says) and then invoke import directly, i.e. pack_B->import.

The better way (IMHO) to do this is to actually respect the package system intent and put pack_B in a different file, and don't surprise your maintainers.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: How to export a package sitting lower in a module?
Select or Download Code
Re^2: How to export a package sitting lower in a module?
by HelenCr (Monk) on Apr 22, 2013 at 15:26 UTC
    Corion and kennethk:

    It works best if you put separate packages into separate files.

    Sometimes (especially if you have an object-oriented project, with many little packages), it's not convenient to have each package in a speparate file.

    So the solution is to add the export text (as Corion says) and then invoke import directly, i.e. pack_B->import.

    Please note that this still doesn't work: the .pm module fails, complaining that the second @ISA is a bareword.

    Main script:

    use pack_A; pack_B->import; third();
    The packages:
    # Name of this file: pack_A.pm package pack_A; use strict; require Exporter; @ISA = qw {Exporter}; @EXPORT = qw {first second}; sub first { } sub second { } 1; #end package pack_A; package pack_B; use strict; require Exporter; @ISA = qw {Exporter}; @EXPORT = qw {third fourth}; sub third { } sub fourth { } 1; #end package pack_B; #end of file pack_A.pm

      Sometimes (especially if you have an object-oriented project, with many little packages), it's not convenient to have each package in a speparate file.
      If you are doing OO programming in Perl, you don't need to import anything. Using packages as libraries and exporting subroutines into a user space is actually a direct contradiction with a rigid OO model.
      Please note that this still doesn't work: the .pm module fails, complaining that the second @ISA is a bareword.
      That's because you're violating strict. Try our:
      package pack_B; use strict; require Exporter; our @ISA = qw {Exporter}; our @EXPORT = qw {third fourth}; sub third { } sub fourth { } 1;

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Thank you, kennethk. That works (provided that you use "our @ISA" and "our @EXPORT" both in the first (pack_A) and the second (pack_B) packages).

        Many thanks - Helen

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-07-28 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (196 votes), past polls