why is important to use : use strict?

Discussion in 'Perl Misc' started by Martina, Sep 19, 2005.

  1. Martina

    Martina Guest

    Why is important to use "use strict"?
    Perl script work very good without this command.
    Am I forgot something?

    Tnx.
     
    Martina, Sep 19, 2005
    #1
    1. Advertising

  2. Martina

    Brian Wakem Guest

    Martina wrote:

    > Why is important to use "use strict"?
    > Perl script work very good without this command.
    > Am I forgot something?
    >
    > Tnx.



    There's a million reasons. It really helps with typos, that could go
    unnoticed without strict.


    Consider:-

    my $num0 = 9;
    my $num1 = 9;

    my $num2 = $numO * $num1;

    print "\$num2=$num2\n";


    The output is '$num2=0'. It wouldn't run under strict as Global symbol
    "$numO" requires explicit package name at....

    Now imagine hundreds of lines, dozens of variables. Make a typo without
    strict and everything still runs, but you get runtime errors. Hard to
    track down. You might not even notice, but your output would be skewed.


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Sep 19, 2005
    #2
    1. Advertising

  3. Martina wrote:
    > Why is important to use "use strict"?
    > Perl script work very good without this command.
    > Am I forgot something?


    You are forgetting that programmers are humans, too, and thus are making
    mistakes.
    "use strict" catches many of those all too human mistakes like e.g. typos in
    variable names.

    jue
     
    Jürgen Exner, Sep 19, 2005
    #3
  4. In article <mOzXe.9152$%i1.7167@trnddc09>,
    "Jürgen Exner" <> writes:
    >Martina wrote:
    >> Why is important to use "use strict"?
    >> Perl script work very good without this command.
    >> Am I forgot something?

    >
    >You are forgetting that programmers are humans, too, and thus are making
    >mistakes.
    >"use strict" catches many of those all too human mistakes like e.g. typos in
    >variable names.


    I'll have to take exception to this part about variable names.
    use strict; almost demands that you also use 'my' variables.
    Even when using warnings too, 'my' variables aren't checked for
    "variable used only once" warnings. That is my clue that I mis-type
    a variable name.

    I don't understand that aspect of 'my' variables.

    Chris

    --
    Chris Richmond | I don't speak for Intel & vise versa
     
    Chris Richmond - MD6-FDC ~, Sep 19, 2005
    #4
  5. Martina

    Sandman Guest

    In article <dgmb2o$9cq$-com.hr>, "Martina" <>
    wrote:

    > Why is important to use "use strict"?
    > Perl script work very good without this command.
    > Am I forgot something?
    >
    > Tnx.


    strict catches lots of errors that aren't only critical, but could still be the
    reason a huge application fails due to variables being incorrectly rrefernced
    or named.

    And if you make sure to always use strict (and warnings), you will find that
    it's easier to get help here since you have shown that at least the syntax is
    correct and thus the error must be something else.

    --
    Sandman[.net]
     
    Sandman, Sep 19, 2005
    #5
  6. Martina

    John Bokma Guest

    (Chris Richmond - MD6-FDC ~) wrote:

    > I'll have to take exception to this part about variable names.
    > use strict; almost demands that you also use 'my' variables.
    > Even when using warnings too, 'my' variables aren't checked for
    > "variable used only once" warnings. That is my clue that I mis-type
    > a variable name.


    Which fails if you type a name wrong twice. Especially if you're
    refactoring a project :-D.

    > I don't understand that aspect of 'my' variables.


    I am sure the documentation will clear this up, a lot even.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Sep 19, 2005
    #6
  7. Chris Richmond - MD6-FDC ~ wrote:
    > In article <mOzXe.9152$%i1.7167@trnddc09>,
    > "Jürgen Exner" <> writes:
    >> "use strict" catches many of those all too human mistakes like e.g.
    >> typos in variable names.

    >
    > I'll have to take exception to this part about variable names.
    > use strict; almost demands that you also use 'my' variables.
    > Even when using warnings too, 'my' variables aren't checked for
    > "variable used only once" warnings. That is my clue that I mis-type
    > a variable name.
    >
    > I don't understand that aspect of 'my' variables.


    Trivial sample program:

    my $foo = 'some text';
    print $too;

    Not using strictures this program just terminates without any output.

    Under strictures it generates the error
    Global symbol "$too" requires explicit package name at [...] line 4.
    Execution of [...] aborted due to compilation errors.

    Which one do you prefer?
    BTW: probably you had to look two or three times to spot the mistake in the
    code. A good indication that even a code review by a fellow programmer
    probably would have missed this mistake.

    jue
     
    Jürgen Exner, Sep 20, 2005
    #7
    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. Alan Franklin
    Replies:
    1
    Views:
    1,521
    Dario
    Feb 2, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    995
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,265
    Smokey Grindel
    Dec 2, 2006
  4. Ben Edwards
    Replies:
    2
    Views:
    119
    James Edward Gray II
    Jul 13, 2007
  5. Replies:
    5
    Views:
    115
Loading...

Share This Page