Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Duh. 'my' scope in if else blocks.

by TimToady (Parson)
on Dec 12, 2007 at 16:48 UTC ( [id://656668]=note: print w/replies, xml ) Need Help??


in reply to Re: Duh. 'my' scope in if else blocks.
in thread Duh. 'my' scope in if else blocks.

In addition to all the body blocks, the whole if statement is a block of its own. Just like for and foreach loops.
Note though that we very intentionally broke this in Perl 6. It's one of those places where you have to keep track of a list of exceptions in Perl 5, and all such lists are a design smell. (The right way to fix it was to fix the for/foreach syntax, not the if syntax. A loop variable should be lexical to its block by virtue of being a formal parameter to that block, not because it just happens to be mentioned in an exceptional syntax.)

Replies are listed 'Best First'.
Re^3: Duh. 'my' scope in if else blocks. (implicit)
by tye (Sage) on Dec 12, 2007 at 17:31 UTC

    Heh, I've long disliked the "invisible" block around Perl if() constructs. It belies the term "lexical scope" to me. I resolved this dislike by mostly refusing to declare lexicals inside of conditional expressions.

    This reminds me of my long-standing frustration with the adamant, unconditional localization of loop variables. Default localization of loop variables seems a wise choice but there have been far too many times where part of my purpose in looping over some values was to pick one of them. And you can't do that "the obvious way" in Perl. You get the relatively awkward:

    my $selected; for my $candidate ( ... ) { $selected= $candidate; last if ...; }

    localization of loop variables preceeded 'my' so, given the time-machine-like oportunity of Perl 6 breaking backward compatability in a backward-compatible way, I'm tempted to propose that "for my $foo (" continue to effectively localize (by creating a new lexical $foo) but change "my $foo; ...; for $foo (" to leave the last looped value in $foo. But, of course, that gets the Huffman coding backward and is convoluted enough to be unwise.

    I re-read the section of a4 about 'foreach' and didn't see this mentioned. So I'm assuming localization is still unconditional. It makes me long for an alternative to avoid the localization. But having "for @choices -> my $chosen {" serve that purpose doesn't scratch the itch as well as I'd like and also leaves too many scratch marks.

    But maybe you've already designed a better way to do this (and probably described it and I've probably read the description and either forgotten it or not understood the implications of it :). Perhaps "reusing" a variable name from the current lexical scope as a formal parameter should mean that this variable is actually used as the one that the parameter is bound to and the (last) bound parameter value is left in that variable. In the short time I've considered that idea, and with so little of the rest of Perl 6 swapped in, I think I might like that idea.

    - tye        

      For up to date information on Perl 6 you should look at the Synopsis.

      To summarize the current state, for just takes a block and feeds as many scalars as needed into that block.

      The block can either be a "pointy" block:

      for @list -> $a { # here $a is local'ized in the perl 5 sense }

      Or you can use the "weird scoping" twigil for placeholder arguments:

      for @list { say $^a; # $^a is only available in this block }

      I think that just reusing a lexical variable if there is one available doesn't fit very will into the current concept of blocks and captures.

      Perhaps this is one of the cases where weird/unusal scoping deserves its own twigil:

      my $a; for @list -> $=a { # $=a is the "reused" version of $a } # here $a has the last value of $=a

      I still don't know how much effort that is, how well it fits into the current specs, and if it's worth the trouble. But it's an interesting idea ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found