perl6

Mixing roles into callables

帅比萌擦擦* 提交于 2019-12-10 14:49:59
问题 Theoretically, you can mix in a role into an object in runtime. So I am trying to do this with a function: my &random-f = -> $arg { "Just $arg" }; say random-f("boo"); role Argable { method argh() { self.CALL-ME( "argh" ); } } &random-f does Argable; say random-f.argh; Within the role, I use self to refer to the already defined function, and CALL-ME to actually call the function within the role. However, this results in the following error: Too few positionals passed; expected 1 argument but

How to open a file handle on a string in Perl 6?

回眸只為那壹抹淺笑 提交于 2019-12-10 13:35:06
问题 In Perl 5, I can open a filehandle on string like this: open my $kfh, "<", \$message->payload; I have a scenario that uses string as a filehandle and passes it to the open method: my $fh = new IO::Zlib; open my $kfh, "<", \$message->payload; if($fh->open($kfh, 'rb')){ print <$fh>; $fh->close; } where $message->payload is read from Kafka, and the content is a byte array. raiph had a similar question, but it didn't answer my question. So I want to know how to open a filehandle on a string in

How can I declare and use a Perl 6 module in the same file as the program?

 ̄綄美尐妖づ 提交于 2019-12-10 13:34:40
问题 Sometimes I don't want multiples files, especially if I'm playing around with an idea that I want to keep a nice structure that can turn into something later. I'd like to do something like this: module Foo { sub foo ( Int:D $number ) is export { say "In Foo"; } } foo( 137 ); Running this, I get a compilation error (which I think is a bit odd for a dynamic language): ===SORRY!=== Error while compiling /Users/brian/Desktop/multi.pl Undeclared routine: foo used at line 9 Reading the Perl 6

perl6 - Converting data in a Blob into a Num

不问归期 提交于 2019-12-10 13:18:09
问题 I've got some bytes in a blob, an immutable buffer for binary data and I am looking for a way to convert what it holds into a floating point data structure, Num, since it is the class that fits all those 3 formats that could be in the $blob IEEE Float IEEE Double IEEE Long Double What would be the best way of doing that conversion? 回答1: Import NativeCall , perform a cast to a pointer of desired type and dereference the result: use NativeCall; nativecast(Pointer[num32], $blob).deref; 来源: https

Does Perl 6 performance suffer by using rationals for decimal numbers

旧时模样 提交于 2019-12-10 13:14:07
问题 I understand that Perl 6 implements decimals as rationals wherever possible in order to avoid floating point issues that are present in most other languages. Has anybody done benchmarking or have an understanding of the performance penalty of doing this? 回答1: Does Perl 6 performance suffer by using rationals for decimal numbers I think the most useful overall answer is "no, not really, but let me elaborate a bit". If you would prefer to not care about accuracy or performance If you'd like P6

Why is Perl 6's unwrap method a method of Routine?

☆樱花仙子☆ 提交于 2019-12-10 13:13:08
问题 There's an unwrap method, but the way it seems I'm supposed to use it isn't the way it should be used. It seems like it should either be a standalone routine or a method in a different class. What am I missing? It appears that it doesn't care what its invocant is as long as it gets the right Routine::WrapHandle thingy as an argument. In this example, I wrap a subroutine and get back a WrapHandle : sub add-two-numbers ( $n, $m ) { $n + $m } sub do-some-stuff ( $n, $m, $o ) { add-two-numbers(

No such method <name> for invocant of type <class>

陌路散爱 提交于 2019-12-10 13:09:45
问题 I've created a class which contains multi definitions for function overloading, however when I try to call the class and the overloaded method, it throws an error. A working example which can be run to produce this error is shown below: class Test { multi test(@data) { return test(@data, @data.elems); } multi test(@data, $length) { return 0; } } my @t = 't1', 't2', 't3'; say Test.test(@t); Error: No such method 'test' for invocant of type 'Test'. Did you mean any of these? List Set gist list

Assigning a value to the attribute of a mixed-in role

不打扰是莪最后的温柔 提交于 2019-12-10 13:07:01
问题 I'm trying to work an example that uses the Enumeration role in Perl 6 (as part of fixing doc issue Enumeration role is not documented). I came up with this simple example: class DNA does Enumeration { my $DNAindex = 0; my %pairings = %( A => "T", T => "A", C => "G", G => "T" ); method new( $base-pair where "A" | "C" | "G" | "T" ) { self.bless( key => $base-pair, value => %pairings{$base-pair}, index => 33); } multi method gist(::?CLASS:D:) { return "$!key -> $!value with $!index"; } } for <A

Multiple MAIN signatures

丶灬走出姿态 提交于 2019-12-10 12:57:37
问题 I have a package with multiple main and I want to define several options: My code is something like this: package Perl6::Documentable::CLI { proto MAIN(|) is export {*} my %*SUB-MAIN-OPTS = :named-everywhere; multi MAIN( "setup" ) { ... } multi MAIN ( "start" , Str :$topdir = "doc", Bool :v(:verbose($v)) = False ) { ... } But when I try to actually execute it with: perl6 -Ilib bin/documentable start -v --topdir=ss It outputs this line: Usage: bin/documentable [--topdir=<Str>] [-v|--verbose]

Creating a Maybe type in Perl 6

时光怂恿深爱的人放手 提交于 2019-12-10 12:50:00
问题 I have a lot of functions that can fail, but also have a return type defined in their signature. Since I like defining the types of variables whenever possible, I want to define a Maybe subset to use for this. What I came up with is this: subset Maybe is export of Mu where Mu | Failure; The problem with this is Failure is a subclass of Mu , so this will match anything and everything, when what I really want is to be able to match one specific type along with Failure dynamically. My next