Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raku, where is the Perl's 5 `<>`?

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

like image 897
user2925716 Avatar asked May 31 '21 16:05

user2925716


People also ask

Will there be Perl 6?

Raku is a member of the Perl family of programming languages. Formerly known as Perl 6, it was renamed in October 2019. Raku introduces elements of many modern and historical languages.

When was Perl 6 released?

Perl 6 First Official Release. Released on December 24, 2015, Perl 6 Version 1.0 is also Perl 6.

What is Perl called now?

Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was officially changed to Raku in October 2019.

Is Raku a compiled language?

When you load a module for the first time, Rakudo compiles it into bytecode.

What is Raku Perl 6?

Formerly known as Perl 6, it was renamed in October 2019. Raku introduces elements of many modern and historical languages. Compatibility with Perl was not a goal, though a compatibility mode is part of the specification.

How is Rakudo written in Not Quite Perl 6?

There is a layer between Raku and the virtual machines called Not Quite Perl 6, or NQP, which implements Raku rules for parsing Raku, as well as an abstract syntax tree and backend-specific code generation. Large portions of Rakudo are written in Raku itself, or in its subset NQP.

What is Raku?

Formerly known as Perl 6, it was renamed in October 2019. Raku introduces elements of many modern and historical languages. Compatibility with Perl was not a goal, though a compatibility mode is part of the specification. The design process for Raku began in 2000. In Perl 6, we decided it would be better to fix the language than fix the user.

Is Perl 6 backward compatible with Perl 5?

An implication of these goals was that Perl 6 would not have backward compatibility with the existing Perl codebase. This meant that some code which was correctly interpreted by a Perl 5 compiler would not be accepted by a Perl 6 compiler.


Video Answer


2 Answers

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.

like image 100
raydiak Avatar answered Oct 21 '22 02:10

raydiak


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

like image 36
jubilatious1 Avatar answered Oct 21 '22 01:10

jubilatious1