Questions tagged [raku]
For questions relating to the Raku programming language (formerly known as Perl 6).
1,943
questions
7
votes
2
answers
105
views
Default value in new method causing object construction to work in an unexpected way
Can someone help me understand the following behavior?
class Box {
has $.data;
multi method new($d) {
say 'here';
self.bless(data => $d);
}
}
# construct object with ...
13
votes
2
answers
970
views
How can I split a string in every possible way?
Given the word abcd, how would I construct the following nested list?
[ (abcd) (a bcd) (ab cd) (abc d) (a b cd) (a bc d) (ab c d) (a b c d) ]
That is splitting the word in every way it can be, while ...
5
votes
1
answer
101
views
"No single root directory found, all dists must extract to a single directory" error when trying to upload module to zef via App::Mi6
Getting a weird error I've never seen before while trying to upload a new module to zef using the App::Mi6 mi6 release command:
==> Step 9. UploadToZef
Are you sure you want to upload Directory-0.0....
9
votes
2
answers
172
views
Elegant way to write when inside a for loop?
I can write something like this (elem here is an XML::Element but it doesn't really matter):
for $elem.nodes {
when XML::Element { ... }
when XML::Text { ... }
...
default { note qq{...
7
votes
1
answer
59
views
Comma doesn't seem to find "Legacy extensions" for some modules
I’m trying to figure out how to use the feature for automatically update legacy extensions with the Comma IDE. It either behaves inconsistently or I'm not understanding something clearly. I used Comma ...
7
votes
3
answers
95
views
Raku signature (Array @r) is not (Array:D)?
I can not seem to make head or tails of this Raku error message I found while exploring grammars...
Cannot resolve caller report(Array:D); none of these signatures matches:
(Array @r)
(Match $r)
So ...
5
votes
1
answer
90
views
"Pathname can't be converted from UTF-8 to current locale" warning with Libarchive::Read module
I'm getting the file listings for tar.gz files using the Libarchive::Read module. When a tarball file name has UTF-8 characters in it, I get an error which is generated by the libarchive C library:
...
6
votes
1
answer
103
views
Any way to easily ensure zef is pulling latest versions of raku modules?
I recently filed an issue here about zef downloading a stale version of a module, NativeHelpers::Callback which was failing when I tried to install it with zef install NativeHelpers::Callback. I had ...
8
votes
1
answer
67
views
Possible to automatically fold POD in Comma IDE?
I'm using the paid version of the Comma IDE. I'd like to be able to automatically fold the POD. I'm usig the ideavim plugin but that doesn't seem to offer a solution. I also tried to use the "...
8
votes
1
answer
119
views
Memory leak (?) using IO::Socket::Async (on FreeBSD 13.1)
In processing a stream of logs (via UDP) in a raku (v2022.07) app, I'm
hitting what appears to be a memory leak using IO::Socket::Async.
I pulled the code out into a simpler program which I've ...
8
votes
2
answers
84
views
Error install Inline::Perl5 with Zef (Raku)
I'm trying to install Raku on a Debian 11 x64. I never installed it before (perl5 too)
It's a server with some Perl5 scripts, and I want to "use" it in Raku with the Inline::Perl5 (also I ...
7
votes
1
answer
93
views
How do I prevent script from crashing as a result of failed proc?
I've got this:
try { run 'tar', '-zxvf', $path.Str, "$dir/META6.json", :err }
Despite being in a try{} block, this line is still causing my script to crash:
The spawned command 'tar' exited ...
12
votes
2
answers
165
views
Asynchronous reading of an stdout
I've wrote this simple script, it generates one output line per second (generator.sh):
for i in {0..5}; do echo $i; sleep 1; done
The raku program will launch this script and will print the lines as ...
8
votes
2
answers
143
views
Recommended method for constraining class attributes?
I have a class with a string attribute. The attribute represents a path to a file. I want to ensure this file exists before constructing an object. In the future, I may also want to throw additional ...
4
votes
1
answer
115
views
Why doesn't IO::Socket::Async's emit a trailing "a"?
I was wondering if anyone knows how to get around the encoding of IO::Socket::Async, particularly the draw-backs described by this:
For example, if the UTF-8 encoding is being used and the last byte ...
1
vote
0
answers
87
views
Raku to Perl guide — does it exist? [closed]
There is a substantial Perl to Raku guide.
What about Raku to Perl guide? Anything in existence? Or maybe it is currently being written? Or planned?
My wishlist would be:
With an epigraph like "...
9
votes
2
answers
170
views
Secure websockets with Cro
Briefly: I created a service on an internet server using Cro and websocket. Very simple using Cro examples. No problem when sending and receiving data from an HTML page when the page is served as ...
8
votes
1
answer
130
views
How can I respond to a key press, without a CRLF, in Raku?
I would like to implement:
Press any key to continue...
That responds to, well, any key without pressing the Return or Enter key.
Term::ReadKey does not implement this behavior. It requires a CRLF.
I'...
8
votes
1
answer
113
views
How can I obtain a pointer to a Grammar token or regex?
This is similar to this question for classes, except the same procedure does not seem to work for Grammars.
grammar TestGrammar {
token num { \d+ }
}
my $test-grammar = TestGrammar.new();
my $...
8
votes
2
answers
123
views
Why is my %h is List = 1,2; a valid assignment?
While finalizing my upcoming Raku Advent Calendar post on sigils, I decided to double-check my understanding of the type constraints that sigils create. The docs describe sigil type constraints with ...
7
votes
2
answers
107
views
How to slip gather-take in lazy manner into map?
I need to construct following flow:
Accept list of file names
Extract multiple lines from those files
Process those lines
However I have no idea how to properly inject gather-take into map:
sub MAIN ...
6
votes
1
answer
115
views
How can you use multiple modules in a Raku project, if the modules are defined in the project?
I'm playing around with writing modules in Raku, when it made sense for me to break a piece of functionality off into another .rakumod file. How can I link these files together when I compile?
I tried ...
6
votes
2
answers
79
views
How to align strings to right, and chop them if too long in Raku?
The important part is, I want the alignment to hold, so the string gets chopped from left. I tried substr, but that crashes if a string is not long enough. Currently I have a similar solution to this:
...
9
votes
1
answer
127
views
How can I group elements of a list in Raku?
Is there some method in Raku which, when you pass it a "getter", groups together items from the original list for which the getter is returning the same value?
I am looking for something ...
8
votes
1
answer
89
views
Anonymous state variables versus topic variables in Raku
In January 2020 I had written this code
my @a = ( 1, { ([+] 1 .. ++$ ) / [×] 1 .. ++$ } ... Inf)[^7];
say @a.join(' + ') ~ "=" ~ [+] @a;
to express the following mathematical series in Raku:...
7
votes
1
answer
96
views
How can I pipe in a string to a Raku program?
foo.raku:
#! /usr/bin/env raku
use v6.d;
sub MAIN (Str $some-text = $*IN.slurp, Bool :$verbose) {
say "Your text:" if $verbose;
say $some-text;
}
When I run it
~% echo &...
8
votes
1
answer
125
views
How to correctly decontainerize the value side of a Pair object
I'm building up several Pair objects in a loop, and I use the same scalar variable (albeit with a different value) for the value of each of them.
As a simplified example of what I'm doing, consider
my ...
4
votes
1
answer
61
views
how to make a context aware code evaluator
I was looking at REPL-like evaluation of code from here and here, and tried to make a very small version for it, yet it fails:
use nqp;
class E {
has Mu $.compiler;
has $!save_ctx;
...
4
votes
3
answers
146
views
How do I "assign" the value in `CArray` that contains a memory address to a `Pointer`?
This is a NativeCall question.
I have 8 bytes (little endian) in a CArray representing a memory address. How do I create a Pointer out it?
(CArray and Pointer are two of NativeCall's C compatible ...
6
votes
1
answer
152
views
Concurrency: how to share a cache-hash safely?
Is it enough to protect the write accesses or is it necessary to protect the read accesses as well in print-columns?
#!/usr/bin/env raku
my @w_list_items;
len_items_list( <car tree house mouse dog ...
13
votes
2
answers
239
views
When does it make sense to use a sigil-less variable in Raku?
Raku sigils denote the nature of the underlying variable (e.g., $scalar, @positional, %associative, &code).
It's possible to declare a variable as sigil-less with a backslash (e.g., \some-variable)...
7
votes
3
answers
280
views
How much does Raku's Promise construct share in common with JavaScript's Promises?
I'm learning asynchronous programming in JS and I couldn't help but noticed both JS and Raku has some construct for asynchronous programming with the same name, however I'm uncertain to what extent ...
9
votes
1
answer
168
views
Using a default value for a function parameter which depends of other parameter
I'd like to create an script which takes an input file and optionally an output file. When you don't pass an output file, the script uses the same filename as the input but with the extension changed. ...
7
votes
1
answer
121
views
Inconsistent behavior when coercing an Array to a List?
my @foo;
@foo = (1, (2, 3), (4, (5, 6), 7), (8), 9).List;
say @foo.flat;
# OUTPUT: (1 (2 3) (4 (5 6) 7) 8 9)
# this is NOT the output I expected.
@foo = (1, (2, 3), (4, (5, 6), 7), (8), 9);
say @foo....
7
votes
2
answers
155
views
How to use CStruct
I'm trying to return struct from shared library written in C. This is simple code, for testing of returning structure and simple int32, libstruct.c, compiled by gcc -shared -Wl,-soname,libstruct.so.1 -...
9
votes
2
answers
114
views
Constructing enums using Z=> with variables instead of list literals
I would like to construct a String-typed enum. The following works:
my Str enum E ( <a b c> Z=> 'one', 'two', 'three' );
E.kv.raku.say;
("c", "three", "a", &...
10
votes
2
answers
118
views
Specify the number of decimal digits on the explicit generator of a sequence in Raku
I've written a simple code:
sub euler-ex ($x) of Seq {
1, { $x**++$ / [×] 1 .. ++$ } ... Inf }
say " 5: " ~ euler-ex(5)[^20] ~ " = " ~ [+](euler-ex(5)[^20]);
The output:
5: 1 ...
9
votes
0
answers
172
views
Why doesn't 'utf8-c8' encoding work when reading filehandles
I wish to read byte sequences that will not decode as valid UTF-8, specifically byte sequences that correspond to high and low surrogates code points. The result should be a raku string.
I read that, ...
5
votes
1
answer
102
views
Why aren't variables interpolated in `constant` declarations?
use v6.d;
my Str $foo = 'Hello';
my constant $BAR = "--$foo--";
say $BAR;
OUTPUT:
Use of uninitialized value element of type Str in string context.
Methods .^name, .raku, .gist, or .say ...
3
votes
2
answers
165
views
Is there a Raku function/method that would allow a sorted hash array to be iterated upon and determine its biggest value?
I am currently working with a hash array that I have sorted by its keys and values. I have been successful in splitting the hash up such that I can grab the key and value separately. I have created a ...
3
votes
1
answer
111
views
Can't seem to be able to grab non-string output from run
I can't think of other way to run a command line that outputs binary files, so I'll have to go with this.
Let's add a binary file to a git repository
mkdir test
cd test
git init .
wget https://upload....
3
votes
1
answer
117
views
Why is it that sometimes we have to use coercion to access individual elements of an array?
My understanding is that in the code below I wouldn't have to use (2), just (1) would suffice to print each element of the array in its own line.
$ cat test-hash.raku
use ...
9
votes
2
answers
203
views
Is there a "pipe" operator in Raku?
I mean the reverse application operator. For example in Haskell it would be &:
ghci> import Data.Function ((&))
ghci> 2 & (+ 1)
3
In Raku I saw ==> feed operator, but that doesn'...
7
votes
1
answer
158
views
Why is parsing of aaaaabb not successful assuming the given grammar?
Suppose grammar G has the following productions …
S → aSb | A | B
A → λ | a | aa | aaa
B → bB | b
Using grammar G, a derivation of the string aaaaabb is …
S ⇒ aSb ⇒ aaSbb ⇒ aaAbb ⇒ aaaaabb
In the ...
5
votes
2
answers
150
views
Shouldn't the same srand value produce the same random numbers?
When I repeatedly run this code,
srand 1;
my @x = (1..1000).pick: 100;
say sum @x;
I get different answers each time. If I'm resetting with srand why shouldn't it produce the same random numbers ...
6
votes
2
answers
239
views
Way to minimize scripts start-up time via persistence?
Raku start-up times are notoriously huge.
On my machine (old one but the most modern I own):
time raku -e 'put "Hello, World!"'
495.67 - 567.02 millis
(1.53 secs for the 1st time launch)
...
4
votes
1
answer
117
views
listop operator causing infinite recursion, any way to fix?
I'm looking to possibly help update the File::HomeDir module which was never finished. While inspecting it, I noticed that stubbed out methods were causing infinite loops:
In the File::HomeDir role:
...
6
votes
1
answer
99
views
Constrain return type to be type captured by role parameter
Should the following work in Raku:
my role R[::T] {
has T $!a;
method m ( T $v ) {
$!a = $v;
}
method n ( --> T ) {
$!a;
}
}
my class C does R[Str] { }
my C $c ...
5
votes
1
answer
133
views
How are dynamically scoped variables implemented in Rakudo/MoarVM?
That is, variables like $*scalar, @*array and %*hash.
I'm asking this question mainly because I want to have an idea of how much of a burden they are on the overall performance of the grammar/regex ...
5
votes
1
answer
232
views
Using scalar variables to refer to an array inside an explicit generator
I've written a code to calculate the Fibonacci series using array variables inside the explicit generator like this:
my @fib = [0],[1],-> @a, @b {[|@a Z+ |@b]} ... Inf;
say @fib[^6];
This works as ...