When to use "use strict;"?

Discussion in 'Perl Misc' started by Peng Yu, Jan 25, 2010.

  1. Peng Yu

    Peng Yu Guest

    I was told to always use "use strict;" in another thread in this
    newsgroup. However, I checked Programming Perl. It seems that "use
    strict" is not necessary for small program.

    In my case, each my perl script is less than 100 lines and would be
    reused anywhere else. Do I still need to add "use strict" to my perl
    program?
     
    Peng Yu, Jan 25, 2010
    #1
    1. Advertising

  2. Peng Yu <> wrote:
    >I was told to always use "use strict;" in another thread in this
    >newsgroup. However, I checked Programming Perl. It seems that "use
    >strict" is not necessary for small program.


    Please define "necessary". The Perl interpreter doesn't care, in so
    fartechnically strict is never necessary. Is it a pretty stupid idea not
    to use it? You bet!

    >In my case, each my perl script is less than 100 lines and would be
    >reused anywhere else. Do I still need to add "use strict" to my perl
    >program?


    Will you be send to jail if you don't? No.
    Will you be criticized when posting snippets of that code and asking for
    evaluation? Very likely yes.
    Will strict() help you catch and identify stupid programming errors like
    typos in variable names? Very likely yes.

    Ultimately it is up to you if you want to use the assistance that is
    available to you or if you are a hardcore I-don't-need-no-f****-help guy
    and prefer to make your coding more difficult than necessary.

    Most people prefer to leverage all the help they can get and make their
    life easier.

    jue
     
    Jürgen Exner, Jan 25, 2010
    #2
    1. Advertising

  3. Peng Yu <> wrote:
    > I was told to always use "use strict;" in another thread in this
    > newsgroup. However, I checked Programming Perl. It seems that "use
    > strict" is not necessary for small program.
    >
    > In my case, each my perl script is less than 100 lines and would be
    > reused anywhere else. Do I still need to add "use strict" to my perl
    > program?


    "use strict;" helps to detect a lot of "typing mistakes".
    You do not need it if you never do such mistakes :)

    P.S.
    There are some "tricks" disabled by "use strict;".
    Do you use them? No => use "use strict;"

    --
    [pl>en Andrew] Andrzej Adam Filip : :
    "Rembrandt's first name was Beauregard, which is why he never used it."
    -- Dave Barry
     
    Andrzej Adam Filip, Jan 25, 2010
    #3
  4. Peng Yu

    Brad Baxter Guest

    On 1/25/2010 6:34 PM, Peng Yu wrote:
    > I was told to always use "use strict;" in another thread in this
    > newsgroup. However, I checked Programming Perl. It seems that "use
    > strict" is not necessary for small program.
    >
    > In my case, each my perl script is less than 100 lines and would be
    > reused anywhere else. Do I still need to add "use strict" to my perl
    > program?


    It doesn't hurt. Always use strict (I do). Well, except
    for one-liners (most of the time).

    perl -Mstrict -lwe'print "Just another Perl Hacker"'

    --
    Brad
     
    Brad Baxter, Jan 26, 2010
    #4
  5. Peng Yu

    sreservoir Guest

    On 1/25/2010 6:52 PM, Andrzej Adam Filip wrote:
    > Peng Yu<> wrote:
    >> I was told to always use "use strict;" in another thread in this
    >> newsgroup. However, I checked Programming Perl. It seems that "use
    >> strict" is not necessary for small program.
    >>
    >> In my case, each my perl script is less than 100 lines and would be
    >> reused anywhere else. Do I still need to add "use strict" to my perl
    >> program?

    >
    > "use strict;" helps to detect a lot of "typing mistakes".
    > You do not need it if you never do such mistakes :)
    >
    > P.S.
    > There are some "tricks" disabled by "use strict;".
    > Do you use them? No => use "use strict;"


    { no strict qw(what you want); tricks }

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
     
    sreservoir, Jan 26, 2010
    #5
  6. On Tue, 26 Jan 2010 00:34:46 +0100, Peng Yu <> wrote:

    > Do I still need to add "use strict" to my perl program?


    Yes.
     
    Jochen Lehmeier, Jan 26, 2010
    #6
  7. Peng Yu wrote:
    > I was told to always use "use strict;" in another thread in this
    > newsgroup. However, I checked Programming Perl. It seems that "use
    > strict" is not necessary for small program.
    >
    > In my case, each my perl script is less than 100 lines and would be
    > reused anywhere else. Do I still need to add "use strict" to my perl
    > program?


    I always use strict (except in one-liners).

    --
    RGB
     
    RedGrittyBrick, Jan 26, 2010
    #7
  8. >>>>> "PY" == Peng Yu <> writes:

    PY> I was told to always use "use strict;" in another thread in this
    PY> newsgroup. However, I checked Programming Perl. It seems that
    PY> "use strict" is not necessary for small program.

    If you need to ask, you aren't experienced enough to know when you don't
    need it. And if you are experienced enough to know when you don't need
    it, you've also been bitten by stupid bugs often enough to always use it
    anyway.


    Charlton


    --
    Charlton Wilbur
     
    Charlton Wilbur, Jan 26, 2010
    #8
  9. Peng Yu

    sreservoir Guest

    On 1/26/2010 11:23 AM, Charlton Wilbur wrote:
    >>>>>> "PY" == Peng Yu<> writes:

    >
    > PY> I was told to always use "use strict;" in another thread in this
    > PY> newsgroup. However, I checked Programming Perl. It seems that
    > PY> "use strict" is not necessary for small program.
    >
    > If you need to ask, you aren't experienced enough to know when you don't
    > need it. And if you are experienced enough to know when you don't need
    > it, you've also been bitten by stupid bugs often enough to always use it
    > anyway.


    or need to not have it. but that's what lexical pragmata are for.

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
     
    sreservoir, Jan 26, 2010
    #9
  10. On 26 Jan 2010 Charlton Wilbur <> wrote in
    news::

    >>>>>> "PY" == Peng Yu <> writes:

    >
    > PY> I was told to always use "use strict;" in another thread
    > in this PY> newsgroup. However, I checked Programming Perl. It
    > seems that PY> "use strict" is not necessary for small
    > program.
    >
    > If you need to ask, you aren't experienced enough to know when you
    > don't need it. And if you are experienced enough to know when you
    > don't need it, you've also been bitten by stupid bugs often enough
    > to always use it anyway.


    I would agree. Does anyone know why so much of the code available from
    perldoc (the FAQ and the module docs) is not written so that it can be
    used under strict?

    I've lost count of the number of times I've copied something in from
    the manual, run it and then cursed when strict throws it out.

    --
    Graham Drabble
    http://www.drabble.me.uk/
     
    Graham Drabble, Jan 27, 2010
    #10
  11. On Wed, 27 Jan 2010 17:59:30 +0100, Graham Drabble
    <> wrote:

    > I've lost count of the number of times I've copied something in from
    > the manual, run it and then cursed when strict throws it out.


    Do you have examples of what you mean?
     
    Jochen Lehmeier, Jan 27, 2010
    #11
  12. Peng Yu

    Guest

    > On Wed, 27 Jan 2010 17:59:30 +0100, Graham Drabble wrote:
    > > I've lost count of the number of times I've copied something in from
    > > the manual, run it and then cursed when strict throws it out.


    On Jan 27, 10:30 am, "Jochen Lehmeier" <>
    wrote:
    > Do you have examples of what you mean?



    "perldoc Getopt::Long" shows this example:

    use Getopt::Long;
    my $data = "file.dat";
    my $length = 24;
    my $verbose;
    $result = GetOptions ("length=i" => \$length, # numeric
    "file=s" => \$data, # string
    "verbose" => \$verbose); # flag

    Notice that $data, $length, and $verbose are declared with "my", but
    $result is not. As a result, if you copy-and-paste this code into a
    program that has "use strict;" you'll probably see an error that
    $result was not declared.

    (Of course, you could argue that $result was declared earlier in
    the program, but then you could make that argument for $data, $length,
    and $verbose as well.)

    I will usually put the "use strict;" and "use warnings;" lines
    right into the SYNOPSIS section of the perldocs I write, if only to
    instill a sense of "this module works best with 'strict' and
    'warnings'" into users who are too inexperienced to know better. To
    be honest, I'm not sure if that's a good habit or not (including them
    in the perldocs, that is), but I figure if it gets a user to start
    using 'strict' and 'warnings' (where they otherwise wouldn't), then
    the positive of doing that outweighs the negative.

    -- Jean-Luc
     
    , Jan 27, 2010
    #12
  13. On Wed, 27 Jan 2010 20:20:29 +0100,
    <> wrote:

    > Notice that $data, $length, and $verbose are declared with "my", but
    > $result is not.


    > (Of course, you could argue that $result was declared earlier in
    > the program, but then you could make that argument for $data, $length,
    > and $verbose as well.)


    I would say that this kind of example is just that - an example. Or a
    specification. It shows how to use the module, but whether some variable
    used in the example has a "my" in front of it does not really matter. You
    would certainly need to edit the code anyway, since your code will MOST
    probably not need a "length", "file", "verbose" parameter. ;-)

    > I will usually put the "use strict;" and "use warnings;" lines
    > right into the SYNOPSIS section of the perldocs I write,


    I only include the barest necessary code as a "wrapper" around whatever I
    want to show. I assume that the reader of some module documentation knows
    about strict, warnings, my etc. OTOH, I have no public modules, either,
    and know the people who use mine.

    My minimum form of POD is usually like this:

    =head2 someFunc

    ($xyz,$abc) = MyModule::someFunc($def,$ghi);

    Blablabla ...

    =cut

    This outputs the "signature" of the function prominently at the top, i.e.,
    it tells the user what arguments I expect and what I return (of course, if
    the sub is a difficult one, more examples may be added). But whether or
    not "my" is in there does not really matter at all. The user is free to
    cut&paste that "signature" to avoid some little amount of typing, but he
    will usually already have other variable names in place, so the usefulness
    of "cut&paste-ready" code seems limited, to me.

    > be honest, I'm not sure if that's a good habit or not (including them
    > in the perldocs, that is), but I figure if it gets a user to start
    > using 'strict' and 'warnings' (where they otherwise wouldn't), then
    > the positive of doing that outweighs the negative.


    Yup, I guess it's philosophical. But I would very seldomly cut&paste some
    code from the documentation with the intent of using it without any
    further editing - it would in 99% of the cases simply not match my needs
    (one notable exception is "perldoc -f localtime" - I frequently cut&paste
    that beautiful line which includes the return values ;-) ).
     
    Jochen Lehmeier, Jan 27, 2010
    #13
  14. "" <> wrote:
    [Use of strict() in perldoc/perlfaq]

    >[...]To
    >be honest, I'm not sure if that's a good habit or not (including them
    >in the perldocs, that is), but I figure if it gets a user to start
    >using 'strict' and 'warnings' (where they otherwise wouldn't), then
    >the positive of doing that outweighs the negative.


    I second your doubts. An explanation for how a specific feature works or
    the algorithmic idea for how to solve a specific problem is not the
    right place to also introduce (or even just re-enforce) a concept that
    has nothing to do with that feature or that algorithm.
    Those explanations and code samples should be reduced to the bare
    minimum necessary for the issue at hand. Otherwise you are creating a
    diversion and confusion: why did the author do this, it appears as if it
    has nothing to do with my problem? What am I missing?

    jue
     
    Jürgen Exner, Jan 27, 2010
    #14
  15. Peng Yu

    John Bokma Guest

    Ben Morrow <> writes:

    > Quoth "" <>:
    >>
    >> Notice that $data, $length, and $verbose are declared with "my", but
    >> $result is not. As a result, if you copy-and-paste this code into a
    >> program that has "use strict;" you'll probably see an error that
    >> $result was not declared.

    >
    > Cut-and-paste (or rather, copy-and-paste) is *always* a mistake when
    > programming; even if its from examples. You should *read* the example,
    > *understand* it, and then write your own code using that
    > understanding.


    What an extremistic view point, sigh. I agree that you must understand
    the example, but what if the example does *exactly* what you want,
    shouldn't you just copy it or does one really have to type it over to be
    a real programmer? Oh, my, I just copied an example today. Thankfully it
    was Python, and not Perl.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
     
    John Bokma, Jan 27, 2010
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H. Wade Minter
    Replies:
    8
    Views:
    310
    Robin
    Apr 25, 2004
  2. Martina

    why is important to use : use strict?

    Martina, Sep 19, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    107
    Jürgen Exner
    Sep 20, 2005
  3. Ting Wang
    Replies:
    5
    Views:
    189
    John Bokma
    Oct 6, 2005
  4. Marius Gavrilescu

    Re: use strict; use warnings;

    Marius Gavrilescu, Feb 24, 2014, in forum: Perl Misc
    Replies:
    152
    Views:
    565
    $Bill
    Mar 11, 2014
  5. Kaz Kylheku

    Re: use strict; use warnings;

    Kaz Kylheku, Feb 24, 2014, in forum: Perl Misc
    Replies:
    0
    Views:
    73
    Kaz Kylheku
    Feb 24, 2014
Loading...

Share This Page