Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Location of 'use' statements

by demerphq (Chancellor)
on Apr 19, 2003 at 11:03 UTC ( [id://251629]=note: print w/replies, xml ) Need Help??


in reply to Location of 'use' statements

I work with MS Source-Safe (some may say thats unfortunate ;-) whose source contol keyword expansion often doesnt play nicely with perl unless you jump through hoops (as the keywords start with a dollar sign, ie $Log gets translated, wich aint cool if youve used it as a vairable name /grr). This means that I need to put certain source control related stuff as close to the top as possible.

Other than that my preferences are fairly similar to tye and adrianh, probably the most notable difference is that I put pragmas like strict and warnings after my modules, I do this because as they are lexically scoped they have no effect on the contents of the used modules, and to me doing it this way makes this more clear, especially to a potentially inexperienced maintenance coder.

I also usually add a special block at the end for doing module test/development so that you can say perl Module.pm and have it self test or diagnostics or just plain old dev code.

package Foo::Bar; #always goes at the top! ###################################################################### +########## # $Header:: /prject/name.pl 4 02/12/04 18:46 Author + $# ($VERSION) = sprintf "%d.%03d", 1, ' $Revision:: 4 $' =~ /::\ +s+(\S+)/;# # $NoKeywords:: + $# ###################################################################### +########## use base qw/Some::Module/; # 'cause the relationship is important use Essential::CPAN::Modules; use Essential::NONCPAN::Modules; use Non::Essential::Modules; (ie debugging and the like) # I always use strict; use warnings; use warnings::register; # and maybe some other pragmas too use constant here=>1; use vars qw($Here $Should $We $Use $Them); BEGIN { # Any module specific initialization type stuff that must happen firs +t. # if vars qw() have been used and they need defaults they get them he +re. $Here||="A default value"; } sub routines_get_defined_here { } unless (caller) { # module test/development code goes here. This is useful becuase you + can then # say perl module.pm # and have it self test or devtest itself. } 1; package Virtual::Utility::Namespaces::Go::Here; 1; __END__ =pod And Pod goes here =cut


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

• Update:  
Im a huge fan of warnings::register. I use it alot and try to avoid "normal" warnings, replacing them with warnings::warnif.


Replies are listed 'Best First'.
Re: Re: Location of 'use' statements
by Juerd (Abbot) on Apr 19, 2003 at 14:16 UTC

    I put pragmas like strict and warnings after my modules, I do this because as they are lexically scoped they have no effect on the contents of the used modules, and to me doing it this way makes this more clear, especially to a potentially inexperienced maintenance coder.

    use Foo::Bar quux => ${'VERSION'}; use strict;
    An extremely bad example, of course, but while playing with Exporter::Tidy, I noticed that when a use statement gets more complex, you really do want strict active there:
    package Example; use Exporter::Tidy _map => { foo => \$foo }; use strict; my $foo; # silently making $Example::foo exportable
    With strict, I would have found out my mistake of using $foo before it has been declared immediately:
    package Example; use strict; use Exporter::Tidy _map => { foo => \$foo }; my $foo; # Global symbol "$foo" requires explicit package name ...

    Clean solution: foo => \my $foo

    Juerd
    - http://juerd.nl/
    - spamcollector_perlmonks@juerd.nl (do not use).
    

Log In?
Username:
Password:

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

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

    No recent polls found