Perl - Private Variables in Function
by Geethalakshmi[ Edit ] 2010-09-17 14:18:54
Perl - Private Variables in Function
One private variable is the '@_' and access to it's elements by $_[x]. You can create your private variables by use of the my operator. my variables are strictly used within the subroutine where there are defined. my variables can only be scalar, array or hash. The following example uses variable names that it more readable. In addition it passes an extra value that is used for a limits check.
sub bigger_than {
my ($limit, @values);
($limit, @values) = @_;
my(@result);
foreach $_ (@values) {
if ($_ > $limit) {
push(@result, $_);
}
}
return @result;
}
@new = bigger_than(100, @list); # @new contains all values in @list >100
@this = bigger_than(5,1,5,15,30); # @this = 15,30