Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

where to use "use" and package

by smackdab (Pilgrim)
on Aug 01, 2002 at 03:22 UTC ( [id://186673]=perlquestion: print w/replies, xml ) Need Help??

smackdab has asked for the wisdom of the Perl Monks concerning the following question:

In my Debug.pm file, it make a difference whether I put the "use" statements before or after the package declaration...


If I put them as this, I get undefined subroutine:
use File::Copy qw(copy); use IO::File; package Mods::Debug; [...stuff...] copy($file1, $file2);
I can either move the "use" statements after the package declaration or I can explicitely call copy with File::Copy::copy(...)

But "copy()" has been @EXPORTed in the module...so I am confused ;-)

What is best? THANKS!
OFFTOPIC, but maybe a moderator is interested: If you use my title and then do a preview, the resulting title is only "where to use", the rest is chopped off

Replies are listed 'Best First'.
Re: where to use "use" and package
by elusion (Curate) on Aug 01, 2002 at 03:31 UTC
    The reason that you need to use the fully qualified name (File::Copy::copy(...)) is because by putting your use's before your package declaration, you've declared them into your main namespace.

    Any time you're in a file and haven't declared the namespace with package, the namespace defaults to main. After your package declaration, you're in the Mods::Debug namespace.

    So by putting the use's after the package declaration, you import them into the Mods::Debug namespace instead of main. This is the right way to do it.

    elusion : http://matt.diephouse.com

      Case matters. The default package is called main, not Main.

      Abigail

      Thanks, I didn't realize that each namespace would have to load in the *somewhat* "standard" modules. thanks !!!
        If the methods in a module were really standard, they wouldn't be in that module -- they'd be builtins
Re: where to use "use" and package
by BrowserUk (Patriarch) on Aug 01, 2002 at 03:45 UTC

    As I understand it, when your script is being compiled, the default package (read symbol table?) is main:: until a package statement is processed, at which point a new (default) symbol table is set up.

    As the scoping of symbols in imported (use'd) modules is defined by the current default symbol table. Anything use'd before the first package will be aliased in main::, so after the package Mods::Debug statement, you could equally refer to copy() as main::copy() or File::Copy::copy().

    The upshot: If you want to use symbols in a package, use them in the package where you want to use them:).

    Update:Corrected a couple of typoes. s/Main/main/ and s/File::copy()/File::Copy::copy()/.

Re: where to use "use" and package
by thunders (Priest) on Aug 01, 2002 at 15:23 UTC
    placing a package in the same file as your main program is not really different than defining the package in another file. Had you put the package in a separate file it would be obvious why you couldn't copy, because you never imported the copy method from File::Copy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://186673]
Approved by vek
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found