7

I'm further playing with Raku's CommaIDE and I wanna print a binary file line by line. I've tried this, but it doesn't work:

for "G.txt".IO.lines -> $line {
    say $_;
    }

How shall I fix it ? It's obviously incorrect.

EDIT this doesn't work either, see the snippet bellow

for "G.txt".IO.lines -> $line {
    say $line;
    }

enter image description here

Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
user2925716
  • 949
  • 1
  • 6
  • 13
  • 1
    for "G.txt".IO.lines -> $line { say $line; } – LuVa May 31 '21 at 18:01
  • @ValleLukas Still doesn't work well,please see my **EDIT** :-( – user2925716 May 31 '21 at 18:16
  • @ValleLukas The problem is that `G.txt` contains non-ascii characters. – user2925716 May 31 '21 at 18:22
  • 1
    This error is not related to your code or the file imho. Looks like something is still wrong with Comma or the Raku installation. If the error where about the file you would see something like "Malformed UTF-8 near bytes ... in ..." – Holli May 31 '21 at 19:39
  • Hi @ValleLukas code posted here in the comments works fine on my Rakudo 2020.10 install, (tested outside Comma_IDE). – jubilatious1 Jun 01 '21 at 16:54
  • @jubilatious1 How can I run a "Perl5" code from **Raku**, more specifically from `CommaIDE` ? – user2925716 Jun 01 '21 at 17:34
  • @user2925716 are you talking about using Raku's `Inline::Perl5` module/interface? see https://docs.raku.org/language/5to6-nutshell and https://github.com/niner/Inline-Perl5/ and possibly https://stackoverflow.com/questions/56844396/list-return-from-inlineperl5-gives-a-count-of-items-not-the-list – jubilatious1 Jun 01 '21 at 22:16
  • 2
    Re "*I wanna print a binary file line by line.*", "Binary file" is a catch-all term to mean "not text". Since we're specifically talking about a file that doesn't contain text, it doesn't make sense to talk about lines! – ikegami Jun 08 '21 at 08:49
  • The question title asks "where is the Perl's 5 `<>`?" I arrived here looking for an answer to "raku language equivalent of perl diamond operator?" The question and answers are talking about reading binary files in raku and the Comma IDE. Not a good question title IMHO. – Norman Gaywood Feb 25 '22 at 03:56

2 Answers2

6

You're showing us h.raku but Comma is giving you an error regarding c.raku, which is some other file in your Comma project.

It looks like you're working with a text file, not binary. Raku makes a clear distinction here: a text file is treated as text, regardless of encoding. If it's UTF-8, using .lines as you are now should work just fine because that's the default. If it's some other encoding, you can call .lines(:enc<some-other-encoding>). If it's truly binary, then the concept of "lines" really has no meaning, and you want something more like .slurp(:bin), which will give you a Buf[uint8] for working on the byte level.

raydiak
  • 61
  • 1
5

The question specifically refers to reading a binary file, for which reading line-wise may (or may not) make sense--depending on the file.

Here's code to read a binary file straight from the docs (using class IO::CatHandle):

~$ raku -e '(my $f1 = "foo".IO).spurt: "A\nB\nC\n"; (my $f2 = "foo"); with IO::CatHandle.new: $f2 {.encoding: Nil; .slurp.say;};'
    Buf[uint8]:0x<41 0A 42 0A 43 0A>

Compare to reading the file with default encoding (utf8):

~$ raku -e '(my $f1 = "foo".IO).spurt: "A\nB\nC\n"; (my $f2 = "foo"); with IO::CatHandle.new: $f2 {.slurp.say;};'
    A
    B
    C

See: https://docs.raku.org/routine/encoding

Note: the read method uses class IO::Handle which reads binary by default. So the code is simply:

~$ raku -e '(my $file1 = "foo".IO).spurt: "A\nB\nC\n"; my $file2 = "foo".IO; given $file2.open { .read.say; .close;};'
Buf[uint8]:0x<41 0A 42 0A 43 0A>

See: https://docs.raku.org/type/IO::Handle#method_read

For further reading, see discussion of Perl5's <> diamond-operator-equivalent in Raku:
https://docs.raku.org/language/5to6-nutshell#while_until

...and some (older) mailing-list discussion of the same:
https://www.nntp.perl.org/group/perl.perl6.users/2018/11/msg6295.html

Finally, the docs refer to writing a mixed utf8/binary file here (useful for further testing): https://docs.raku.org/routine/encoding#Examples

jubilatious1
  • 1,999
  • 10
  • 18