Hello Perl experts,
I recently self-learn about the subroutine.
When do we need a local variables in the subroutine? Illustration with an example will be great!
Here is one example I have:
"
sub add{
local ($sum);
$sum=o;
foreach $_(@_){
$sum+=$_;
}
$sum;
}
"
I am totally confused by the local ($sum)here. What will happen, if we don't use the local() but just initialize a $sum?
Regards,
szzxy
I recently self-learn about the subroutine.
When do we need a local variables in the subroutine? Illustration with an example will be great!
Here is one example I have:
"
sub add{
local ($sum);
$sum=o;
foreach $_(@_){
$sum+=$_;
}
$sum;
}
"
I am totally confused by the local ($sum)here. What will happen, if we don't use the local() but just initialize a $sum?
Regards,
szzxy