perl6

perl6 'do(file)' equivalent

ぐ巨炮叔叔 提交于 2019-12-05 09:34:21
In perl5 I used to 'do (file)' for configuration files like this: ---script.pl start --- our @conf = (); do '/path/some_conf_file'; ... foreach $item (@conf) { $item->{rules} ... ... ---script.pl end --- ---/path/some_conf_file start --- # arbitrary code to 'fill' @conf @conf = ( {name => 'gateway', rules => [ {verdict => 'allow', srcnet => 'gw', dstnet => 'lan2'} ] }, {name => 'lan <-> lan2', rules => [ {srcnet => 'lan', dstnet => 'lan2', verdict => 'allow', dstip => '192.168.5.0/24'} ] }, ); ---/path/some_conf_file end --- Also Larry Wall's "Programming Perl" also mentions this method: But

how to pass a class method as argument to another method of the class in perl 6

懵懂的女人 提交于 2019-12-05 09:34:16
I have a script like the below. Intent is to have different filter methods to filter a list. Here is the code. 2 3 class list_filter { 4 has @.my_list = (1..20); 5 6 method filter($l) { return True; } 7 8 # filter method 9 method filter_lt_10($l) { 10 if ($l > 10) { return False; } 11 return True; 12 } 13 14 # filter method 15 method filter_gt_10($l) { 16 if ($l < 10) { return False; } 17 return True; 18 } 19 20 # expecting a list of (1..10) to be the output here 21 method get_filtered_list_lt_10() { 22 return self.get_filtered_list(&{self.filter_lt_10}); 23 } 24 25 # private 26 method get

index() error message incorrect for third parameter?

大城市里の小女人 提交于 2019-12-05 08:14:26
sub count-a { my $word = "banana"; my $count = 0; my $foo; # Source of error: $foo intentionally not given a value. # The value ought to be zero. while True { $foo = index $word, "a", $foo; last unless $foo.defined; $foo++; $count++ } return $count; } say count-a; Is this error message wrong? Cannot resolve caller index(Str: Str, Any); none of these signatures match: (Str:D $: Cool:D $needle, *%_) (Str:D $: Str:D $needle, *%_) (Str:D $: Cool:D $needle, Cool:D $pos, *%_) (Str:D $: Str:D $needle, Int:D $pos, *%_) in sub count-a at scrap.p6 line 11 in block <unit> at scrap.p6 line 18 The error

How to remove diacritics in Perl 6

霸气de小男生 提交于 2019-12-05 06:16:18
Two related questions. Perl 6 is so smart that it understands a grapheme as one character, whether it is one Unicode symbol (like ä , U+00E4 ) or two and more combined symbols (like p̄ and ḏ̣ ). This little code my @symb; @symb.push("ä"); @symb.push("p" ~ 0x304.chr); # "p̄" @symb.push("ḏ" ~ 0x323.chr); # "ḏ̣" say "$_ has {$_.chars} character" for @symb; gives the following output: ä has 1 character p̄ has 1 character ḏ̣ has 1 character But sometimes I would like to be able to do the following. 1) Remove diacritics from ä . So I need some method like "ä".mymethod → "a" 2) Split "combined"

how to load Perl5's Data::Printer in Perl6?

橙三吉。 提交于 2019-12-05 04:57:25
I've been trying to load in the Perl5 module Data::Printer into Perl6, but am having a hard time. I asked this earlier, Cannot import Perl5 module using Inline::Perl5 into Perl6 and did get useful advice from @raiph and Elizabeth, but was advised to do another question con@con-VirtualBox:~$ perldoc -lm Data::Printer /usr/local/share/perl/5.26.0/Data/Printer.pm con@con-VirtualBox:~$ perl6 To exit type 'exit' or '^D' > use Inline::Perl5; Nil > use lib:from<Perl5> '/usr/local/share/perl/5.26.0/Data/'; Nil > my @a = 1,2,3,4 [1 2 3 4] > p @a ===SORRY!=== Error while compiling: Undeclared routine: p

What's an “additional tie breaker” for Perl 6 longest token matching?

99封情书 提交于 2019-12-05 04:47:50
The docs for Perl 6 longest alternation in regexes punt to Synopsis 5 to document the rules for longest token matching . There are three rules if different alternatives would match a substring of the same length: The longest declarative prefix breaks the tie The highest specificity breaks the tie "If it's still a tie, use additional tie-breakers." The left most alternation finally wins It's that third rule that I'm curious about. First the way the text is organized makes clear that the behaviour of the implementation must be deterministic (not random). Second - and more important - describing

How can I use “map” inside a “for” loop in Perl 6?

冷暖自知 提交于 2019-12-05 03:57:41
I'm trying to use map inside a for loop, but this code is not working. This code should print 1 and 2 three times. But the code below produces no output. It will only partially work if you uncomment the exit line. Any idea why? I tested it using the current Perl 6 version (Rakudo version 2015.12-79-g4bb47d5 built on MoarVM version 2015.12 and also the Windows version). for (1..3) { map { say $_ }, (1..2); # exit; } Let's take a look at say (map { say $_ }, 1..2).WHAT; This tells us &map returns a Seq , which is a lazy construct. Additionally, the last statement in the body of a for loop is

Perl6: How could I make all warnings fatal?

删除回忆录丶 提交于 2019-12-05 02:48:12
问题 How could I make all warnings in Perl6 fatal, so that the script dies as soon as a warning appears on the screen. CONTROL { when CX::Warn { note $_; exit 1 } } dies more often. This script dies with CONTROL { when CX::Warn { note $_; exit 1 } } but not with use fatal : #!/usr/bin/env perl6 use v6; my @a = 1 .. 4; @a[5] = 6; my @b; for @a -> $i { @b.push( ~$i ); } say "=====\n" x 3; 回答1: Warnings are control exceptions of type CX::Warn that are resumed by default. If you want to change that

Why aren't // and m// exactly synonymous?

雨燕双飞 提交于 2019-12-05 02:38:12
From the examples below, I see that / / and m/ / aren't exactly synonymous, contrary to what I expected. I thought that the only reason to use m/ / instead of / / was that it allows using different delimiters (e.g. m{ } ). Why are they different and why would I want to use one versus the other? I am searching for CSV files in a directory. At first I searched for files ending in csv , thus (all code shown as seen from the Perl 6 REPL): > my @csv_files = dir( test => / csv $ / ); ["SampleSheet.csv".IO] but recently a file ending in Csv showed up. So I tried matching case insensitively: > my @csv

Does Perl 6 have an infinite Int?

牧云@^-^@ 提交于 2019-12-05 02:17:00
I had a task where I wanted to find the closest string to a target (so, edit distance) without generating them all at the same time. I figured I'd use the high water mark technique (low, I guess) while initializing the closest edit distance to Inf so that any edit distance is closer: use Text::Levenshtein; my @strings = < Amelia Fred Barney Gilligan >; for @strings { put "$_ is closest so far: { longest( 'Camelia', $_ ) }"; } sub longest ( Str:D $target, Str:D $string ) { state Int $closest-so-far = Inf; state Str:D $closest-string = ''; if distance( $target, $string ) < $closest-so-far {