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

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

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
May 7, 2008 01:48
Subject:
Re: [perl #53504] Bug in if(open(my $fh,...)) { } scoping
Message ID:
20080507084754.GZ4173@klangraum.plasmasturm.org
* Marc Lehmann <schmorp@schmorp.de> [2008-05-02 19:05]:
> On Thu, May 01, 2008 at 12:14:55PM +0200, Aristotle Pagaltzis <pagaltzis@gmx.de> wrote:
> > * Matt Sergeant <matt@sergeant.org> [2008-04-30 16:15]:
> > > If we'd have had given/when back then I suspect the scoping
> > > would have been implemented differently.
> > 
> > Well yeah – one of the many things Perl 6 canonicalises is
> > that there are no edge cases in scoping. A variable declared
> > within a pair of curlies is local to it, and a variable
> > declared outside is not, and no constructs leak scopes across
> > blocks somehow.
> 
> So the my variable would not be local to the if at all in this
> case, but to the outer block? Thats quite horrible, IMnsHO
> (even C++ managed to correct this mistake in scoping :)

No. My description was slightly incomplete in that all blocks are
closures in Perl 6 and so all of them can have formal parameters,
which, of course, are declared in front and outside of the block.
So this Perl 5 construct:

    if ( my $foo = <$bar> ) { ... }

in Perl 6 becomes:

    if =$bar -> $foo { ... }

However, there is no exception for having that declaration remain
in scope for the conditional and block of a following `elsif`. In
instances where you think you want that, you want `given`/`when`
instead:

    given foo() {
        when 'bar' { ... }
        when 'baz' { ... }
        else       { ... }
    }

Again the rule is very simple: the block always denotes the exact
scope of a variable – there is no cross-scope declaration leak
magic anywhere.

> But then, perl6 r.i.p. :=)

Careful with such proclamations, this list is archived in many
places… :-)

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

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