develooper Front page | perl.perl5.porters | Postings from April 2008

Re: [perl #53504] Bug in if(open(my $fh,...)) { } scoping

Thread Previous | Thread Next
From:
Graham Barr
Date:
April 30, 2008 08:20
Subject:
Re: [perl #53504] Bug in if(open(my $fh,...)) { } scoping
Message ID:
550FA47F-CA29-4B74-B8D4-FBAC22E42E50@pobox.com
On Apr 30, 2008, at 9:03 AM, Rafael Garcia-Suarez wrote:

> 2008/4/30 Matt Sergeant <matt@sergeant.org>:
>>> I know. The `problem' is that the scoping doesn't DWIM. At least not
>>> for me. I don't expect the 'if (my $x = ...) {}' to live on in  
>>> the else
>>> branch, which is why I expect the above snippet to work. I also  
>>> know it
>>> to not work when I see the error, and then I usually change the  
>>> code to
>>>
>>> {   my @scr;
>>>    if    (@scr = ...) {
>>>        :
>>>        }
>>>    elsif (@scr = ...) {
>>>        }
>>>    }
>>>
>>
>>  Yup, IMHO this was an error in judgement by whoever chose this  
>> behaviour
>> originally. But we're stuck with it now.
>
> I don't think so.
> I think the intent behind this feature's design was to allow :
>
> if ((my $ret = somecall()) == 0) {
>     # profit !
> }
> elsif ($ret == -1) {
>     # handle error code -1
> }
> elsif ($ret == -2) {
>     # handle error code -2
> }
> else { etc... }

Yes. I cannot find the email thread that led up to the change but it  
was added in 5.003_08

   Title:  "Defined scoping for C<my> in control structures"
           (Finally defines semantics of "my" in control expressions,
           like the condition of "if" and "while".  In all cases, scope
           of a "my" var extends to the end of the entire control
           structure.  Also adds new construct "for my", which
           automatically declares the control variable "my" and limits
           its scope to the loop.)
    From:  Chip Salzenberg
   Files:  op.c perly.c perly.c.diff perly.h perly.y proto.h toke.c

There is also a mention, with an example, in

http://search.cpan.org/dist/perl/pod/ 
perl5004delta.pod#New_and_changed_builtin_functions

page down a bit to my() in Control Structures

Graham.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About