develooper Front page | perl.perl6.users | Postings from January 2020

Using raku/perl6 as unix "cat"....

Thread Next
From:
William Michels via perl6-users
Date:
January 19, 2020 00:45
Subject:
Using raku/perl6 as unix "cat"....
Message ID:
CAA99HCwbOkGj=J68x0etxW6jJ4BwDgaCABwJvvYELMY7mqmdZQ@mail.gmail.com
Hello All,

I've been reviewing literature that discusses using raku/perl6 as a
replacement for common unix utilities. One important unix utility is
"cat". I looked at docs/blogs and found a recommendation to use "$*IN"
along with "slurp" (references at bottom). Using a seven-line test
file "testthis_abc_def.txt" below (1), the recommended "slurp" code
works as expected (2).

However, another recommendation to use "$*IN" along with the "get"
method fails when a blank line is encountered, only returning
truncated output (3). I tried correcting truncated output seen with
"get" by playing with the command-line arguments "-ne" (4) and "-pe"
(5), but only ended up mangling output even further.

Can "get" be used in when writing raku/perl6 replacement code for "cat"?

Any advice appreciated,

Bill.


[1]mydir$ cat testthis_abc_def.txt
a
b
c

d
e
f
[2]mydir$ perl6 -e 'say $*IN.slurp;' < testthis_abc_def.txt
a
b
c

d
e
f

[3]mydir$ perl6 -e '.say while $_ = $*IN.get;' < testthis_abc_def.txt
a
b
c
[4]mydir$ perl6 -ne '.say while $_ = $*IN.get;' < testthis_abc_def.txt
b
c
e
f
[5]mydir$ perl6 -pe '.say while $_ = $*IN.get;' < testthis_abc_def.txt
b
c

e
f
(Mu)
[6]mydir$


REFERENCES:
1. https://docs.raku.org/routine/slurp
2. https://docs.raku.org/routine/get
3. https://andrewshitov.com/2019/09/09/the-cat-utility-written-in-perl-6/
4. https://stackoverflow.com/questions/52597984/catching-exception-of-a-shell-command-in-perl-6

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