Perl SemiPrivate Variables in Function

by Geethalakshmi 2010-09-17 14:19:35

Perl SemiPrivate Variables in Function


The local operator is used to define variables of any type that are private to the subroutine there are defined in and any subroutine called within the defininf subroutine.

$value = "original";

tellme();
spoof();
tellme();

sub spoof {
local ($value) = "temporary";
tellme();
}

sub tellme {
print "Current value is $value\n";
}

This code cause the sub tellme to be executed 3 times, printing the results as follows...

first time, prints "original"
second time, prints "temporary"
third time, prints "original"

Tagged in:

1146
like
0
dislike
0
mail
flag

You must LOGIN to add comments