lines already removes the newline at the end, thus the "chomp" is truly not
needed.
and the -n, -p flags also call lines, so no chomp needed for the one-liners
either. In the below example "input2.txt" is a two-line file.

> perl6 -ne 'print $_.perl' input2.txt
"mmm:NN:o:P:q:RRR:s:T""grub:hippo:ibex:jaguar:kangaroo:lemur:nematode"

Incidentally I was also at that meeting, and after a few iterations came up
with this one-liner to re-arrange colon-separated fields as tab-separated.
Not necessarily recommending this-

perl6 -pe 's/ (<-[:]>*) * % . / $0[0,2,1,5].join("\t") /' input1.txt
input2.txt

Joe had very readable and straightforward program, which was a welcome
antidote to my "concentrate of code" tendency. And as for his question "
wondering if there's some direct analog in perl6 to the perl5
construct: while(<>){
... }" ... docs tell me $*ARGFILES is close, except in sub MAIN in 6.d when
it became a synonym for $*IN.

Recap, let's see how perl5 behaves.
$  perl -e 'while (<>) {print}' input2.txt
mmm:NN:o:P:q:RRR:s:T
grub:hippo:ibex:jaguar:kangaroo:lemur:nematode

$ echo I am one more line | perl -e 'while (<>) {print}'
I am one more line

$ echo I am one more line | perl -e 'while (<>) {print}' input2.txt
mmm:NN:o:P:q:RRR:s:T
grub:hippo:ibex:jaguar:kangaroo:lemur:nematode

perl6 using $*ARGFILES

$ perl6.bat -e 'for $*ARGFILES.lines {.say}' input2.txt
mmm:NN:o:P:q:RRR:s:T
grub:hippo:ibex:jaguar:kangaroo:lemur:nematode

$ echo I am one more line | perl6.bat -e 'for $*ARGFILES.lines {.say}'
I am one more line

$ echo I am one more line | perl6.bat -e 'for $*ARGFILES.lines {.say}'
input2.txt
mmm:NN:o:P:q:RRR:s:T
grub:hippo:ibex:jaguar:kangaroo:lemur:nematode

Looks the same to me!

The bit about sub MAIN in 6.d is to force devs to be explicit in what's
coming from the command line vs stdin, since MAIN is specifically good with
parsing command-line args, I think.

-y


On Mon, Jul 29, 2019 at 1:40 PM Brad Gilbert <b2gi...@gmail.com> wrote:

> A rather direct translation:
>
>     for lines() {
>        # .=chomp;
>        print join("\t", .split(':')[0, 2, 1, 5] ), "\n";
>     }
>
> Note that for `.=chomp` to work, you probably have to declare `$_` as `is
> copy`.
>
>     for ("a\n", "b\n") -> $_ is copy {
>        .=chomp;
>        print join("\t", .split(':')[0, 2, 1, 5] ), "\n";
>     }
>
> But that really doesn't matter as `lines` auto-chomps the input.
>
> ---
>
> I think I would organize it differently if I was trying to explain Perl6.
>
>     for lines() {
>       put .split(':').[0, 2, 1, 5].join("\t")
>     }
>
> (`put` only adds the value of `$*OUT.nl-out`, while `say` calls `.gist`
> first.)
>
> On Mon, Jul 29, 2019 at 2:50 PM William Michels via perl6-users <
> perl6-us...@perl.org> wrote:
>
>> Hello, Just a short backgrounder to say that this question arose this
>> past weekend at a Perl6 Meetup (Oakland, CA). Specifically we were
>> looking at how to write a Perl6 version of some introductory Perl5
>> code in "Learning Perl", 7th Edition by Tom Phoenix, brian d foy,
>> Randal L. Schwartz:
>>
>> #Perl 5 code below:
>> while (<>) {
>>   chomp;
>>   print join("\t", (split /:/)[0, 2, 1, 5] ), "\n";
>> }
>>
>>
>> https://www.oreilly.com/library/view/learning-perl-7th/9781491954317/ch01.html
>>
>> (Thanks to Joseph Brenner for organizing the Perl6 Meetup).
>>
>>
>>
>>
>>
>>
>> On Mon, Jul 29, 2019 at 2:09 AM Elizabeth Mattijsen <l...@dijkmat.nl>
>> wrote:
>> >
>> > Also, you can make this conditional:  show me all the comment lines of
>> a source file:
>> >
>> >
>> > $ perl6 -e '.say if .starts-with('#') for lines' source-file
>> >
>> >
>> > > On 29 Jul 2019, at 10:06, Richard Hainsworth <rnhainswo...@gmail.com>
>> wrote:
>> > >
>> > > Also no need for all the brackets
>> > >
>> > > .say for lines;
>> > >
>> > > This is quite idiomatic Perl 6 and not golfing
>> > >
>> > > On Mon, 29 Jul 2019, 07:13 Joseph Brenner, <doom...@gmail.com> wrote:
>> > > > Hmmm. I would expect that to be in the Perl 5 to Perl 6 Migration
>> Guides, but I do not see it there.
>> > >
>> > > Exactly, I was just looking there, and I ended up playing around with
>> > > the method form of lines, and didn't think to try the function
>> > > form of it.
>> > >
>> > > To summarize, if the goal is to write a "simple_echo" script that
>> > > can work with a file name or with lines on standard input:
>> > >
>> > >    simple_echo lines.txt
>> > >    cat lines.txt | simple_echo
>> > >
>> > > The perl5 version would probably be:
>> > >
>> > >   #!/usr/bin/env perl
>> > >   while(<>){
>> > >      print;
>> > >   }
>> > >
>> > > The perl6 version would be something like:
>> > >
>> > >   #!/usr/bin/env perl6
>> > >   use v6;
>> > >   for lines() {
>> > >       say $_;
>> > >   }
>> > >
>> > >
>> > > The kind of thing I was playing with was:
>> > >
>> > >   #!/usr/bin/env perl6
>> > >   use v6;
>> > >   my @lines = $*ARGFILES.IO.lines;
>> > >   say @lines;
>> > >
>> > > That works for lines from a file, but not from standard input, and
>> the
>> > > error message isn't tremendously helpful:
>> > >
>> > >   No such method 'lines' for invocant of type 'IO::Special'
>> > >
>> > >
>> > >
>> > >
>> > > On 7/28/19, Bruce Gray <robertbrucegr...@gmail.com> wrote:
>> > > >
>> > > >
>> > > >> On Jul 28, 2019, at 6:20 PM, Joseph Brenner <doom...@gmail.com>
>> wrote:
>> > > >>
>> > > >> I was just wondering if there's some direct analog in perl6 to the
>> > > >> perl5 construct:
>> > > >>
>> > > >>  while(<>){ ... }
>> > > >>
>> > > >> If I'm planning on passing a filename on the command-line, I can
>> just
>> > > >> get it out of $*ARGFILES easily enough, but what if I also wanted
>> it
>> > > >> to work on lines passed in via standard input?
>> > > >
>> > > >
>> > > > `lines` , as a sub instead of a method, and no arguments.
>> > > >
>> > > > See: https://docs.perl6.org/routine/lines#(Cool)_routine_lines
>> > > >       Without any arguments, sub lines operates on $*ARGFILES,
>> which defaults to
>> > > > $*IN in the absence of any filenames.
>> > > >
>> > > > For example:
>> > > >       perl6 -e 'say .join("\t") for lines().rotor(4);'
>> path/to/file.txt
>> > > >
>> > > > Hmmm. I would expect that to be in the Perl 5 to Perl 6 Migration
>> Guides,
>> > > > but I do not see it there.
>> > > >
>> > > > —
>> > > > Hope this helps,
>> > > > Bruce Gray (Util of PerlMonks)
>> > > >
>> > > >
>>
>

Reply via email to