in reply to Re^2: Scalar Value Not Available To Subroutine
in thread Scalar Value Not Available To Subroutine
Seems so. Effectively, the loop variable has no relationship with any pre-existing variable with the same name:
my $i; for $i ( 1..10 ){ 1; }; print $i };; Use of uninitialized value $i in print at (eval 9) line 1, <STDIN> lin +e 1.
I believe it is a throw-over from pre-lexical Perl.
I've often wished (and occasionally argued in favour of), that for didn't localise pre-existing, lexical loop variables.
It makes for messy workarounds to the situation where a counting loop can exit early conditionally, and you want to know how far it counted:
my $i; for $i ( 1 .. $N ) { last if <SOMECOND>; ... } ## Here we have no way of knowing if we took an early exit; ## thus necessitating additional, unnecessary complexity to retain or +discover that.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Scalar Value Not Available To Subroutine
by ikegami (Patriarch) on Dec 21, 2012 at 05:41 UTC |