M
Mark
Suppose I have a program with the entry point at the top and
subroutines following the exit point. I want to have a subroutine
local variable, $count in my example, that is initialized to a
starting value.
This works, but seems overly cumbersome to me. Is there a better way?
use strict;
use warnings;
# entry point
doit();
doit();
exit; # exit point before subroutine definition
{
my $count;
BEGIN {
$count = 1;
}
sub doit {
print "count=$count\n";
$count++;
}
}
__END__
Output:
count=1
count=2
subroutines following the exit point. I want to have a subroutine
local variable, $count in my example, that is initialized to a
starting value.
This works, but seems overly cumbersome to me. Is there a better way?
use strict;
use warnings;
# entry point
doit();
doit();
exit; # exit point before subroutine definition
{
my $count;
BEGIN {
$count = 1;
}
sub doit {
print "count=$count\n";
$count++;
}
}
__END__
Output:
count=1
count=2