Re: Using raku/perl6 as unix "cat"....
On 2020-01-21 11:08, William Michels via perl6-users wrote: Good answers, all. Thanks to everyone for contributing. For anyone who wants a golfed "cat" replacement, command line arguments can give you shorter code: Hi William, I don't know if I contributed anything at all, but you are most welcome. Now for your next homework assignment, write a "less" substitute in Raku! You have to read the keyboard, do a forwards and backwards, and a search. man less DESCRIPTION Less is a program similar to more (1), but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi (1). Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.) Commands are based on both more and vi. Commands may be preceded by a decimal number, called N in the descriptions below. The number is used by some commands, as indicated. Like "cat" on steroids! Probably more fun than writing Winn API calls! :-) -T
Re: Using raku/perl6 as unix "cat"....
" 'while' ...will stop when it encounters a false line--typically an empty line or '0' ". Wasn't that the point of p5's defined while ( defined(my $line = <> ) ) { or (previously lexified '$val'): print "$val\n" while defined($val = pop(@ary)); From: William Michels via perl6-users Sent: Tuesday, January 21, 2020 1:08 PM To: Trey Harris ; perl6-users Cc: Andrew Shitov ; Curt Tilmes ; Joseph Brenner ; ToddAndMargo ; yary ; Elizabeth Mattijsen ; ngayw...@une.edu.au Subject: Re: Using raku/perl6 as unix "cat" Good answers, all. Thanks to everyone for contributing. For anyone who wants a golfed "cat" replacement, command line arguments can give you shorter code: mydir$ perl6 -e '.say for lines' ab_cd.txt a b c d mydir$ perl6 -ne '.say' ab_cd.txt a b c d mydir$ # below two single quotes as empty arg: mydir$ perl6 -pe '' ab_cd.txt a b c d mydir$ Finally, a question about the 'get' docs, which use 'while' as an example for the (IO::CatHandle) 'get' method. The docs say "(IO::CatHandle) method get: Returns a single line of input from the handle... . Returns Nil when there is no more input. ... ." And, "(IO::Handle) routine get: Reads a single line of input from the handle... . Returns Nil, if no more input is available... ." See: https://docs.raku.org/routine/get Should a different example other than "while/get" be used on the docs page? Or should some of the comments Yary made regarding "while/get" be incorporated into the docs--so programmers can be warned about truncating their output on blank lines with "while/get"? Yary, from Jan. 18th: " 'while' ...will stop when it encounters a false line--typically an empty line or '0' ". Best Regards, Bill. On Mon, Jan 20, 2020 at 4:13 PM Trey Harris wrote: > > On Mon, Jan 20, 2020 at 19:03 Trey Harris wrote: >> >> On Mon, Jan 20, 2020 at 02:59 William Michels via perl6-users >> wrote: >>> >>> Hi Yary (and Todd), >>> >>> Thank you both for your responses. Yary, the problem seems to be with >>> "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 >>> actually returns fewer lines with "for" than it did with "while": >> >> >> If you want to do line-oriented input, use `.lines` with `for`; it returns >> something `for` can iterate over. > > > Sorry, in a setting where a handle isn’t the context, I meant `lines`, not > `.lines`, though I was referring _to_ a thing called `.lines`, the multi > method. I don’t think we’ve all yet agreed on how multis that can be called > as plain routines should be referred to in umbrella term. `.lines` is more > “correct”, but it’s less likely to actually work without understanding more, > which is a strange conundrum for documentation. > > Trey
Re: Using raku/perl6 as unix "cat"....
Good answers, all. Thanks to everyone for contributing. For anyone who wants a golfed "cat" replacement, command line arguments can give you shorter code: mydir$ perl6 -e '.say for lines' ab_cd.txt a b c d mydir$ perl6 -ne '.say' ab_cd.txt a b c d mydir$ # below two single quotes as empty arg: mydir$ perl6 -pe '' ab_cd.txt a b c d mydir$ Finally, a question about the 'get' docs, which use 'while' as an example for the (IO::CatHandle) 'get' method. The docs say "(IO::CatHandle) method get: Returns a single line of input from the handle... . Returns Nil when there is no more input. ... ." And, "(IO::Handle) routine get: Reads a single line of input from the handle... . Returns Nil, if no more input is available... ." See: https://docs.raku.org/routine/get Should a different example other than "while/get" be used on the docs page? Or should some of the comments Yary made regarding "while/get" be incorporated into the docs--so programmers can be warned about truncating their output on blank lines with "while/get"? Yary, from Jan. 18th: " 'while' ...will stop when it encounters a false line--typically an empty line or '0' ". Best Regards, Bill. On Mon, Jan 20, 2020 at 4:13 PM Trey Harris wrote: > > On Mon, Jan 20, 2020 at 19:03 Trey Harris wrote: >> >> On Mon, Jan 20, 2020 at 02:59 William Michels via perl6-users >> wrote: >>> >>> Hi Yary (and Todd), >>> >>> Thank you both for your responses. Yary, the problem seems to be with >>> "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 >>> actually returns fewer lines with "for" than it did with "while": >> >> >> If you want to do line-oriented input, use `.lines` with `for`; it returns >> something `for` can iterate over. > > > Sorry, in a setting where a handle isn’t the context, I meant `lines`, not > `.lines`, though I was referring _to_ a thing called `.lines`, the multi > method. I don’t think we’ve all yet agreed on how multis that can be called > as plain routines should be referred to in umbrella term. `.lines` is more > “correct”, but it’s less likely to actually work without understanding more, > which is a strange conundrum for documentation. > > Trey
Re: Using raku/perl6 as unix "cat"....
> On 21 Jan 2020, at 01:01, Norman Gaywood wrote: > On Tue, 21 Jan 2020 at 03:12, Brad Gilbert wrote: > .say for lines > > Since .say calls gist(), would it be not be better/safer to call .put instead? > >.put for lines Since lines only produces Str objects, and both Str.Str and Str.gist are noops, it doesn't make any difference? Liz
Re: Using raku/perl6 as unix "cat"....
On Mon, Jan 20, 2020 at 19:03 Trey Harris wrote: > On Mon, Jan 20, 2020 at 02:59 William Michels via perl6-users < > perl6-users@perl.org> wrote: > >> Hi Yary (and Todd), >> >> Thank you both for your responses. Yary, the problem seems to be with >> "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 >> actually returns fewer lines with "for" than it did with "while": > > > If you want to do line-oriented input, use `.lines` with `for`; it returns > something `for` can iterate over. > Sorry, in a setting where a handle isn’t the context, I meant `lines`, not `.lines`, though I was referring _to_ a thing called `.lines`, the multi method. I don’t think we’ve all yet agreed on how multis that can be called as plain routines should be referred to in umbrella term. `.lines` is more “correct”, but it’s less likely to actually work without understanding more, which is a strange conundrum for documentation. Trey
Re: Using raku/perl6 as unix "cat"....
On Mon, Jan 20, 2020 at 19:01 Norman Gaywood wrote: > > On Tue, 21 Jan 2020 at 03:12, Brad Gilbert wrote: > >> .say for lines >> > > Since .say calls gist(), would it be not be better/safer to call .put > instead? > >.put for lines > If being set directly from `lines`, I can’t see when there would be a difference unless you’re playing with encodings. If you entire program is just this, they’re the same for normal Unix files. (I have no idea what happens in the case of sparse files, abnormal files, devices, etc.) And “safer” is in the eye of the beholder... if you’re outputting diagnostics for immediate human consumption, a gist is less likely to ruin formatted output. Trey
Re: Using raku/perl6 as unix "cat"....
On Mon, Jan 20, 2020 at 02:59 William Michels via perl6-users < perl6-users@perl.org> wrote: > Hi Yary (and Todd), > > Thank you both for your responses. Yary, the problem seems to be with > "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 > actually returns fewer lines with "for" than it did with "while": If you want to do line-oriented input, use `.lines` with `for`; it returns something `for` can iterate over. `.get` is the wrong tool for the job; it returns a single line. To get all lines, you must call it repeatedly; so you’d need to run an infinite loop (either using `loop`, `while` with a true value, or `for` with a never-ending sequence) and break out when you reach the end of the file. This _works_ just fine, but it’s very weird: perl6 -e 'loop { last if $*IN.eof; $_ = $*IN.get; .say }' < abc_def.text You’d typically use “get” in a case where you’re doing low-level I/O, not for writing a POSIX-like line-oriented filter. `for lines -> $line { ... }` is what you want in programs, but `.say for lines` (or put instead of say, they’re equivalent here) in a one-liner. >
Re: Using raku/perl6 as unix "cat"....
On Tue, 21 Jan 2020 at 03:12, Brad Gilbert wrote: > .say for lines > Since .say calls gist(), would it be not be better/safer to call .put instead? .put for lines -- Norman Gaywood, Computer Systems Officer School of Science and Technology University of New England Armidale NSW 2351, Australia ngayw...@une.edu.au http://turing.une.edu.au/~ngaywood Phone: +61 (0)2 6773 2412 Mobile: +61 (0)4 7862 0062 Please avoid sending me Word or Power Point attachments. See http://www.gnu.org/philosophy/no-word-attachments.html
Re: Using raku/perl6 as unix "cat"....
.say for lines On Mon, Jan 20, 2020 at 1:59 AM William Michels via perl6-users < perl6-users@perl.org> wrote: > Hi Yary (and Todd), > > Thank you both for your responses. Yary, the problem seems to be with > "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 > actually returns fewer lines with "for" than it did with "while": > > [1]mydir$ cat testthis_abc_def.txt > a > b > c > > d > e > f > [2]mydir$ perl6 -e '.say while $_ = $*IN.get;' < testthis_abc_def.txt > a > b > c > [3]mydir$ perl6 -e '.say for $_ = $*IN.get;' < testthis_abc_def.txt > a > > I assumed 'get' was looking at EOF but that's incorrect--apparently > 'get' acts more like a glorified call to lines[0], reading one line of > an input file and then stopping. I can read my whole test file by > explicitly testing for EOF (note the redeclaration of $*IN below). > Otherwise, my best course of action is replacing 'get' with 'lines', > and using either 'while' or 'for' ('.say for $*IN.lines;' replicates > 'cat' behavior the best): > > [4]mydir$ perl6 -e 'while not($*IN.eof) -> {$*IN.get.say };' < > testthis_abc_def.txt > a > b > c > > d > e > f > [5]mydir$ perl6 -e '.say while $_ =$*IN.lines;' < testthis_abc_def.txt > (a b c d e f) > [6]mydir$ perl6 -e '.say for $_ = $*IN.lines;' < testthis_abc_def.txt > (a b c d e f) > [7]mydir$ perl6 -e '.say for $*IN.lines;' < testthis_abc_def.txt > a > b > c > > d > e > f > > HTH, Bill. > > > > > > On Sat, Jan 18, 2020 at 5:55 PM yary wrote: > > > > "while" is the wrong looping construct for going over file lines, and > that's across a great many computer languages. It will stop when it > encounters a false line- typically an empty line or '0' > > > > Try "for" > > > > -y > > > > > > On Sat, Jan 18, 2020 at 4:45 PM William Michels > wrote: > >> > >> 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 >
Re: Using raku/perl6 as unix "cat"....
Hi Yary (and Todd), Thank you both for your responses. Yary, the problem seems to be with "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 actually returns fewer lines with "for" than it did with "while": [1]mydir$ cat testthis_abc_def.txt a b c d e f [2]mydir$ perl6 -e '.say while $_ = $*IN.get;' < testthis_abc_def.txt a b c [3]mydir$ perl6 -e '.say for $_ = $*IN.get;' < testthis_abc_def.txt a I assumed 'get' was looking at EOF but that's incorrect--apparently 'get' acts more like a glorified call to lines[0], reading one line of an input file and then stopping. I can read my whole test file by explicitly testing for EOF (note the redeclaration of $*IN below). Otherwise, my best course of action is replacing 'get' with 'lines', and using either 'while' or 'for' ('.say for $*IN.lines;' replicates 'cat' behavior the best): [4]mydir$ perl6 -e 'while not($*IN.eof) -> {$*IN.get.say };' < testthis_abc_def.txt a b c d e f [5]mydir$ perl6 -e '.say while $_ =$*IN.lines;' < testthis_abc_def.txt (a b c d e f) [6]mydir$ perl6 -e '.say for $_ = $*IN.lines;' < testthis_abc_def.txt (a b c d e f) [7]mydir$ perl6 -e '.say for $*IN.lines;' < testthis_abc_def.txt a b c d e f HTH, Bill. On Sat, Jan 18, 2020 at 5:55 PM yary wrote: > > "while" is the wrong looping construct for going over file lines, and that's > across a great many computer languages. It will stop when it encounters a > false line- typically an empty line or '0' > > Try "for" > > -y > > > On Sat, Jan 18, 2020 at 4:45 PM William Michels wrote: >> >> 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
Re: Using raku/perl6 as unix "cat"....
On 2020-01-18 17:54, yary wrote: "while" is the wrong looping construct for going over file lines, and that's across a great many computer languages. It will stop when it encounters a false line- typically an empty line or '0' Try "for" -y Hi William, I don't know if this will help you, but I have a sitution when I read thousands of lines into a string. The lines have CR/LF in them. To read through the string, line by line, I use Yary's suggestion with the following addition: for $x.lines -> $Line { magic stuff ; } -T
Re: Using raku/perl6 as unix "cat"....
"while" is the wrong looping construct for going over file lines, and that's across a great many computer languages. It will stop when it encounters a false line- typically an empty line or '0' Try "for" -y On Sat, Jan 18, 2020 at 4:45 PM William Michels wrote: > 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 >
Using raku/perl6 as unix "cat"....
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