StuPeas
08-17-2006, 05:17 PM
In the code below it seems that surrounding the variable name ($total) in parentheses when declaring it as local (in the sub "change_total"), gives a different output, (when the last call to sub show_total is executed), than if the variable appears without parentheses.
If the parentheses are left off, the last call to sub show_total accesses the global version of $total instead.
The output of the script is as follows:
With brackets
0
5
5
Without brackets
0
5
0
$total = 0;
show_total();
change_total();
show_total();
sub change_total
{
local($total) = 5;
show_total();
}
sub show_total
{
print('$total equals '. "$total\n");
}
I would be greatful if someone could explain the difference to me.
THANX
If the parentheses are left off, the last call to sub show_total accesses the global version of $total instead.
The output of the script is as follows:
With brackets
0
5
5
Without brackets
0
5
0
$total = 0;
show_total();
change_total();
show_total();
sub change_total
{
local($total) = 5;
show_total();
}
sub show_total
{
print('$total equals '. "$total\n");
}
I would be greatful if someone could explain the difference to me.
THANX