Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

use strict; before/after use warnings;

by msh210 (Monk)
on Nov 06, 2019 at 14:08 UTC ( [id://11108366]=perlquestion: print w/replies, xml ) Need Help??

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

I've seen

use warnings;
use strict;
and I've seen
use strict;
use warnings;

Why might one choose either of these? (I'm thinking performance, style, what seems more logical to the author, anything.) Why do you choose what you do?

$_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6

Replies are listed 'Best First'.
Re: use strict; before/after use warnings;
by Discipulus (Canon) on Nov 06, 2019 at 16:09 UTC
    Hello msh210,

    I see no differences. I suppose use strict; use warnings; is with me since ever. It is also less cacophonic in sentences: use strict and warnings or...

    If both are at the top of the program, again I see no differences. If you put a syntax error between them the error is the same:

    io@COMP:C>cat sw.pl use strict; 0 use warnings; io@COMP:C>perl sw.pl "use" not allowed in expression at sw.pl line 5, at end of line syntax error at sw.pl line 5, near "use warnings" Execution of sw.pl aborted due to compilation errors. io@COMP:C>cat ws.pl use warnings; 0 use strict; io@COMP:C>perl ws.pl "use" not allowed in expression at ws.pl line 5, at end of line syntax error at ws.pl line 5, near "use strict" Execution of ws.pl aborted due to compilation errors.

    Obviously strict can catch something and warnings too, but is a rare situation:

    io@COMP:C>cat sw.pl use strict; $x=1; use warnings; io@COMP:C>perl sw.pl Global symbol "$x" requires explicit package name (did you forget to d +eclare "my $x"?) at sw.pl line 2. BEGIN not safe after errors--compilation aborted at sw.pl line 3. io@COMP:C>cat ws.pl use warnings; $x=1; use strict; io@COMP:C>perl ws.pl Name "main::x" used only once: possible typo at ws.pl line 2.

    So I suppose it is a matter of taste.

    The only important thing is to use them both everytime ;)

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: use strict; before/after use warnings;
by choroba (Cardinal) on Nov 06, 2019 at 14:22 UTC
    In my boilerplate, it's warnings first. I probably had a feeling years ago that somehow the line "use warnings" couldn't break strict, but the line "use strict" could somehow warn if not written properly.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      There probably is no right answer, but I always use the opposite. I think that at one time, I considered 'strict' more important. This might be an interesting topic for the 'Voting Booth'.
      Bill

        This might be an interesting topic for the 'Voting Booth'.
        ++ I was thinking the same thing. You can submit your idea here if you want. Although since you can also have an implicit strict by selecting a version, and use the shebang options to enable warnings that's a lot of different ways to do basically the same thing.
        #!/usr/bin/perl -W # warnings enabled globally on previous line use v5.20; # implicit use strict; use strict; # No impact use warnings; # No impact

        Personally it's always strict then warnings (in that order), sometimes preceded by the version (I always have an explicit strict even when implicitly activated).

        Edit: whoops, choroba already submitted the poll idea

      Interesting. Can it, though? Edit to clarify: Was the feeling correct, though?

      $_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6
Re: use strict; before/after use warnings;
by Anonymous Monk on Nov 06, 2019 at 16:29 UTC

    I use strict before warnings. Honestly, though, I think this was originally a case of "monkey see, monkey do," and now it is habit.

    For what it is worth, strict is the older of the pragmas, going all the way back to 5.0. The warnings pragma was introduced in 5.6. Before that, you used $^W to turn on warnings. No, I'm not that old -- I got the information from the corelist script, which comes with Module::CoreList.

Re: use strict; before/after use warnings;
by harangzsolt33 (Chaplain) on Nov 07, 2019 at 04:03 UTC
    I usually write

    use strict;
    use warnings;

    for purely aesthetic reasons.

    For example, when I type a paragraph,
    I sometimes pay attention to make sure
    that lines become increasingly longer as
    the paragraph is beginning, and the lines
    become shorter as you get near the end.

    See above example ^

    When I write if-and-elsif chain, I do this:

       if (condition) { do something; }
    elsif (condition) { do something; }
    elsif (condition) { do something; }
    else              { do something; }


    It's easier to read.

    Okay, call me OCD.

      I agree. It occurred to me that if code reading is harder work than writing, then, on subconscious level, 1st line being shorter than the 2nd is perceived as gradual increase in amount of required effort, and so slightly less stress for a reader. So not only use strict; comes 1st, but, when I have to break too long line in two and there are no parts to align vertically, I'd prefer 1st line shorter and 2nd line longer, more often than not. As ridiculous as it is, when analysed logically.

Re: use strict; before/after use warnings;
by karlgoethebier (Abbot) on Nov 06, 2019 at 17:59 UTC

    From Gulliver's Travels:

    How do you break an egg? Are you a Big-Endian or a Little-Endian?

    Create a poll. Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: use strict; before/after use warnings;
by Don Coyote (Hermit) on Nov 06, 2019 at 17:30 UTC

    Choice is the only option.

Re: use strict; before/after use warnings;
by boftx (Deacon) on Nov 09, 2019 at 01:52 UTC

    And is there any reason not to simply do this?

    use strictures;
    

    Not only does "strictures" turn on warnings, but also makes several of them fatal at the same time (though not the problematic ones.) I do this in all my code for $work since I know I am on a version of Perl that supports it.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

      I do just that. But opinions differ pretty widely and somewhat strongly. So… :P

Re: use strict; before/after use warnings;
by ikegami (Patriarch) on Nov 09, 2019 at 02:00 UTC

    There's no functional difference.

    I use use strict; first because I feel that use warnings; should warn if use strict; isn't being used. :) (It doesn't, though)

Re: use strict; before/after use warnings;
by KolassaRaptor (Monk) on Nov 07, 2019 at 15:38 UTC

    I tend to put strict before warnings, but only because that's the way that I learned it.

    TIMTOWTDI.

Re: use strict; before/after use warnings;
by Anonymous Monk on Nov 07, 2019 at 04:42 UTC
    I tried everything to make it shorter:
    use strict, warnings;
    use strict && warnings;
    use for (strict, warnings); # no not really
    
    The only thing I found was a replacement for strict:
    use 5.12.0; # <- turns on strict
    use strict; # <- but same length :-(
    
      use 5.12.0; # <- turns on strict use strict; # <- but same length :-(

      Same length, but use VERSION also turns on the corresponding feature bundle, which is IMHO pretty useful on newer versions of Perl. Also, the use 5.012; and use v5.12; forms are one character shorter :-)

        I hate programs that use 5.XX. Sometimes, I can't run them in an older Perl, but the only thing that's needed from the version feature bundle is say which started to exist in 5.10. I love explicit feature lists; that's also why I created Syntax::Construct.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: use strict; before/after use warnings;
by Anonymous Monk on Nov 07, 2019 at 01:04 UTC
    Some threads simply do not deserve to be front-paged ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-26 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found