raku

What happens when different thread schedulers are used in the same react block?

独自空忆成欢 提交于 2019-12-05 16:34:15
问题 This is a follow up question to is whenever signal() in react block order dependent? . The following code using the default scheduler $*SCHEDULER lets the user exit immediately by pressing CTRL-C in the following event loop: use v6; my %scheduler; my $use-default-scheduler = True; if $use-default-scheduler { %scheduler = scheduler => $*SCHEDULER; } react { whenever signal(SIGINT, |%scheduler) { say "Got signal"; exit; } whenever Supply.from-list($*IN.lines, |%scheduler) { say "Got line"; exit

Which context confuses this Perl 6 zip operator?

无人久伴 提交于 2019-12-05 02:03:33
Consider this program where I create a hash. I want to then change two values in it: my $hash = %( wallet => 100, gave => 0, received => 0, ); for ^1 { $hash<wallet gave> Z+= <-1 1> }; dd $hash; Like this, the last line of for doesn't do anything and there is no warning. The hash is unchanged: Hash $hash = ${:gave(0), :received(0), :wallet(100)} Adding another statement changes the behavior: my $hash = %( wallet => 100, gave => 0, received => 0, ); for ^1 { $hash<wallet gave> Z+= <-1 1>; True }; dd $hash; Now the inplace edit does its thing, but there's a warning (although I dispute "useless"

how do I create a stand-alone executable with perl 6?

末鹿安然 提交于 2019-12-05 01:37:08
The OLD Perl 6 faq said: "Rakudo, a Perl 6 compiler based on Parrot, allows compilation to bytecode, and a small wrapper exists that can pack up a bytecode file and parrot into a single executable." So, it was possible to create a stand-alone executable, but I can not find any docs explaining how to go about this, or if it's still possible. So, I turn to you. What is the appropriate set of incantations required to convert Perl 6 code into a stand-alone executable that will work on a system that does not have Perl 6 installed. This is not possible with current Rakudo on MoarVM. There's still

How does a Perl 6 object find a multi method that might be in a parent class or role?

北慕城南 提交于 2019-12-05 01:12:39
Consider this example where a subclass has a multi method with no signature and one with a slurpy parameter: class Foo { multi method do-it { put "Default" } multi method do-it ( Int $n ) { put "Int method" } multi method do-it ( Str $s ) { put "Str method" } multi method do-it ( Rat $r ) { put "Rat method" } } class Bar is Foo { multi method do-it { put "Bar method" } multi method do-it (*@a) { put "Bar slurpy method" } } Foo.new.do-it: 1; Foo.new.do-it: 'Perl 6'; Foo.new.do-it: <1/137>; Foo.new.do-it; put '-' x 10; Bar.new.do-it: 1; Bar.new.do-it: 'Perl 6'; Bar.new.do-it: <1/137>; Bar.new.do

How many ways are there to describe the Fibonacci sequence in Perl 6?

眉间皱痕 提交于 2019-12-04 07:52:29
问题 I've been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence. I will start this off with the three from masak's journal: my @fibs := (0, 1, -> $a, $b { $a + $b } ... *); my @fibs := (0, 1, { $^a + $^b } ... *); my @fibs := (0, 1, *+* ... *); I was thinking something like this would also work, but I think I have the syntax wrong: my @fibs := (0, 1, (@fibs Z+ @fibs[1..*])); Something there is

pointer to constructor to a class in perl6

拥有回忆 提交于 2019-12-04 03:19:11
问题 I am trying to write some classes with Perl 6 just for testing out Perl 6 classes and methods. Here is the code: class human1 { method fn1() { print "#from human1.fn1\n"; } } class human2 { method fn1() { print "#from human2.fn1\n"; } } my $a = human1.new(); my $b = human2.new(); $a.fn1(); $b.fn1(); print "now trying more complex stuff\n"; my $hum1_const = &human1.new; my $hum2_const = &human2.new; my $c = $hum2_const(); $c.fn1(); Essentially I want to be able to use either the human1

How do you add a method to an existing class in Perl 6?

自古美人都是妖i 提交于 2019-12-03 23:40:00
The Int class has a method is_prime , so I figured, just for giggles, I'd like to add some other methods to Int for some of my hobby projects that do number theory stuff. I thought I could do something like this: class Int { method is-even (Int:D $number ) returns Bool:D { return False if $number % 2; return True; } } say 137.is-even; But that doesn't work: ===SORRY!=== P6opaque: must compose before allocating I don't know if this means that I can't do that or that I'm doing it incorrectly. I could easily make a new class that inherits from Int , but that's not what I'm interested in: class

How does one write custom accessor methods in Perl6?

随声附和 提交于 2019-12-03 23:17:26
How does one write custom accessor methods in Perl6? If I have this class: class Wizard { has Int $.mana is rw; } I can do this: my Wizard $gandalf .= new; $gandalf.mana = 150; Let's say I want to add a little check to a setter in my Perl6 class without giving up the $gandalf.mana = 150; notation (in other words, I don't want to write this: $gandalf.setMana(150); ). The program should die, if it tries to set a negative mana. How do I do this? The Perl6 documentation just mentions it is possible to write custom accessors, but does not say how. You can get the same accessor interface that saying

How do I invoke a Java method from perl6

本小妞迷上赌 提交于 2019-12-03 17:37:10
问题 use java::util::zip::CRC32:from<java>; my $crc = CRC32.new(); for 'Hello, Java'.encode('utf-8') { $crc.'method/update/(B)V'($_); } say $crc.getValue(); sadly, this does not work Method 'method/update/(B)V' not found for invocant of class 'java.util.zip.CRC32' This code is available at the following links. It is the only example I've been able to find Rakudo Perl 6 on the JVM (slides) Perl 6 Advent Calendar: Day 03 – Rakudo Perl 6 on the JVM 回答1: Final answer Combining the code cleanups

How can I use Perl 5 modules from Perl 6?

两盒软妹~` 提交于 2019-12-03 10:39:12
Is the a way to use Perl 5 modules from CPAN from Rakudo Perl 6? For example, how can I use the venerable Perl 5 module, CGI, which hasn't been ported yet, in Perl 6. Update: And what this funky code from some early Perl 6 module: use CGI:from<perl5>; Is the :from<perl5> directive used to evoke some kind of a Perl 5 compatibility layer? Can't seem to find any documentation about it. user7610 Inline::Perl5 Get it from http://modules.perl6.org/ panda install Inline::Perl5 Following example shows how to import and call Perl 5 module Text::Unidecode , "the Unicode transliteration of last resort"