Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Scalar Value Not Available To Subroutine

by BrowserUk (Pope)
on Dec 20, 2012 at 18:04 UTC ( #1009772=note: print w/ replies, xml ) Need Help??


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.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


Comment on Re^3: Scalar Value Not Available To Subroutine
Select or Download Code
Re^4: Scalar Value Not Available To Subroutine
by ikegami (Pope) on Dec 21, 2012 at 05:41 UTC

    Effectively, the loop variable has no relationship with any pre-existing variable with the same name:

    Same goes for local, fyi.

    >perl -E"say 0+\$x; local $x; say 0+\$x;" 4885852 2403948

    I've often wished (and occasionally argued in favour of), that for didn't localise pre-existing, lexical loop variables.

    It would be useful and intuitive. It would drastically change the behaviour of the following, though

    for $lex (...) { push @f, sub { ... $lex ... }; }

    As it is now, I never use a pre-existing var.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009772]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-09-23 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls