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:
H.Merijn Brand
Date:
April 30, 2008 04:31
Subject:
Re: [perl #53504] Bug in if(open(my $fh,...)) { } scoping
Message ID:
20080430133125.11e12409@pc09.procura.nl
On Wed, 30 Apr 2008 09:01:22 +0200, "Rafael Garcia-Suarez"
<rgarciasuarez@gmail.com> wrote:

> 2008/4/29 H.Merijn Brand <h.m.brand@xs4all.nl>:
> >  related nuisance:
> >
> >  /home/merijn 102 > cat xx.pl
> >  #!/pro/bin/perl
> >
> >  use strict;
> >  use warnings;
> >
> >  if    (my @scr = glob ("*.sh")) {
> >     print "I have shell scripts here\n";
> >     }
> >  elsif (my @scr = glob ("*.pl")) {
> >     print "I have perl scripts here\n";
> >     }
> >  /home/merijn 103 > perl xx.pl
> >  "my" variable @scr masks earlier declaration in same scope at xx.pl line 9.
> >  I have perl scripts here
> >  /home/merijn 104 >
> 
> That's exactly the opposite problem you're having there. Just remove
> the 2nd my...

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 = ...) {
        }
    }

-- 
H.Merijn Brand         Amsterdam Perl Mongers (http://amsterdam.pm.org/)
using & porting perl 5.6.2, 5.8.x, 5.10.x  on HP-UX 10.20, 11.00, 11.11,
& 11.23, SuSE 10.1 & 10.2, AIX 5.2, and Cygwin.       http://qa.perl.org
http://mirrors.develooper.com/hpux/            http://www.test-smoke.org
                        http://www.goldmark.org/jeff/stupid-disclaimers/

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