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

Possible redundant use of "use strict" and "use warnings"?

by Special_K (Monk)
on Mar 09, 2020 at 18:08 UTC ( [id://11114026]=perlquestion: print w/replies, xml ) Need Help??

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

I am reviewing code which has the following at the top of the script:

#!/tool/bin/perl use strict; use warnings;

Then inside each subroutine it includes the same:

sub sub1 { use strict; use warnings; ... } sub sub2 { use strict; use warnings; ... } sub subN { use strict; use warnings; ... }

Is there any purpose to this seemingly redundant use of "use strict" and "use warnings" or can these statements be removed from each individual subroutine if I keep them at the top of the script?

Replies are listed 'Best First'.
Re: Possible redundant use of "use strict" and "use warnings"?
by haukex (Archbishop) on Mar 09, 2020 at 18:11 UTC

    The scope of those pragmas is lexical, meaning that if you turn them on once at the top of the file, they'll be enabled for the entire file (unless turned off with no), so they don't need to be present in each sub.

    I've seen people do this with the rationale that the subs can be self-contained when copy/pasted into other code, which is of course not a preferable way to share code in the first place.

Re: Possible redundant use of "use strict" and "use warnings"?
by Your Mother (Archbishop) on Mar 09, 2020 at 18:53 UTC

    FWIW, I put them into subs sometimes in the legacy code I work on because the top level of the code does not use them, which is bonkers, so it’s the only way to introduce sanity with new code or in reviewable/discreet chunks.

Re: Possible redundant use of "use strict" and "use warnings"?
by LanX (Saint) on Mar 09, 2020 at 19:06 UTC
    In case someone wants to remove or weaken the top level strictures, this wouldn't effect the subs.

    (Like globally disabling warn on undefined and so on)

    But frankly I doubt that's the case here, looks more like cargo cult to me.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Possible redundant use of "use strict" and "use warnings"?
by Perlbotics (Archbishop) on Mar 09, 2020 at 18:14 UTC

    You can remove them. Perl remembers the modules already loaded (%INC) and does not load them again.
    Take care to consolidate arguments if the modules use some (not the case here).

      Perl remembers the modules already loaded (%INC) and does not load them again.

      Yes, but to be nitpicky that's a feature of require - use is equivalent to BEGIN { require Module; Module->import( LIST ); }, meaning that multiple use statements will still cause the packages' ->import method to be called multiple times, which may make a difference if import does some nonstandard things.

Re: Possible redundant use of "use strict" and "use warnings"?
by Anonymous Monk on Mar 10, 2020 at 02:34 UTC
    Just clean them out of the subs, after verifying that there are no "no" directives anywhere.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found