why is important to use : use strict?

M

Martina

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

Tnx.
 
B

Brian Wakem

Martina said:
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.
 
J

Jürgen Exner

Martina said:
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
 
C

Chris Richmond - MD6-FDC ~

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
 
S

Sandman

"Martina" <[email protected]> said:
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.
 
J

John Bokma

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.
 
J

Jürgen Exner

Chris said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top