> So these are equivalent:
seq 10 | perl6 -ne 'my Int $y += $_; END { print $y; }'
seq 10 | perl6 -e '(my Int $y += $_; END { print $y; }) for lines'
> (Note that I needed to surround it in parentheses so that it is one
> statement.)
> It could be argued that -n should turn your code into a lambda first.
seq 10 | perl6 -e '{ my Int $y += $_; END { print $y; } } for lines'
10
Right, this is what I sort of expect from
seq 10 | perl6 -ne 'my Int $y += $_; END { print $y; }'
as the "my" would seem to localize $y in the while (or for) loop, so it
shouldn't accumulate. But it does.
> Then you would need to use the 「state」 keyword more often.
seq 10 | perl6 -e '{ state Int $y += $_; END { print $y; } } for lines'
55
The use of "state" makes sense. In p5
$ seq 10 | perl -nE ' my $y += $_} ; END { say $y; '
[crickets]
$ seq 10 | perl -nE ' $y += $_} ; { say $y; '
55
$ seq 10 | perl -nE ' our $y += $_} ; { say $y; '
55
Hmm:
$ seq 10 | perl -mstrict -wnE ' $y += $_} ; { say $y; '
55
$ seq 10 | perl -mstrict -wnE ' my $y += $_} ; { say $y; '
Name "main::y" used only once: possible typo at -e line 1.
Use of uninitialized value $y in say at -e line 1, <> line 10.
$ seq 10 | perl -mstrict -wnE ' our $y += $_} ; { say $y; '
55
I'd've thought the first one would get a warning too.
$ seq 10 | perl6 -ne 'our Int $y += $_; END { say $y; }'
===SORRY!=== Error while compiling -e
Cannot put a type constraint on an 'our'-scoped variable
at -e:1
--> our Int $y⏏ += $_; END { say $y; }
expecting any of:
constraint
From: Brad Gilbert
Sent: Thursday, September 26, 2019 9:52 PM
To: Andy Bach
Cc: William Michels ; yary ; perl6
; Joseph Brenner ; Elizabeth Mattijsen
; Marc Chantreux ; Vittore Scolari
Subject: Re: anything faster than say [+] lines?
With the Perl5 compiler the -n flag literally adds this around your code before
compiling:
while ( <> ) {
…
}
Rakudo handles -n by transforming the AST (or the bytecode) into something that
loops.
Basically it is more like:
… for lines
(In that it doesn't affect scoping or compile-time effects.)
So these are equivalent:
seq 10 | perl6 -ne 'my Int $y += $_; END { print $y; }'
seq 10 | perl6 -e '(my Int $y += $_; END { print $y; }) for lines'
(Note that I needed to surround it in parentheses so that it is one statement.)
It could be argued that -n should turn your code into a lambda first.
seq 10 | perl6 -e '{ my Int $y += $_; END { print $y; } } for lines'
10
Then you would need to use the 「state」 keyword more often.
seq 10 | perl6 -e '{ state Int $y += $_; END { print $y; } } for lines'
55
On Thu, Sep 26, 2019 at 4:31 PM Andy Bach
mailto:andy_b...@wiwb.uscourts.gov>> wrote:
> Could the "-e" flag be limiting variable initializations to one?
I don't think so. I recall the -n being shorthand for wrapping your -e program
in
while ( <> ) {
# your program here
}
(-p just adds a continue "print" block, I believe), as folks would do cool
tricks of writing their -e script to have an early close while curly, instead
of, say, using END blocks
$ seq 10 | perl -nE ' $y += $_} ; { say $y; '
55
Note: using "my"
$ seq 10 | perl -nE ' my $y += $_} ; { say $y; '
[crickets]
gets you nothing, as $y is scoped to the -n while loop ;->
From: William Michels mailto:w...@caa.columbia.edu>>
Sent: Thursday, September 26, 2019 3:01 PM
To: yary mailto:not@gmail.com>>
Cc: perl6 mailto:perl6-users@perl.org>>; Andy Bach
mailto:andy_b...@wiwb.uscourts.gov>>; Joseph
Brenner mailto:doom...@gmail.com>>; Elizabeth Mattijsen
mailto:l...@dijkmat.nl>>; Marc Chantreux
mailto:e...@phear.org>>; Vittore Scolari
mailto:vittore.scol...@gmail.com>>
Subject: Re: anything faster than say [+] lines?
Hi Yary,
Honestly, I just tried re-writing the fastest StackOverflow answer
(written in Perl 5) that I found below, in Perl 6. To write P5 as P6 I
had to declare the variable $x with 'my'. Then I played around with a
declaration restricting to "Int" type (to look at potential
performance hits), just because well--with Perl 6--I could.
>#Perl 5 code:
>seq 100 | perl -lne '$x += $_; END { print $x; }'
https://stackoverflow.com/a/47162173
I'm guessing the answer as to 'why "my Int $y" isn't re-initialized
every time' in P6 is similar to the reason in P5? Could the "-e" flag
be limiting variable initializations to one?
Best Regards, Bill.
On Thu, Sep 26, 2019 at 12:00 PM yary
mailto:not@gmail.com>> wrote:
>
> I see that Int/Num error, and also would like an explanation as to why "my
> Int $y" isn't re-initialized to Any each time through this loop
>
> $ seq 100 | perl6 -ne 'my Int $y += $_; END { print $y; }'
>
> Type check failed in assignment to $y; expected Int but got Num
> (5050e0)
>
> in block at -e line 1
>
>
> $ perl6 --version
>
> This is Rakudo Star version