In the first example, change increment() to return $data instead of undef, and then you can call error() without getting an uninitialized value error.
In the second example, simply give $data a default value, call error() before your while block, and you'll see that it prints.
Here's the code I used for an example:
I put the defined check in there so the loop will get past the first iteration. $data++ (when $data is not initialized) evaluates to zero:#!/usr/bin/perl -w use strict; my $data; my $ERROR = 0; # error(); # uncomment this to print $data before the loop while ( (defined $data++) && everything_is_okay() ) { print "looping for the $data time.\n"; if ( $data > 5 ) { $ERROR++; error(); # print $data if there's an error } } sub everything_is_okay { if ( $ERROR ) { print STDERR "ERROR LEVEL $ERROR\n"; exit $ERROR; } return 1; } sub error { print $data; }
my $data; print $data++;
In reply to RE: RE: Benchmark.pm's scoping behavior
by chromatic
in thread Benchmark.pm's scoping behavior
by Russ
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |