The 'use strict' pragma is a little hard to get used to if you have
been coding a while without it, but can be a great help in creating and
debugging your programs.  'use strict' lets the perl interpreter know that
you want all variables, subs, and references to be declared and scoped.
Here's an example.  In the following code it might be hard to figure out why
the expected result doesn't print:

foreach $computer(@hostlist){
   print "Now working on $Computer\n";
}

     In this short example it is relatively easy to see that $computer was
accidentally capitalized, so the expected result will not print.  Without
'use strict', Perl would obligingly create a variable with the new name and
print the contents, undef in this case.  If you tried to do this under the
'use strict' pragma, the interpreter would generate errors.  First of all,
you would need to declare $computer with 'my', so your code would look more
like this:

my @hostlist;
foreach my $computer(@hostlist){
   print "Now working on $computer\n";
}

     And if you accidentally capitalized $computer, the interpreter would
think you were trying to use a variable that wasn't declared with 'my', and
would generate errors, possibly saving you huge headaches as you try to
retrace your steps to find out why $computer isn't printing out.  

     This is just off the top of my head, but try to use it in some of your
scripts.  After you've worked away at it and gotten it to work, you'll get a
better idea of what it can do.  While you're at it, read up on 'my'.  You'll
need a good idea of how to use lexical scoping in your scripts.  If you have
more specific questions, go ahead and post them here.  Happy coding! :)

-----Original Message-----
From: Yuan Cheng
To: [EMAIL PROTECTED]
Sent: 3/15/02 7:29 PM
Subject: the scope of 'use strict'

HI, all:

I am wondering what else 'use strict' does besides it
is stated in the perldoc that it stricts on use of
'vars', 'refs' and 'subs'.  Thanks.


yc

__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------------------
This email may contain confidential and privileged 
material for the sole use of the intended recipient. 
If you are not the intended recipient, please contact 
the sender and delete all copies.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to