Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

RE: RE: Re: Module dependency

by jreades (Friar)
on Sep 07, 2000 at 18:31 UTC ( [id://31398]=note: print w/replies, xml ) Need Help??


in reply to RE: Re: Module dependency
in thread Module dependency

Funnily enough, that's exactly what I thought -- so I was going to produce a big 'ta-da' with &package::mySub()... but then I wrote and ran the program you see and it worked.

So, a quick reread of my Perl documentation suggests the following:

  • .pm files can be imported straight into the namespace of the using script if the .pm file lacks a package declaration
  • As soon as a package declaration is made in a .pm file the standard rules of EXPORTing and importing apply
  • So, if I rewrite my code as follows:
################## # MyPackage ################## package MyPackage; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(exportedSub); @EXPORT_OK = qw(exportedOkSub); $var = 'a variable'; sub exportedSub { print "Exported sub called!\n"; } sub exportedOkSub { print "ExportedOK sub called!\n"; } 1; ################# # Script.pl ################# use MyPackage qw(exportedOkSub); exportedSub(); exportedOkSub(); print "Var: " . $MyPackage::var . "\n"; exit 0;

Then it should work as advertised.

Note, however, that I have done some fudging as MyPackage isn't properly included in my @INC -- so YMMV. :^)

~

Replies are listed 'Best First'.
RE: RE: RE: Re: Module dependency
by Fastolfe (Vicar) on Sep 07, 2000 at 22:43 UTC
    Since 'use' is exactly equivalent to a 'require' and an 'import' at compile-time, not specifying a 'package' keyword in your .pm file means the file itself isn't a module/package at all, but is simply Perl code that will be read into the current name space as if you'd done a require "file.pl"; somewhere. You've just changed the extension to pm instead of pl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found