develooper Front page | perl.perl6.users | Postings from December 2018

Re: Basic question about lexical binding in relationship with "listassignment"

Thread Previous
From:
JJ Merelo
Date:
December 27, 2018 09:33
Subject:
Re: Basic question about lexical binding in relationship with "listassignment"
Message ID:
CAJoLtgeTctJ8RNOTLxCO2H85L=9o8WdnvPo=KnysgnjxMtwRBg@mail.gmail.com
Hi,

El jue., 27 dic. 2018 a las 9:30, Raymond Dresens (<
raymond.dresens@gmail.com>) escribió:

> Hello,
>
> I'm getting up to speed with Perl 6 again after a long while, and I more
> or less 'hit my nose' against something that I can reduce to the
> following very basic snippet of code:
>
>     my $foo = 3;
>     say $foo;
>
>     {
>         say $foo;
>
>         my $foo = 6;
>
>         say $foo;
>     }
>
> This will not compile with Perl 6 (Rakudo 2018.12) -- lexical symbol
> '$foo' is already bound to an outer symbol; the implicit outer binding
> must be rewritten as OUTER::<$foo> before you can unambiguously declare
> a new '$foo' in this scope.
>

But where's that error? In the first "say $foo"?

>
> This will compile with Perl 5 when I add 'use v5.14;' at the top and it
> will then print 3, then 3 then 6 (as expected).
>
> Well, it seems that I can 'cheat' by simply doing a list assignment:
>
>     my $foo = 3;
>     say $foo;
>
>     {
>         say $foo;
>
>         my ($foo) = 6; # avoid `... '$foo' is already bound ...'
>

That should make no difference...

>
>         say $foo;
>         say $foo.WHAT; # is it really an integer?
>     }
>
> The output is:
>
>     3
>     (Any)
>

That's the first say $foo in the block, I guess...

    6
>     (Int)
>
> This behavior is fine I think (but a little unexpected due to my
> experience with Perl 5).
>
> Is this behavior normal/wanted/intented (from a language/compiler
> perspective?).
>
> Perhaps this shouldn't be and the compiler should also complain in this
> list assignment case, or perhaps generate a warning?
>
> I'm not that confident to state that this is a bug ;)
>
> Hence the reason why I kind-of report this,
>


It might be a documentation bug... Which you might want to report anyway.
The thing with Perl 6 is that parsing, compiling and runtime are quite
different. In Perl, far as I can tell, you are actually assigning scope to
$foo when you run that sentence. That's not the case in Perl 6, or does not
seem to be. However, this should be quite clear from the get go.

Cheers

JJ

Thread Previous


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