perl6

How do you append to a file in Perl 6?

二次信任 提交于 2019-12-08 18:48:23
问题 I was trying this and a few other things but it truncates the file each time: my $file = 'primes.txt'; sub MAIN ( Int:D $low, Int:D $high where * >= $low ) { unless my $fh = open $file, :w, :append { die "Could not open '$file': {$fh.exception}"; } for $low .. $high { $fh.put: $_ if .is-prime; } } Changing this to open $file, :a also seems to truncate the file. This is 2018.04 on macOS. 回答1: Perl6 &open semantics are based on POSIX, with the following mapping: :mode<ro> --> O_RDONLY :mode<wo>

How to read gz file line by line in Perl6

牧云@^-^@ 提交于 2019-12-08 17:26:22
问题 I'm trying to read a huge gz file line by line in Perl6. I'm trying to do something like this my $file = 'huge_file.gz'; for $file.IO.lines -> $line { say $line; } But this give error that I have a malformed UTF-8. I can't see how to get this to read gzipped material from the help page https://docs.perl6.org/language/unicode#UTF8-C8 or https://docs.perl6.org/language/io I want to accomplish the same thing as was done in Perl5: http://blog-en.openalfa.com/how-to-read-and-write-compressed-files

Declaring an array inside a Perl 6 NativeCall CStruct

会有一股神秘感。 提交于 2019-12-08 17:24:57
问题 Is there any way to declare an array of objects inside a CStruct? struct my_struct { int foo; int bar; char somestring[80]; }; class My::Struct is repr('CStruct') { has int32 $.foo; has int32 $.bar; ??? } A CArray[uint8] would be a char * pointer, not actually reserving space inside the struct. Instead of My::Struct.new , I could probably make the memory myself (instead of My::Struct.new() , I use a buf8.allocate(xxx) and keep a handle so the GC doesn't reap it, nativecast it to My::Struct),

More concise way to build a configuration class using environment variables?

点点圈 提交于 2019-12-08 17:22:34
问题 I have a class Configuration that reads in environment variables: class Configuration { has $.config_string_a; has $.config_string_b; has Bool $.config_flag_c; method new() { sub assertHasEnv(Str $envVar) { die "environment variable $envVar must exist" unless %*ENV{$envVar}:exists; } assertHasEnv('CONFIG_STRING_A'); assertHasEnv('CONFIG_STRING_B'); assertHasEnv('CONFIG_FLAG_C'); return self.bless( config_string_a => %*ENV{'CONFIG_STRING_A'}, config_string_b => %*ENV{'CONFIG_STRING_B'}, config

perl6 interpolate array in match for AND, OR, NOT functions

孤者浪人 提交于 2019-12-08 17:12:21
问题 I am trying to re-do my program for match-all, match-any, match-none of the items in an array. Some of the documentations on Perl6 don't explain the behavior of the current implementation (Rakudo 2018.04) and I have a few more questions. (1) Documentation on regex says that interpolating array into match regex means "longest match"; however, this code does not seem to do so: > my $a="123 ab 4567 cde"; 123 ab 4567 cde > my @b=<23 b cd 567>; [23 b cd 567] > say (||@b).WHAT (Slip) > say $a ~~ m/

Concurrency, react-ing to more than one supply at a time

流过昼夜 提交于 2019-12-08 17:06:18
问题 Please consider the code below. Why is the output of this is "BABABA" and not "AAABAA" / "AABAAAB"? Shouldn't the two supplies run in parallel and the whenever fire immedeatly when there is an event in any of them? my $i = 0; my $supply1 = supply { loop { await Promise.in(3); done if $i++> 5; emit("B"); } }; my $supply2 = supply { loop { await Promise.in(1); done if $i++> 5; emit("A"); } }; react { #whenever Supply.merge($supply1, $supply2) -> $x { $x.print } whenever $supply1 -> $x { $x

Differences between .Bool, .so, ? and so

半腔热情 提交于 2019-12-08 17:00:20
问题 I’m trying to figure out what the differences are between the above-mentioned routines, and if statements like say $y.Bool; say $y.so; say ? $y; say so $y; would ever produce a different result. So far the only difference that is apparent to me is that ? has a higher precedence than so . .Bool and .so seem to be completely synonymous. Is that correct and (practically speaking) the full story? 回答1: What I've done to answer your question is to spelunk the Rakudo compiler source code. As you

Is there a way to deal with Unix sockets in Perl 6?

天大地大妈咪最大 提交于 2019-12-08 16:47:35
问题 I'd like to communicate with mpv using Unix sockets, but Perl 6 doesn't offer any high level interface for doing that. So I thought I could write a little module for that, started digging deeper, found the implementation of IO::Socket::INET , and learned about the NQP ops nqp::socket and nqp::connect . However, I couldn't find any mention of those in the NQP operations list, and they seem to be quite distinct from the traditional BSD socket API. Hence I'd like to ask: are those two able to

Perl 6 error message: Malformed UTF-8 in block <unit>

大憨熊 提交于 2019-12-08 15:58:32
问题 I'm trying to read a downloaded html-file my $file = "sn.html"; my $in_fh = open $file, :r; my $text = $in_fh.slurp; and I get the following error message: Malformed UTF-8 in block <unit> at prog.p6 line 10 How to avoid this and get access to the file's contents? 回答1: If you do not specify an encoding when opening a file, it will assume utf8 . Apparently, the file that you wish to open, contains bytes that cannot be interpreted as UTF-8. Hence the error message. Depending on what you want to

Filtering elements matching two regexes in Perl 6

假如想象 提交于 2019-12-08 15:53:51
问题 Something is going on here that I don't quite understand. > my @arr = <ac bc abc> > @arr.grep: (( * ~~ /a/ ) && ( * ~~ /b/ )) (bc abc) But > @arr.grep(* ~~ /a/).grep(* ~~ /b/) (abc) What's the reason? 回答1: You've come up with perfectly cromulent solutions. Another would be: my @arr = <ac bc abc> @arr.grep: { $_ ~~ /a/ && $_ ~~ /b/ } (abc) The rest of this answer just explains the problem. The problem in this question is a more complicated version of the problem covered at WhateverStar &&