Dear All,
I have two perl scripts: - The first one:
sub Tt {
$TTT = uc($TTT); if(($TTT ne "ON") && ($TTT ne "OFF")){ print "ERROR: Check variable TTT";} else { print "It is a Test...";} } # ---MAIN---
$TTT = "OFF";
Tt();
If I use $TTT = "OFF", I get 'It is a Test...' and if I use $TTT="O" I get 'ERROR: Check variable TTT'. This, it is normal for me.
- The second script using 'strict' this time:
use strict;
sub Tt {
my $TTT; $TTT = uc($TTT); if(($TTT ne "ON") && ($TTT ne "OFF")){ print "ERROR: Check variable TTT";} else { print "It is a Test...";} }
# ---MAIN---
my $TTT = "OFF";
Tt();
If I use $TTT = "OFF" I get time 'ERROR: Check variable TTT', Why ?
In the first example, you are setting a _package_ variable named $TTT to a value in your main script, and then you are calling a function which references that _package_ variable.
In the second example, you are creating and setting a _lexical_ variable in your main script. You then call a function which declares a new lexical variable in the new scope which hides the lexical in the surrounding scope.
BTW, I'm assuming this is just a contrived example. Normally you would not set a variable and access it across a function boundary. The proper way, of course, would be to pass the variable as an argument to the function.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>