Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Perl module problem

by djantzen (Priest)
on Dec 21, 2003 at 20:31 UTC ( #316236=note: print w/replies, xml ) Need Help??


in reply to Perl module problem

In addition to the errors and debugging tips the others have pointed out, you should know that use is equivalent to BEGIN { require 'Module X' } which means that no matter where your use statement occurs, it will be executed during compilation of your module. It belongs therefore at the top of the file, and not inside of this subroutine.

Also, how do you mean to call these subroutines? Like MyPackage::MySub or MyPackage->MySub? If it's the latter then you'll need to fetch the package name from @_ and you should just drop the prototypes since they'll be ignored.


"The dead do not recognize context" -- Kai, Lexx

Replies are listed 'Best First'.
Re: Re: Perl module problem
by ysth (Canon) on Dec 21, 2003 at 21:55 UTC
    I frequently prefer to put the use for a module that is used only in one sub in the body of the sub. The only problem I could see with this approach is if anything is imported; in that case I would agree that it belongs at the top, since it affects the whole package.

      There aren't any negative consequences codewise from doing it, even if you are importing stuff. For example, occasionally when debugging I'll throw use Data::Dumper; print Dumper $foo in the middle of some code. Dumper is an imported subroutine in this usage and works just fine so long as it's used in the same package it's imported into.

      The problem is not with writing functional code but with writing readable, maintainable code. Novice programmers, probably like the AnonyMonk here, might read such code and think mistakenly that use works at runtime like require or eval "use Foo";. Moreover, someone maintaining your code, even if an experienced Perl programmer, isn't generally going to scan your codebase looking for embedded use statements because it's customary to place them at the top of the file, and that could lead to confusion, for example in case of conflicts with imported symbols. In short, yeah you can get away with it, but it introduces a maintenance risk.


      "The dead do not recognize context" -- Kai, Lexx

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2020-06-07 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (42 votes). Check out past polls.

    Notices?