Questions tagged [raku]

For questions relating to the Raku programming language (formerly known as Perl 6).

Raku

Raku is an expressive and feature-rich programming language designed by Larry Wall and developed by the community.

Features include:

  • Object-oriented programming
  • Functional programming primitives
  • Parallelism
  • Concurrency
  • Asynchrony
  • Definable grammars for pattern matching and generalized string processing
  • Optional and gradual typing
  • Extensive Unicode support from code points to grapheme clusters
  • Multiple dispatch
  • Introspection
  • Rational numbers

is a Raku implementation that targets multiple back ends.

The MoarVM back end is recommended for normal use, but back ends for the Java Virtual Machine and JavaScript are also under active development.

The Rakudo implementation shipped a first official release named “коледа” on December 25, 2015. Most of the specification is frozen, and optimizations are ongoing.

Official links

1993 questions
11
votes
2 answers

How to type-constrain the entries of a Raku function's array argument?

I am trying to define a subroutine in Raku whose argument is, say, an Array of Ints (imposing that as a constraint, i.e. rejecting arguments that are not Arrays of Ints). Question: What is the "best" (most idiomatic, or straightforward, or whatever…
grobber
  • 1,083
  • 1
  • 9
  • 20
11
votes
1 answer

Why do I get “Lexical with name '$x' does not exist in this frame” when using “will leave”?

I have the following Raku code: class Thing { method close { say "closed"; } }; for 1..1000 { my $x will leave { .close } = Thing.new; } Running it, I get the error: Lexical with name '$x' does not exist in this frame in…
Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
11
votes
4 answers

Raku vs. Perl5, an unexpected result

When I run this script in Raku I get the letter A with several newlines. Why do I not get the concatenated strings as expected (and as Perl5 does) ? EDIT BTW can I compile in commaIDE the file with the Perl5 compiler, where can I change this…
user2925716
  • 949
  • 1
  • 6
  • 13
11
votes
2 answers

How can I return 2 hashes from a raku sub?

... #!/usr/bin/env raku # -*-perl6-*- # 2021-5-30: Example of how a sub does not seem to be able to return 2 Hashes... sub GetHashes { my %H = 100 => 2149, 101 => 2305, 102 => 2076, 103 => 1767, 104 => 1743 ; my %G = 100 => 21493, 101…
GRM
  • 119
  • 2
11
votes
1 answer

`now` becomes slow in a 10 million iterations loop

I have a SnowFlake script for Python, and I convert it to a Raku module, and call it 10,000,000 times, and it is very slow (file test.raku): use IdWorker; my $worker = IdWorker.new(worker_id => 10, sequence => 0); my @ids = gather for…
chenyf
  • 5,048
  • 1
  • 12
  • 35
11
votes
2 answers

Actually CATCHing exceptions without creating GOTO

Looking over my Raku code, I've realized that I pretty much never use CATCH blocks to actually catch/handle error. Instead, I handle errors with try blocks and testing for undefined values; the only thing I use CATCH blocks for is to log errors…
codesections
  • 8,900
  • 16
  • 50
11
votes
1 answer

`does` versus `but` operators when mixing in a Role into an object in Raku

If I have a Role R defined as: role R { method answer { 42 } } What is the difference (if any) between these two lines: my $a = 'question' does R; my $b = 'question' but R; They appear very similar: say $a.answer; # OUTPUT: «42» say $b.answer; …
codesections
  • 8,900
  • 16
  • 50
11
votes
2 answers

How to use a wrapper script for a Raku CLI

I am wondering what steps, (such as shebang lines and wrapper scripts) are recommended when creating a CLI application in Raku. I am interested in info both for scripts that will be installed with Zef and those that will be distributed…
codesections
  • 8,900
  • 16
  • 50
11
votes
1 answer

Instantiate a Raku class, and update an instance variable in the constructor

I seem to be having difficulty understanding how to correctly work with classes in Raku. I am trying to create a 'Database' class that I will use throughout my cro application. However I don't seem to understand how to deal with setting instance…
camstuart
  • 623
  • 3
  • 13
11
votes
1 answer

How to pass pointer to a container from function?

I can bind containers to new names: my %h; my $p := %h{ "a" }{ "b" }{ "c" }; $p = 1; say %h; Which outputs expected: {a => {b => {c => 1}}} But what if I need to return such pointer from subroutine? my %h; sub get-pointer { my $p := %h{ "a" }{…
Pawel Pabian bbkr
  • 1,139
  • 5
  • 14
11
votes
1 answer

How to declare a function that accepts a typed array parameter

Say I want to declare a function whose parameter is an array of strings: sub process-string-array(Str[] stringArray) # invalid { ... } How would I do that ?
zentrunix
  • 2,098
  • 12
  • 20
11
votes
2 answers

How Do You Do File Locking in Raku?

I've been trying to figure out how to do file locking in Raku without success. I started looking into fcntl with NativeCall, but then realized that fcntl locks don't prevent file access from other threads. What's the best way to do file locking in…
JustThisGuy
  • 1,109
  • 5
  • 10
11
votes
2 answers

Assignment to a List Container Confusion

I may be suffering from brain fade, but according to the docs regarding Items and List assignment (https://docs.raku.org/language/variables#Item_and_list_assignment ), Assignment to a List container (list-context) always triggers list…
librasteve
  • 6,832
  • 8
  • 30
11
votes
4 answers

Parametrized types in Raku, how to use run time values as parameters

I'd like to create some parametrized types for Raku; basically, I'd like to create some different classes whose main difference would be the range of values of one of its attributes; for instance, classes represent types of building, I'd like to…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
11
votes
3 answers

how to interpolate string containing capture-group parentheses as regex in Raku?

I want to match against a programmatically-constructed regex, containing a number of (.*) capture groups. I have this regex as a string, say my $rx = "(.*)a(.*)b(.*)" I would like to interpolate that string as a regex and match for it. The docs…
grobber
  • 1,083
  • 1
  • 9
  • 20