Perl is one of the oldest programming language which is used till now exhaustively. Major usescases of the Perl are web development, system administration, GUI development, networking and more.

Popular New Releases in Perl

diff-so-fancy

v1.4.1

cloc

1.90

MySQLTuner-perl

Version 1.8.3

pgbadger

Version 11.8

sqitch

Sqitch v1.2.1

Popular Libraries in Perl

diff-so-fancy

by so-fancy doticonperldoticon

star image 14424 doticonMIT

Good-lookin' diffs. Actually… nah… The best-lookin' diffs. :tada:

cloc

by AlDanial doticonperldoticon

star image 12261 doticonGPL-2.0

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.

FlameGraph

by brendangregg doticonperldoticon

star image 11766 doticon

Stack trace visualizer

gitolite

by sitaramc doticonperldoticon

star image 7914 doticonGPL-2.0

Hosting git repositories -- Gitolite allows you to setup git hosting on a central server, with very fine-grained access control and many (many!) more powerful features.

MySQLTuner-perl

by major doticonperldoticon

star image 7425 doticonGPL-3.0

MySQLTuner is a script written in Perl that will assist you with your MySQL configuration and make recommendations for increased performance and stability.

nikto

by sullo doticonperldoticon

star image 5025 doticonGPL-2.0

Nikto web server scanner

Expose

by Jack000 doticonperldoticon

star image 4202 doticonMIT

A simple static site generator for photoessays

sicp-pdf

by sarabander doticonperldoticon

star image 3910 doticon

SICP PDF with Texinfo and LaTeX source

EQGRP

by x0rz doticonperldoticon

star image 3704 doticon

Decrypted content of eqgrp-auction-file.tar.xz

Trending New libraries in Perl

slipstream

by samyk doticonperldoticon

star image 1568 doticon

NAT Slipstreaming allows an attacker to remotely access any TCP/UDP services bound to a victim machine, bypassing the victim’s NAT/firewall, just by the victim visiting a website

the-bastion

by ovh doticonperldoticon

star image 1052 doticonNOASSERTION

Authentication, authorization, traceability and auditability for SSH accesses.

wireguard-vyatta-ubnt

by WireGuard doticonperldoticon

star image 744 doticonGPL-3.0

WireGuard for Ubiquiti Devices

checkinpanel

by Oreomeow doticonperldoticon

star image 735 doticonMIT

一个主要运行在 𝐞𝐥𝐞𝐜𝐕𝟐𝐏 或 𝐪𝐢𝐧𝐠𝐥𝐨𝐧𝐠 等定时面板,同时支持系统运行环境的签到项目(环境:𝑷𝒚𝒕𝒉𝒐𝒏 3.8+ / 𝑵𝒐𝒅𝒆.𝒋𝒔 10+ / 𝑩𝒂𝒔𝒉 4+ / 𝑶𝒑𝒆𝒏𝑱𝑫𝑲8 / 𝑷𝒆𝒓𝒍5)

mmdb_china_ip_list

by alecthw doticonperldoticon

star image 643 doticonMIT

Geoip MaxMind Database for china ip list! This is also an example of generating MaxMind Database!

.dotfiles

by ThePrimeagen doticonperldoticon

star image 611 doticon

SARS-CoV-2_Sequencing

by CDCgov doticonperldoticon

star image 312 doticonApache-2.0

A collection of sequencing protocols and bioinformatic resources for SARS-CoV-2 sequencing.

fetch-master-6000

by anhsirk0 doticonperldoticon

star image 192 doticonGPL-3.0

Simple Dilbert themed system info-fetching tool

pipe-viewer

by trizen doticonperldoticon

star image 180 doticonArtistic-2.0

A lightweight YouTube client for Linux (fork of straw-viewer)

Top Authors in Perl

1

gitpan

370 Libraries

star icon448

2

tokuhirom

268 Libraries

star icon2105

3

rjbs

248 Libraries

star icon1298

4

foswiki

211 Libraries

star icon296

5

bestpractical

209 Libraries

star icon1533

6

redhat/centos-stream/src

178 Libraries

star icon0

7

bingos

172 Libraries

star icon439

8

perlancar

155 Libraries

star icon215

9

briandfoy

152 Libraries

star icon576

10

rafl

136 Libraries

star icon562

1

370 Libraries

star icon448

2

268 Libraries

star icon2105

3

248 Libraries

star icon1298

4

211 Libraries

star icon296

5

209 Libraries

star icon1533

7

172 Libraries

star icon439

8

155 Libraries

star icon215

9

152 Libraries

star icon576

10

136 Libraries

star icon562

Trending Kits in Perl

No Trending Kits are available at this moment for Perl

Trending Discussions on Perl

Escaping metacharacters in a Raku regex (like Perl's quotemeta() or \Q...\E)?

RAKUDO_RAKUAST=1 raku --target=ast is not yet available

What Raku regex modifier makes a dot match a newline (like Perl's /s)?

How can I implement 2d subscripts via AT-POS for different classes?

To determine whether the current loop iteration is the last one or not

The inserted conditional code regex for Raku

How can the Raku behavior on capturing group in alternate be the same as Perl

Difference in Perl regex variable $+{name} and $-{name}

Raku last on non-loops

Can Raku range operator on strings mimic Perl's behaviour?

QUESTION

Escaping metacharacters in a Raku regex (like Perl's quotemeta() or \Q...\E)?

Asked 2022-Mar-29 at 23:38

How can I escape metacharacters in a Raku regex the way I would with Perl's quotemeta function (\Q..\E)?

That is, the Perl code

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5

treats each of ., *, and ? as metacharacters and thus prints The big bad wolf jumped over the lazy dog. But if I change the second-to-last line to $sentence =~ s{\Q$substring\E}{big bad wolf};, then Perl treats .*? as literal characters and thus prints The quick brown fox jumped over the lazy dog.

How can I treat characters literally in a Raku regex?

ANSWER

Answered 2022-Feb-10 at 00:03
Your question's answer:

You can treat characters in a Raku regex literally by surrounding them with quotes (e.g., '.*?') or by using using regular variable interpolation (e.g., $substring inside the regex where $substring is a string contaning metacharacters).

Thus, to translate the Perl program with \Q...\E from your question into Raku, you could write:

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9

This would treat .*? as literal characters, not metacharacters. If you wanted to avoid interpolation with literal text rather than a variable, you could change the substitution regex to s/quick '.*?' fox/big bad wolf/. Conversely, if you want to use the $substring variable as part of a regex (that is, if you do want .*? to be metacharacters) you'd need to to change the substitution regex to s/<$substring>/big bad wolf/. For more details, you can consult the Rexex interpolation docs.

How you could have found this answer without waiting for SO

What should you do when you don't know how to do something in Raku? Asking either on the IRC channel or here on Stack Overflow is an option – and asking a clear Q on SO has the benefit of making the answer more searchable for anyone else who has the same question in the future.

But both IRC and SO are asynchronous – so you'll probably need to wait a bit for an answer. There are other ways that folks interested in Raku frequently get good/great answers to their questions more easily and quickly than they could from IRC/SO, and the remainder of this answer provides some guidance about these ways. (I've numbered the steps in the general order I'd recommend, but there's no reason you need to follow that order).

Easily get better answers more quickly than asking SO Qs Step -1: Let Raku answer the question for you

Raku strives to have awesome error messages, and sometimes you'll be lucky enough to try something in a way that doesn't work but where Raku can tell what you were trying to do.

In those cases, Raku will just tell you how to do what you wanted to do. And, in fact, \Q...\E is one such case. If you'd tried to do it the Perl way

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9/\Q$substring\E/
10

you'd have gotten the same answer I gave above (use $substring or quotes) in the form of the following error message:

1my $sentence = 'The quick brown fox jumped over the lazy dog';
2my $substring = 'quick.*?fox';
3$sentence =~ s{$substring}{big bad wolf};
4print $sentence
5my $sentence = 'The quick brown fox jumped over the lazy dog';
6my $substring = 'quick.*?fox';
7$sentence ~~ s/$substring/big bad wolf/;
8print $sentence
9/\Q$substring\E/
10Unsupported use of \Q as quotemeta.  In Raku please use: quotes or
11literal variable match.
12

So, sometimes, Raku will solve the problem for you! But that's not something that will happen all the time and, any time you're tempted to ask a SO question, it's a good bet that Raku didn't answer your question for you. So here are the steps you'd take in that case:

Step 0: check the docs

The first true step should, of course, be to search the Raku docs for anything useful. I bet you did this – the docs currently don't return any relevant results for \Q..\E. In fact, the only true positive match of \Q...\E in those results is from the Perl to Raku guide - in a nutshell: "using String::ShellQuote (because \Q…\E is not completely right) ...". And that's obviously not what you're interested in.

The docs website doesn't always yield a good answer to simple questions. Sometimes, as we clearly see with the \Q...\E case, it doesn't yield any answer at all for the relevant search term.

Step 1: Search Stack Overflow

Again, you probably did this, but it's good to keep in mind: You can limit your SO search questions/answers tagged as related to Raku by adding [raku] to your query. Here, a query of [raku] "\Q...\E" wouldn't have yielded anything relevant – but, thanks to your question, it will in the future :)

Step 2: Archived/historical "spec" docs

Raku's design was written up in a series of "spec" docs written principally by Larry Wall over a 2 decade period.

(The word "specs" is short for "specification speculations". It's both ultra authoritative detailed and precise specifications of the Raku language, authored primarily by Larry Wall himself, and mere speculations -- because it was all subject to implementation. And the two aspects are left entangled, and now out-of-date. So don't rely on them 100% -- but don't ignore them either.)

The "specs", aka design docs, are a fantastic resource. You can search them using google by entering your search terms in the search box at design.raku.org.


A search for \Q...\E lists 7 pages. The only useful match is Synopsis 5: Regexes and Rules ("24 Jun 2002 — \Q$var\E / ..."). If I click it and then do an in-page search for \Q, I get 2 matches that, together, answer your question (at least with respect to variables – they don't mention literal strings):

In Raku / $var / is like a Perl / \Q$var\E /

\Q...\E sequences are gone.

Step 3: IRC chat logs

In this case, searching the design docs answered your question. But what if it hadn't/we didn't understand the answer?

In that case, searching the IRC logs can be a great option (as previously discussed in the Quicker answers section of an answer to a past Q. The IRC logs are an incredibly rich mine of info with outstanding search features. Please read that section for clear general guidance.

In this particular case, if we'd searched for \Q in the old Raku channel, we would have gotten a bunch of useful matches. None of the first few fully answer your question, but several do (or at least make the answer clear) if read in context – but it's the need to read the surrounding context that makes me put searching the IRC logs below the previous steps.

Source https://stackoverflow.com/questions/71057626

QUESTION

RAKUDO_RAKUAST=1 raku --target=ast is not yet available

Asked 2022-Mar-08 at 19:27

It has been almost a year since I saw Jonathan Worthington presenting the new RakuAST in the YouTube video A Raku API to Raku programs the journey so far from TRC 2021. In the video, he showed that we could dump this new RakuAST using RAKUDO_RAKUAST=1 like this:

1RAKUDO_RAKUAST=1 raku --target=ast -e 'say [*] 1..10'
2

I'm using the lasted version of RakudoStar, and yet, the command above still dumps the old QAST tree.

What is happening here?

Why doesn't it show the new RakuAST and how can I compile rakudo the right way so that it does?

For the build, I use the instruction on this page https://rakudo.org/downloads/rakudo/source for Linux with a slight modification because the instructions are slightly outdated. This is what I do instead of what the page tells me to (It worked previously but not anymore):

1RAKUDO_RAKUAST=1 raku --target=ast -e 'say [*] 1..10'
2sudo perl Configure.pl --backends=moar --gen-moar --gen-nqp
3

ANSWER

Answered 2022-Mar-08 at 11:46

You need to checkout and build the rakuast branch of Rakudo. The RakuAST work is still very much in progress, and has not landed in the main branch let.

Source https://stackoverflow.com/questions/71392434

QUESTION

What Raku regex modifier makes a dot match a newline (like Perl's /s)?

Asked 2022-Feb-09 at 23:24

How do I make the dot (.) metacharacter match a newline in a Raku regex? In Perl, I would use the dot matches newline modifier (/s)?

ANSWER

Answered 2022-Feb-07 at 10:40

TL;DR The Raku equivalent for "Perl dot matches newline" is ., and for \Q...\E it's ....

There are ways to get better answers (more authoritative, comprehensive, etc than SO ones) to most questions like these more easily (typically just typing the search term of interest) and quickly (typically seconds, couple minutes tops). I address that in this answer.

What is Raku equivalent for "Perl dot matches newline"?

Just .

If you run the following Raku program:

1/./s
2

you'll see the following error message:

1/./s
2Unsupported use of /s.  In Raku please use: .  or \N.
3

If you type . in the doc site's search box it lists several entries. One of them is . (regex). Clicking it provides examples and says:

An unescaped dot . in a regex matches any single character. ...
Notably . also matches a logical newline \n

My guess is you either didn't look for answers before asking here on SO (which is fair enough -- I'm not saying don't; that said you can often easily get good answers nearly instantly if you look in the right places, which I'll cover in this answer) or weren't satisfied by the answers you got (in which case, again, read on).

In case I've merely repeated what you've already read, or it's not enough info, I will provide a better answer below, after I write up an initial attempt to give a similar answer for your \Q...\E question -- and fail when I try the doc step.

What is Raku equivalent for Perl \Q...\E?

'...', or $foo if the ... was metasyntax for a variable name.

If you run the following Raku program:

1/./s
2Unsupported use of /s.  In Raku please use: .  or \N.
3/\Qfoo\E/
4

you'll see the following error message:

1/./s
2Unsupported use of /s.  In Raku please use: .  or \N.
3/\Qfoo\E/
4Unsupported use of \Q as quotemeta.  In Raku please use: quotes or
5literal variable match.
6

If you type \Q...\E in the doc site's search box it lists just one entry: Not in Index (try site search). If you go ahead and try the search as suggested, you'll get matching pages according to google. For me the third page/match listed (Perl to Raku guide - in a nutshell: "using String::ShellQuote (because \Q…\E is not completely right) ...") is the only true positive match of \Q...\E among 27 matches. And it's obviously not what you're interested in.

So, searching the doc for \S...\E appears to be a total bust.


How does one get answers to a question like "what is the Raku equivalent of Perl's \Q...\E?" if the doc site ain't helpful (and one doesn't realize Rakudo happens to have a built in error message dedicated to the exact thing of interest and/or isn't sure what the error message means)? What about questions where neither Rakudo nor the doc site are illuminating?

SO is one option, but what lets folk interested in Raku frequently get good/great answers to their questions easily and quickly when they can't get them from the doc site because the answer is hard to find or simply doesn't exist in the docs?

Easily get better answers more quickly than asking SO Qs

The docs website doesn't always yield a good answer to simple questions. Sometimes, as we clearly see with the \Q...\E case, it doesn't yield any answer at all for the relevant search term.

Fortunately there are several other easily searchable sources of rich and highly relevant info that often work when the doc site does not for certain kinds of info/searches. This is especially likely if you've got precise search terms in mind such as /s or \Q...\E and/or are willing browse info provided it's high signal / low noise. I'll introduce two of these resources in the remainder of this answer.

Archived "spec" docs

Raku's design was written up in a series of "spec" docs written principally by Larry Wall over a 2 decade period.

(The word "specs" is short for "specification speculations". It's both ultra authoritative detailed and precise specifications of the Raku language, authored primarily by Larry Wall himself, and mere speculations -- because it was all subject to implementation. And the two aspects are left entangled, and now out-of-date. So don't rely on them 100% -- but don't ignore them either.)

The "specs", aka design docs, are a fantastic resource. You can search them using google by entering your search terms in the search box at design.raku.org.


A search for /s lists 25 pages. The only useful match is Synopsis 5: Regexes and Rules ("24 Jun 2002 — There are no /s or /m modifiers (changes to the meta-characters replace them - see below)." Click it. Then do an in-page search for /s (note the space). You'll see 3 matches:

There are no /s or /m modifiers (changes to the meta-characters replace them - see below)

A dot . now matches any character including newline. (The /s modifier is gone.)

. matches an anything, while \N matches an anything except what \n matches. (The /s modifier is gone.) In particular, \N matches neither carriage return nor line feed.


A search for \Q...\E lists 7 pages. The only useful match is again Synopsis 5: Regexes and Rules ("24 Jun 2002 — \Q$var\E / ..."). Click it. Then do an in-page search for \Q. You'll see 2 matches:

In Raku / $var / is like a Perl / \Q$var\E /

\Q...\E sequences are gone.

Chat logs

I've expanded the Quicker answers section of my answer to one of your earlier Qs to discuss searching the Raku "chat logs". They are an incredibly rich mine of info with outstanding search features. Please read that section of my prior answer for clear general guidance. The rest of this answer will illustrate for /s and \Q...\E.


A search for the regex / newline . ** ^200 '/s' / in the old Raku channel from 2010 thru 2015 found this match:

. matches an anything, while \N matches an anything except what \n matches. (The /s modifier is gone.) In particular, \N matches neither carriage return nor line feed.

Note the shrewdness of my regex. The pattern is the word "newline" (which is hopefully not too common) followed within 200 characters by the two character sequence /s (which I suspect is more common than newline). And I constrained to 2010-2014 because a search for that regex of the entire 15 years of the old Raku channel would tax Liz's server and time out. I got that hit I've quoted above within a couple minutes of trying to find some suitable match of /s (not end-of-sarcasm!).


A search for \Q in the old Raku channel was an immediate success. Within 30 seconds of the thought "I could search the logs" I had a bunch of useful matches.

Source https://stackoverflow.com/questions/70996158

QUESTION

How can I implement 2d subscripts via AT-POS for different classes?

Asked 2022-Feb-09 at 23:21

here is an MRE (showing two attempts, with debug left in to be helpful) to try and get 2d subscripting working with AT-POS across a DataFrame that has columns of Series...

1class Series does Positional {
2    has Real @.data = [0.1,0.2,0.3];
3
4    method AT-POS( $p ) { 
5        @!data[$p]
6    }   
7}
8
9class DataFrame does Positional {
10    has Series @.series;
11
12#`[ ATTEMPT #1
13    method AT-POS( $p, $q? ) { 
14        given $q {
15            when Int {                  #say 'Int';
16                @!series[$p][$q]
17            }   
18            when Whatever {             #say '*';
19                @!series[$p].data 
20            }   
21            default {                   #say 'default';
22                @!series[$p] 
23            }   
24
25        }   
26    }   
27#]
28    # ATTEMPT #2
29    method AT-POS(|c) is raw { #`[dd c;] @!series.AT-POS(|c) }
30}
31
32my $df = DataFrame.new( series =&gt; [Series.new xx 3] );
33
34say $df[1].data;            #[0.1 0.2 0.3]
35say $df[1][2];              #0.3
36say $df[0,1];               #(Series.new(data =&gt; $[0.1, 0.2, 0.3]) Series.new(data =&gt; $[0.1, 0.2, 0.3]))
37say $df[1;2];               #0.3
38say $df[1;*];               #got (0.1) ... expected [0.1 0.2 0.3]
39say $df[*;1];               #got (0.2) ... wanted [0.2 0.2 0.2]
40

I already researched on SO and found three related questions here, here and here ... and the attempt #2 in my code seeks to apply @lizmats Answer to the third one. Encouragingly both the attempts in my MRE have the same behaviour. But I cannot workout

  • why the when Whatever {} option is not entered (attempt #1)
  • what the |c is doing - even though I can see that it works in the single subscript case (attempt #2)

I have done some experimenting with multi postcircumfix:<[ ]>( DataFrame:D $df, @slicer where Range|List ) is export {} but this seems to overcomplicate matters.

==================

Great answer from @jonathan building on the original from @Lizmat - thanks! Here is the final, working code:

1class Series does Positional {
2    has Real @.data = [0.1,0.2,0.3];
3
4    method AT-POS( $p ) { 
5        @!data[$p]
6    }   
7}
8
9class DataFrame does Positional {
10    has Series @.series;
11
12#`[ ATTEMPT #1
13    method AT-POS( $p, $q? ) { 
14        given $q {
15            when Int {                  #say 'Int';
16                @!series[$p][$q]
17            }   
18            when Whatever {             #say '*';
19                @!series[$p].data 
20            }   
21            default {                   #say 'default';
22                @!series[$p] 
23            }   
24
25        }   
26    }   
27#]
28    # ATTEMPT #2
29    method AT-POS(|c) is raw { #`[dd c;] @!series.AT-POS(|c) }
30}
31
32my $df = DataFrame.new( series =&gt; [Series.new xx 3] );
33
34say $df[1].data;            #[0.1 0.2 0.3]
35say $df[1][2];              #0.3
36say $df[0,1];               #(Series.new(data =&gt; $[0.1, 0.2, 0.3]) Series.new(data =&gt; $[0.1, 0.2, 0.3]))
37say $df[1;2];               #0.3
38say $df[1;*];               #got (0.1) ... expected [0.1 0.2 0.3]
39say $df[*;1];               #got (0.2) ... wanted [0.2 0.2 0.2]
40class Series does Positional {
41    has Real @.data = [0.1,0.2,0.3];
42
43    method elems {
44        @!data.elems
45    }   
46
47    method AT-POS( |p ) is raw {
48        @!data.AT-POS( |p )
49    }   
50}
51
52class DataFrame does Positional {
53    has Series @.series;
54
55    method elems { 
56        @!series.elems
57    }   
58
59    method AT-POS( |p ) is raw { 
60        @!series.AT-POS( |p )
61    }   
62}
63
64my $df = DataFrame.new( series =&gt; Series.new xx 3 );
65
66say $df[1].data;            #[0.1 0.2 0.3]
67say $df[1][2];              #0.3
68say $df[0,1];               #(Series.new(data =&gt; $[0.1, 0.2, 0.3]) Series.new(data =&gt; $[0.1, 0.2, 0.3]))
69say $df[1;2];               #0.3
70say $df[1;*];               #(0.1 0.2 0.3)
71say $df[*;1];               #(0.2 0.2 0.2)
72

ANSWER

Answered 2022-Feb-03 at 18:24

The AT-POS method is only ever passed integer array indices.

The logic to handle slicing (with *, ranges, other iterables, the zen slice) is located in the array indexing operator, which is implemented as the multiple-dispatch subroutine postcircumfix:<[ ]> for single-dimension indexing and postcircumfix:<[; ]> for multi-dimension indexing. The idea is that a class that wants to act as an array-alike need not worry about re-implementing all of the slicing behavior and, further, that the slicing behavior will behave consistently over different user-defined types.

For slicing to work, one must implement elems as well as AT-POS. Adding:

1class Series does Positional {
2    has Real @.data = [0.1,0.2,0.3];
3
4    method AT-POS( $p ) { 
5        @!data[$p]
6    }   
7}
8
9class DataFrame does Positional {
10    has Series @.series;
11
12#`[ ATTEMPT #1
13    method AT-POS( $p, $q? ) { 
14        given $q {
15            when Int {                  #say 'Int';
16                @!series[$p][$q]
17            }   
18            when Whatever {             #say '*';
19                @!series[$p].data 
20            }   
21            default {                   #say 'default';
22                @!series[$p] 
23            }   
24
25        }   
26    }   
27#]
28    # ATTEMPT #2
29    method AT-POS(|c) is raw { #`[dd c;] @!series.AT-POS(|c) }
30}
31
32my $df = DataFrame.new( series =&gt; [Series.new xx 3] );
33
34say $df[1].data;            #[0.1 0.2 0.3]
35say $df[1][2];              #0.3
36say $df[0,1];               #(Series.new(data =&gt; $[0.1, 0.2, 0.3]) Series.new(data =&gt; $[0.1, 0.2, 0.3]))
37say $df[1;2];               #0.3
38say $df[1;*];               #got (0.1) ... expected [0.1 0.2 0.3]
39say $df[*;1];               #got (0.2) ... wanted [0.2 0.2 0.2]
40class Series does Positional {
41    has Real @.data = [0.1,0.2,0.3];
42
43    method elems {
44        @!data.elems
45    }   
46
47    method AT-POS( |p ) is raw {
48        @!data.AT-POS( |p )
49    }   
50}
51
52class DataFrame does Positional {
53    has Series @.series;
54
55    method elems { 
56        @!series.elems
57    }   
58
59    method AT-POS( |p ) is raw { 
60        @!series.AT-POS( |p )
61    }   
62}
63
64my $df = DataFrame.new( series =&gt; Series.new xx 3 );
65
66say $df[1].data;            #[0.1 0.2 0.3]
67say $df[1][2];              #0.3
68say $df[0,1];               #(Series.new(data =&gt; $[0.1, 0.2, 0.3]) Series.new(data =&gt; $[0.1, 0.2, 0.3]))
69say $df[1;2];               #0.3
70say $df[1;*];               #(0.1 0.2 0.3)
71say $df[*;1];               #(0.2 0.2 0.2)
72method elems() { @!data.elems }
73

Is Series and:

1class Series does Positional {
2    has Real @.data = [0.1,0.2,0.3];
3
4    method AT-POS( $p ) { 
5        @!data[$p]
6    }   
7}
8
9class DataFrame does Positional {
10    has Series @.series;
11
12#`[ ATTEMPT #1
13    method AT-POS( $p, $q? ) { 
14        given $q {
15            when Int {                  #say 'Int';
16                @!series[$p][$q]
17            }   
18            when Whatever {             #say '*';
19                @!series[$p].data 
20            }   
21            default {                   #say 'default';
22                @!series[$p] 
23            }   
24
25        }   
26    }   
27#]
28    # ATTEMPT #2
29    method AT-POS(|c) is raw { #`[dd c;] @!series.AT-POS(|c) }
30}
31
32my $df = DataFrame.new( series =&gt; [Series.new xx 3] );
33
34say $df[1].data;            #[0.1 0.2 0.3]
35say $df[1][2];              #0.3
36say $df[0,1];               #(Series.new(data =&gt; $[0.1, 0.2, 0.3]) Series.new(data =&gt; $[0.1, 0.2, 0.3]))
37say $df[1;2];               #0.3
38say $df[1;*];               #got (0.1) ... expected [0.1 0.2 0.3]
39say $df[*;1];               #got (0.2) ... wanted [0.2 0.2 0.2]
40class Series does Positional {
41    has Real @.data = [0.1,0.2,0.3];
42
43    method elems {
44        @!data.elems
45    }   
46
47    method AT-POS( |p ) is raw {
48        @!data.AT-POS( |p )
49    }   
50}
51
52class DataFrame does Positional {
53    has Series @.series;
54
55    method elems { 
56        @!series.elems
57    }   
58
59    method AT-POS( |p ) is raw { 
60        @!series.AT-POS( |p )
61    }   
62}
63
64my $df = DataFrame.new( series =&gt; Series.new xx 3 );
65
66say $df[1].data;            #[0.1 0.2 0.3]
67say $df[1][2];              #0.3
68say $df[0,1];               #(Series.new(data =&gt; $[0.1, 0.2, 0.3]) Series.new(data =&gt; $[0.1, 0.2, 0.3]))
69say $df[1;2];               #0.3
70say $df[1;*];               #(0.1 0.2 0.3)
71say $df[*;1];               #(0.2 0.2 0.2)
72method elems() { @!data.elems }
73method elems() { @!series.elems }
74

In DataFrame gives the results you're looking for.

If one really wants different slicing semantics, or a far more efficient implementation than the standard one is possible, one can also add multi candidates for the indexing operator (remembering to mark them is export).

Source https://stackoverflow.com/questions/70976231

QUESTION

To determine whether the current loop iteration is the last one or not

Asked 2022-Feb-07 at 10:34

How do we know and determine whether or not the current point of loop is the last one (as it's simply done in Perl due to having \ ref. operator see on) ?

The edited corrected link: https://perlmonks.org/?node_id=11140741

Please help point a workaround. Thanks

ANSWER

Answered 2022-Feb-03 at 13:38

If you want to do something if it is the last iteration of the loop, why not use the LAST phaser?

1for ^5 {
2    FIRST say &quot;$_ is the first&quot;;
3    LAST say &quot;$_ was the last&quot;;
4    .say;
5}
6

which outputs:

1for ^5 {
2    FIRST say &quot;$_ is the first&quot;;
3    LAST say &quot;$_ was the last&quot;;
4    .say;
5}
60 is the first
70
81
92
103
114
124 was the last
13

Source https://stackoverflow.com/questions/70972192

QUESTION

The inserted conditional code regex for Raku

Asked 2022-Feb-05 at 16:28

How to do the inserted conditional code regex in Raku regex
As analogue to its Perl regex

1 my $F = 1;
2 'foobarbar' =~ / (?(?{  $F  }) foo |  bar ) bar /x  ;
3

Please help out after tried so hard a day to no avail, thanks.

ANSWER

Answered 2022-Feb-01 at 11:00

This will work:

1 my $F = 1;
2 'foobarbar' =~ / (?(?{  $F  }) foo |  bar ) bar /x  ;
3my $F=1
4'foobar' ~~ / ^^ &quot;{ $F ?? &quot;foo&quot; !! &quot;bar&quot; }&quot; bar /; # 「foobar」
5$F=0
6'foobar' ~~ / ^^ &quot;{ $F ?? &quot;foo&quot; !! &quot;bar&quot; }&quot; bar /; # Nil
7

Code blocks in regexes will be run, but unless you convert them explicitly to strings (via quotes) they will be discarded.

Source https://stackoverflow.com/questions/70938290

QUESTION

How can the Raku behavior on capturing group in alternate be the same as Perl

Asked 2022-Jan-29 at 21:40

How can Raku behavior on capturing group in alternate be just like Perl regex' one e.g.

1&gt; 'abefo' ~~ /a [(b) | (c) (d)] (e)[(f)|(g)]/
2「abef」
3 0 =&gt; 「b」
4 2 =&gt; 「e」
5 3 =&gt; 「f」
6

needed to be 'usual' Perl regex result (let index system stay Raku):

1&gt; 'abefo' ~~ /a [(b) | (c) (d)] (e)[(f)|(g)]/
2「abef」
3 0 =&gt; 「b」
4 2 =&gt; 「e」
5 3 =&gt; 「f」
6 $0 = 'b'
7 $1 = undef
8 $2 = undef 
9 $3 = e
10 $4 = f
11

Please give useful guide.

ANSWER

Answered 2022-Jan-29 at 15:38

Quoting the Synopsis 5: Regexes and Rules design speculation document:

it is still possible to mimic the monotonic Perl 5 capture indexing semantics

Inserting a $3= for the (e):

1&gt; 'abefo' ~~ /a [(b) | (c) (d)] (e)[(f)|(g)]/
2「abef」
3 0 =&gt; 「b」
4 2 =&gt; 「e」
5 3 =&gt; 「f」
6 $0 = 'b'
7 $1 = undef
8 $2 = undef 
9 $3 = e
10 $4 = f
11/ a [ (b) | (c) (d) ] $3=(e) [ (f) | (g) ] /
12
13andthen say 'abefo' ~~ $_
14
15「abef」
16 0 =&gt; 「b」
17 3 =&gt; 「e」
18 4 =&gt; 「f」
19

I've briefly looked for a mention of this in the doc but didn't see it.

So maybe we should file doc issues for mentioning this, presumably in Capture numbers and $ ($1, $2, ...).

Source https://stackoverflow.com/questions/70904460

QUESTION

Difference in Perl regex variable $+{name} and $-{name}

Asked 2022-Jan-18 at 09:12

What is the difference between Perl regex variables $+{name} and $-{name} when both are used to refer to the same regex group from Perl statement/expression code?

ANSWER

Answered 2022-Jan-18 at 06:36

While $+{name} holds the captured substring referred by name as a scalar value, $-{name} refers to an array which holds capture groups with the name.
Here is a tiny example:

1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6'12' =~ /(?&lt;foo&gt;\d)(?&lt;foo&gt;\d)/; # '1' and '2' will be captured individually
7
8print $+{'foo'}, &quot;\n&quot;;          # prints '1'
9
10for (@{$-{'foo'}}) {            # $-{'foo'} is a reference to an array
11    print $_, &quot;\n&quot;;             # prints '1' and '2'
12}
13

As $+{name} can hold only a single scalar value, it is assigned to the first (leftmost) element of the capture groups.

Source https://stackoverflow.com/questions/70750715

QUESTION

Raku last on non-loops

Asked 2021-Dec-09 at 01:18

I have something that I can do easily in Perl, but not in Raku without fiddling around with flag variables. Here's the Perl code:

1#!/usr/bin/perl
2
3MAIN_BLOCK: {
4        foreach $item (1 2 3 4 5) {
5                $item == 6 and last MAIN_BLOCK;
6        }
7        print &quot;No items matched!\n&quot;;
8}
9

The relevant difference here is that Perl will allow you to use last to exit from any labelled block. Raku will only do this if the block is a loop.

Is there a good way to do this? I feel like there should be a phaser for this, but haven't figured out how to do it without flag variables, which seem like they should be avoidable.

Thanks,

ANSWER

Answered 2021-Dec-09 at 01:18

Raku supports similar control flow with given blocks.

Here's a fairly literal translation (i.e., not necessarily idiomatic Raku) from the Perl code you posted:

1#!/usr/bin/perl
2
3MAIN_BLOCK: {
4        foreach $item (1 2 3 4 5) {
5                $item == 6 and last MAIN_BLOCK;
6        }
7        print &quot;No items matched!\n&quot;;
8}
9given * {
10    for ^6 -&gt; $item {
11        succeed if $item == 6;
12    }
13    default { print &quot;No items matched!\n&quot;; }
14}
15

edit: Oh, and for a less-literally-translated/more-idiomatic-Raku solution, well, TIMTOWTDI but I might go with returning from an anonymous sub:

1#!/usr/bin/perl
2
3MAIN_BLOCK: {
4        foreach $item (1 2 3 4 5) {
5                $item == 6 and last MAIN_BLOCK;
6        }
7        print &quot;No items matched!\n&quot;;
8}
9given * {
10    for ^6 -&gt; $item {
11        succeed if $item == 6;
12    }
13    default { print &quot;No items matched!\n&quot;; }
14}
15sub { for ^6 { return when 6 }
16      say &quot;No items matched!&quot; }()
17

(Of course, I suppose it's possible that the most Raku-ish way to solve do that doesn't involve any Raku syntax at all – but instead involves modifying one of Raku's braided languages to allow for loops to take an else block. But I'm not advising those sort of shenanigans!)

Source https://stackoverflow.com/questions/70281827

QUESTION

Can Raku range operator on strings mimic Perl's behaviour?

Asked 2021-Dec-08 at 18:06

In Perl, the expression "aa" .. "bb" creates a list with the strings:

1aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb
2

In Raku, however, (at least with Rakudo v2021.08), the same expression creates:

1aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb
2aa ab ba bb
3

Even worse, while "12" .. "23" in Perl creates a list of strings with the numbers 12, 13, 14, 15, ..., 23, in Raku the same expression creates the list ("12", "13", "22", "23").

The docs seem to be quite silent about this behaviour; at least, I could not find an explanation there. Is there any way to get Perl's behaviour for Raku ranges?

(I know that the second problem can be solved via typecast to Int. This does not apply to the first problem, though.)

ANSWER

Answered 2021-Dec-08 at 18:06

It's possible to get the Perl behavior by using a sequence with a custom generator:

1aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb
2aa ab ba bb
3say 'aa', *.succ … 'bb';
4# OUTPUT: «aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb»
5
6say '12', *.succ … '23';
7# OUTPUT: «12 13 14 15 16 17 18 19 20 21 22 23»
8

(Oh, and a half solution for the '12'..'23' case: you already noted that you can cast the endpoints to a Numeric type to get the output you want. But you don't actually need to cast both endpoints – just the bottom. So 12..'23' still produces the full output. As a corollary, because ^'23' is sugar for 0..^'23', any Range built with &prefix:<^> will be numeric.)

For the "why" behind this behavior, please refer to my other answer to this question.

Source https://stackoverflow.com/questions/70239228

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Perl

Tutorials and Learning Resources are not available at this moment for Perl

Share this Page

share link

Get latest updates on Perl