🌐
The Mail Archive
mail-archive.com › perl6-all@perl.org › msg114335.html
Re: I need help with my run keeper
https://search.brave.com/search?q=raku+what+is+:$arg0&summary=1&conversation=06ac2b89c0e415523f6cb7 In Raku, the :$arg0 parameter is an optional named argument used within the run subroutine to specify the value of the argv for the process being executed. This argument allows you to set the ...
🌐
Raku Documentation
docs.raku.org › type › Proc
class Proc | Raku Documentation
If :arg0 is set to a value, that value is passed as arg0 to the process instead of the program name.
🌐
Raku Documentation
docs.raku.org › routine › run
run | Raku Documentation
It is an error to run a thread that has already been started. See primary documentation in context for sub run. ... sub run( *@args ($, *@), :$in = '-', :$out = '-', :$err = '-', Bool :$bin = False, Bool :$chomp = True, Bool :$merge = False, Str:D :$enc = 'UTF-8', Str:D :$nl = "\n", :$cwd = ...
🌐
The Mail Archive
mail-archive.com › perl6-all@perl.org › msg114333.html
I need help with my run keeper
I am missing what run does with (not finding them in https://docs.raku.org/routine/run) :$cwd :$env :$arg0 and :$win-verbatim-args This is what I have so far: method new(Proc:U: :$in = '-', :$out = '-', :$err = '-', Bool :$bin = False, Bool :$chomp = True, Bool :$merge = False, Str:D :$enc = 'UTF-8', Str:D :$nl = "\n", --> Proc:D) sub run( *@args ($, *@), :$in = '-', # use `:$in`, `$:out`, and `:$err` arguments to redirect to # different file handle, thus creating a kind of pipe
🌐
Raku Documentation
docs.raku.org › type › independent-routines
Independent routines | Raku Documentation
As of the 2020.12 release of the Rakudo compiler, it is also possible to call the spurt subroutine without any data. This will either create an empty file, or will truncate any existing file at the given path. ... sub run( *@args ($, *@), :$in = '-', :$out = '-', :$err = '-', Bool :$bin = False, Bool :$chomp = True, Bool :$merge = False, Str:D :$enc = 'UTF-8', Str:D :$nl = "\n", :$cwd = $*CWD, Hash() :$env = %*ENV, :$arg0...
🌐
Raku Guide
raku.guide
Raku Guide
This document is intended to give you a quick overview of the Raku programming language. For those new to Raku, it should get you up and running · Some sections of this document refer to other (more complete and accurate) parts of the Raku documentation. You should read them if you need more ...
🌐
Raku Documentation
docs.raku.org › language › functions
Functions | Raku Documentation
When calling a function, positional arguments should be supplied in the same order as the function's signature. Named arguments may be supplied in any order, but it's considered good form to place named arguments after positional arguments. Inside the argument list of a function call, some special syntax is supported:
Top answer
1 of 2
20

Basics

That feature is built into Raku (formerly known as Perl 6). Here is the equivalent of your Getopt::Long code in Raku:

sub MAIN ( Str  :$file    = "file.dat"
         , Num  :$length  = Num(24)
         , Bool :$verbose = False
         )
{
    $file.say;
    $length.say;
    $verbose.say;
}

MAIN is a special subroutine that automatically parses command line arguments based on its signature.

Str and Num provide string and numeric type constraints.

Bool makes $verbose a binary flag which is False if absent or if called as --/verbose. (The / in --/foo is a common Unix command line syntax for setting an argument to False).

: prepended to the variables in the subroutine signature makes them named (instead of positional) parameters.

Defaults are provided using $variable = followed by the default value.

Aliases

If you want single character or other aliases, you can use the :f(:$foo) syntax.

sub MAIN ( Str  :f(:$file)    = "file.dat"
         , Num  :l(:$length)  = Num(24)
         , Bool :v(:$verbose) = False
         )
{
    $file.say;
    $length.say;
    $verbose.say;
}

:x(:$smth) makes additional alias for --smth such as short alias -x in this example. Multiple aliases and fully-named is available too, here is an example: :foo(:x(:bar(:y(:$baz)))) will get you --foo, -x, --bar, -y and --baz and if any of them will pass to $baz.

Positional arguments (and example)

MAIN can also be used with positional arguments. For example, here is Guess the number (from Rosetta Code). It defaults to a min of 0 and max of 100, but any min and max number could be entered. Using is copy allows the parameter to be changed within the subroutine:

#!/bin/env perl6
multi MAIN
#= Guessing game (defaults: min=0 and max=100)
{
    MAIN(0, 100)
}

multi MAIN ( $max )
#= Guessing game (min defaults to 0)
{
    MAIN(0, $max)
}

multi MAIN
#= Guessing game
( $min is copy #= minimum of range of numbers to guess
, $max is copy #= maximum of range of numbers to guess
)
{
    #swap min and max if min is lower
    if $min > $max { ($min, $max) = ($max, $min) }

    say "Think of a number between $min and $max and I'll guess it!";
    while $min <= $max {
        my $guess = (($max + $min)/2).floor;
        given lc prompt "My guess is $guess. Is your number higher, lower or equal (or quit)? (h/l/e/q)" {
            when /^e/ { say "I knew it!"; exit }
            when /^h/ { $min = $guess + 1      }
            when /^l/ { $max = $guess          }
            when /^q/ { say "quiting"; exit    }
            default   { say "WHAT!?!?!"        }
        }
    }
    say "How can your number be both higher and lower than $max?!?!?";
}

Usage message

Also, if your command line arguments don't match a MAIN signature, you get a useful usage message, by default. Notice how subroutine and parameter comments starting with #= are smartly incorporated into this usage message:

./guess --help
Usage:
  ./guess -- Guessing game (defaults: min=0 and max=100)
  ./guess <max> -- Guessing game (min defaults to 0)
  ./guess <min> <max> -- Guessing game

    <min>    minimum of range of numbers to guess
    <max>    maximum of range of numbers to guess

Here --help isn't a defined command line parameter, thus triggering this usage message.

See also

See also the 2010, 2014, and 2018 Perl 6 advent calendar posts on MAIN, the post Parsing command line arguments in Perl 6, and the section of Synopsis 6 about MAIN.

2 of 2
5

Alternatively, there is a Getopt::Long for perl6 too. Your program works in it with almost no modifications:

use Getopt::Long;
my $data   = "file.dat";
my $length = 24;
my $verbose;
get-options("length=i" => $length,    # numeric
            "file=s"   => $data,      # string
            "verbose"  => $verbose);  # flag

say $length;
say $data;
say $verbose;
🌐
JJA
pinguinorodriguez.cl › blog › raku-argument-parsing
Sharing command line parameters in Raku | JJA
November 11, 2020 - But the compiler not only can determine whether a function call is valid or not based on its signature, it can also decide which function to call based the arguments used. ... #!/usr/bin/env raku multi sub MAIN ( 'grep', Str $path, Bool :$debug ) { note "Working on $path" if $debug; .say for $path.IO.lines.grep: /raku/; } multi sub MAIN ( 'count', Str $path, Bool :$debug ) { note "Working on $path" if $debug; say "$_ has { .IO.lines.elems } lines" given $path; } multi sub MAIN ( Str $command, Str $path, Bool :$debug, ) is hidden-from-USAGE { note "Working on $path" if $debug; say "Unknown command: $command"; say $*USAGE; }
🌐
Rosetta Code
rosettacode.org › wiki › Command-line_arguments
Command-line arguments - Rosetta Code
October 1, 2025 - # with arguments supplied $ raku -e 'sub MAIN($x, $y) { say $x + $y }' 3 5 8 # missing argument: $ raku -e 'sub MAIN($x, $y) { say $x + $y }' 3 Usage: -e '...' x y
Find elsewhere
🌐
Raku
course.raku.org › essentials › functions › named-parameters
Named parameters
Course of Raku / Essentials / Creating and calling functions · In contrast to positional parameters, named parameters are referred by their names. The following function takes two parameters called $from and $to. ... It is an error to pass the arguments as if they were positional.
🌐
Learn X in Y Minutes
learnxinyminutes.com › docs › raku
Learn Raku in Y Minutes
In any # other case, `+@` works like `**@`. sub c(+@arr) { @arr.perl.say }; c(['a', 'b', 'c']); # OUTPUT: «["a", "b", "c"]␤» c(1, $('d', 'e', 'f'), [2, 3]); # OUTPUT: «[1, ("d", "e", "f"), [2, 3]]␤» c(1, [1, 2], ([3, 4], 5)); # OUTPUT: «[1, [1, 2], ([3, 4], 5)]␤» # You can call a function with an array using the "argument list flattening" # operator `|` (it's not actually the only role of this operator, # but it's one of them). sub concat3($a, $b, $c) { say "$a, $b, $c"; } concat3(|@array); # OUTPUT: «a, b, c␤» # `@array` got "flattened" as a part of the argument list #########
🌐
Raku Documentation
docs.raku.org › language › create-cli
Command line interface | Raku Documentation
Options with an optional argument (like --profile), which are valid both with and without an argument. You can only give these arguments an option with the = syntax; if there is a space after the option, that means it was called without an argument.
🌐
Ohmycloudy
ohmycloudy.github.io › 35.html
Raku By Example
## $*ARGFILES $*ARGFILES.perl.say; #=> IO::Handle.new(:path(Any),:chomp) # 按行读取 for $*ARGFILES.lines -> $line { say "$line"; } # 一次性读取 # say $*ARGFILES.slurp; ## @*ARGS say @*ARGS.WHAT; say @*ARGS; say @*ARGS.perl; ## $*CWD say $*CWD; say $*CWD.path; say $*CWD.perl; ## $*DISTRO say $*DISTRO; say $*DISTRO.name; say $*DISTRO.is-win; say $*DISTRO.version; say $*DISTRO.path-sep; say $*DISTRO.auth; say $*DISTRO.desc; say $*DISTRO.release; say $*DISTRO.signature; say $*DISTRO.gist; say $*DISTRO.Str; say $*DISTRO.perl; ## dynamic named arguments multi f(:$named) { note &?ROUTINE.sig
🌐
Subroutines
uzluisf.gitlab.io › subroutines
Subroutines | Raku by Example
A signature refers to the number and type of parameters defined by a routine. On the hand, a capture refers to the list of arguments a signature accepts. Thus, when a routine is defined we talk about its signature and when it's called, then we talk about its capture.
🌐
Andrewshitov
andrewshitov.com › 2018 › 10 › 31 › non-value-argument-passing-in-perl-6-subs
📘 Non-value argument passing in Raku subs – Andrew Shitov
By default, you pass the arguments by their values. Despite that, it is not possible to modify them inside the sub. To pass a variable by reference, add the is rw trait. (Note that formally this is not a reference but a mutable argument.)
🌐
Raku Documentation
docs.raku.org › language › system
System interaction | Raku Documentation
The simplest way is to use the @*ARGS variable to obtain arguments from the command line; this array will contain the strings that follow the program name.
🌐
Raku Documentation
docs.raku.org › language › operators
Operators | Raku Documentation
Note: since the result is obtained as a return value, using this operator with the ~~ smartmatch operator is a mistake and will issue a warning. To execute the substitution on a variable that isn't the $_ this operator uses, alias it to $_ with given, with, or any other way.
🌐
Raku::Journey
rakujourney.wordpress.com › 2023 › 08 › 07 › raku-the-variable-lifecycle
raku: the Variable Lifecycle – Raku::Journey
August 7, 2023 - This post is focused on the lifecycle of the standard raku scalar variable with the $ sigil since it is the obvious inheritor of the perl $ variable which can also be seen in similar guise in places like Bash, PHP and SASS.