On Tue, Sep 1, 2015 at 5:41 PM, Matija Papec <mpapec2...@yandex.com> wrote:

> Scoping of lexical looks interesting
>
> perl6 -ne 'my %d; %d{ .words[1] }++; END { %d.sort.perl.say }'
>
> as this could not work in perl5
>
> perl -nE 'my $d =1; END { say $d//"default!" }' # gives default
>

  It's not the scoping.  It's scoped correctly, it's just that you need to
give it something to read, for the code to run, and the assignment to
happen:

$ echo | perl -nE 'my $d =1; END { say $d//"default!" }'
1
$ </dev/null perl -nE 'my $d =1; END { say $d//"default!" }'
default!
$

  Move the assignment to a BEGIN block, and you get it even without
anything to read:

$ </dev/null perl -nE 'my $d; BEGIN { $d=1 } END { say $d//"default!" }'
1
$


  Aside – hey, this is a Perl6 list, right? – you may find the following
difference … illuminating. :)

$ </dev/null | perl6 -ne 'my $d is default(1); END { say $d//"default!" }'
1
$ </dev/null | perl6 -ne 'my $d =1; END { say $d//"default!" }'
default!
$


Eirik

Reply via email to