raku

perl6 How to use junction inside regex interpolation?

南笙酒味 提交于 2021-02-08 13:14:28
问题 Sometimes I have a long list and I would like to check whether a string matches anything in the list. I am trying to interpolate a junction inside a regex. They are all errors. say "12345" ~~ m/ <{ (2,3,4).any }> / Cannot resolve caller MAKE_REGEX(Int, Bool, Bool, Int, PseudoStash); none of these signatures match: say "12345" ~~ m/ $( (2,3,4).any ) / This type cannot unbox to a native string: P6opaque, Junction Does this error message mean that junctions cannot be used inside regex

perl6 How to use junction inside regex interpolation?

拟墨画扇 提交于 2021-02-08 13:14:17
问题 Sometimes I have a long list and I would like to check whether a string matches anything in the list. I am trying to interpolate a junction inside a regex. They are all errors. say "12345" ~~ m/ <{ (2,3,4).any }> / Cannot resolve caller MAKE_REGEX(Int, Bool, Bool, Int, PseudoStash); none of these signatures match: say "12345" ~~ m/ $( (2,3,4).any ) / This type cannot unbox to a native string: P6opaque, Junction Does this error message mean that junctions cannot be used inside regex

How to define the 'AT-POS' method?

為{幸葍}努か 提交于 2021-02-08 13:10:33
问题 I defined the AT-POS method for a class and exported the [] operator. When I used [] on the instance of that class, however, the compiler ignored the operator defined by me. Here is the code: unit module somelib; class SomeClass is export { method AT-POS(@indices) { say "indices are {@indices.perl}" } } multi postcircumfix:<[ ]> (SomeClass:D $inst, *@indices) is export { $inst.AT-POS(@indices) } #! /usr/bin/env perl6 use v6.c use lib "."; use somelib; my $inst = SomeClass.new; $inst[3, 'hi'];

EXPORTHOW::DECLARE and role-like declaration

最后都变了- 提交于 2021-02-08 12:52:05
问题 I need to have a role with a little bit of special functionality implemented through a Metamodel class of mine inheriting from Metamodel::ParametricRoleHOW . One way to apply it is by using a trait. But I want a nice syntax sugar like we can do with classes: special Mine { ... } Ok, I add the following into my module: my package EXPORTHOW { package DECLARE { constant special = My::Metamodel::SpecialRoleHOW; } } and everything runs smoothly... No, it is not: class Foo does Mine { ... } and I

Mixing Private and Public Attributes and Accessors in Raku

∥☆過路亽.° 提交于 2021-02-08 12:27:12
问题 #Private attribute example class C { has $!w; #private attribute multi method w { $!w } #getter method multi method w ( $_ ) { #setter method warn “Don’t go changing my w!”; #some side action $!w = $_ } } my $c = C.new $c.w( 42 ) say $c.w #prints 42 $c.w: 43 say $c.w #prints 43 #but not $c.w = 44 Cannot modify an immutable Int (43) so far, so reasonable, and then #Public attribute example class C { has $.v is rw #public attribute with automatic accessors } my $c = C.new $c.v = 42 say $c.v

How do you access private methods or attributes from outside the type they belong to?

末鹿安然 提交于 2021-02-07 11:43:06
问题 In some rare cases where this would actually be acceptable, like in unit tests, you may want to get or set the value of a private attribute, or call a private method of a type where it shouldn't be possible. Is it really impossible? If not, how can you do it? 回答1: There are two ways you can access a private method of a type, and one way to get private attributes. All require meta-programming except for the first way to invoke private methods, whose explanation still involves meta-programming

How to mock an imported subroutine when unit-testing a module

ⅰ亾dé卋堺 提交于 2021-01-27 20:29:26
问题 Consider a module which exports a subroutine that connects to the Internet and returns a result: unit module A; sub download is export { "result from internet" # Not the actual implementation, obviously. } And another module which imports and calls that subroutine: use A; # imports &download into this lexical scope unit module B; sub do-something is export { download().uc ~ "!!" # Does something which involves calling &download } Now I'd like to write unit tests for module B . But I don't

Filtering with regex and .contains in Perl 6

允我心安 提交于 2021-01-27 07:11:11
问题 I often have to filter elements of an array of strings, containing some substring (e.g. one character). Since it can be done either by matching a regex or with .contains method, I've decided to make a small test to be sure that .contains is faster (and therefore more appropriate). my @array = "aa" .. "cc"; my constant $substr = 'a'; my $time1 = now; my @a_array = @array.grep: *.contains($substr); my $time2 = now; @a_array = @array.grep: * ~~ /$substr/; my $time3 = now; my $time_contains =

Filtering with regex and .contains in Perl 6

谁都会走 提交于 2021-01-27 07:09:56
问题 I often have to filter elements of an array of strings, containing some substring (e.g. one character). Since it can be done either by matching a regex or with .contains method, I've decided to make a small test to be sure that .contains is faster (and therefore more appropriate). my @array = "aa" .. "cc"; my constant $substr = 'a'; my $time1 = now; my @a_array = @array.grep: *.contains($substr); my $time2 = now; @a_array = @array.grep: * ~~ /$substr/; my $time3 = now; my $time_contains =

Can one perl6 module conditionally 'use' another perl6 module?

一曲冷凌霜 提交于 2021-01-27 06:04:46
问题 Is there a sensible way to have one perl6 module check for the presence of another perl6 module and to 'use' it if and only if it is installed? Something like this... module Polygons; if $available { use Measure; #only if Measure is installed } class Rectangle is export { has $.width; has $.height; method area { $!width * $!height; #provides operator overload for Measure * Measure } } #==================== module Measure; class Measure is export { has $.value; has $.unit; method Real { $