Raku

From Gentoo Wiki
Jump to:navigation Jump to:search
This article is a stub. Please help out by expanding it - how to get started.

Raku is a high-level, general-purpose, and gradually typed programming language with low boilerplate objects, optionally immutable data structures, and an advanced macro system. Raku is not tied to a specific programming paradigm: it supports procedural, object oriented, and functional programming in equal measure.

The reference implementation of the Raku programming language has several major components:

Note
In the absence of a Rakudo-Star Gentoo ebuild, it's possible to bootstrap Rakudo Star via Rakubrew. See the Troubleshooting section of this article for details. Additionally, it's possible to use the official docker container rakudo-star provided by the upstream project.

Raku was created as both a specification and a test suite. Any implementation that passes the test suite Raku/roast is considered to be Raku. Thus, while MoarVM is Raku's default virtual machine it's not the only virtual machine that Raku supports. Currently, Raku can be run from within the Java and JavaScript virtual machines and more virtual machine targets are expected to follow.

Raku in Context

Market Niche

To some, Raku is primarily a postmodern glue language. To others, it's thought of as the "Swiss Army lightsaber" owing to its usefulness in situations that require quick hacks, while still maintaining a certain elegance. In practice, it is often used as a shell-like scripting language with objects and robust math support. While not yet a popular choice, Raku's concurrency model is helping it slowly carve out a niche in the DevOps space.

Pros

  • Raku is Multi-paradigm procedural, object oriented, and functional programming styles are supported.
  • Raku Grammars combine named regexes with recursion, human readable rules, and grapheme-level Unicode support to enable advanced text processing capabilities far beyond that of Perl compatible regular expressions.
  • Raku has a concurrency and async model that ensure that non-linear code is easy to read and maintain.
  • Raku natively supports rational numbers which permits direct comparison without concern for any accumulated rounding errors that would occur in other programming languages.
  • Lazy evaluation which enables features such as infinite sets.
  • Low boilerplate object oriented programming.
  • Raku's community has a reputation for being extremely welcoming of beginners.

Cons

  • Raku's heavy use of symbolic operators and optional use of non-ASCII characters can confuse novice Raku programmers.
  • In its current iteration, Raku's execution speed is slower than Python for most operations.
  • Initial VM startup is also sluggish, roughly comparable to Java's virtual machine startup time.
  • Raku's regular expression syntax is very different from PCRE. This takes some getting used to.
  • Raku's Fez package ecosystem is smaller than Python's Pip and Perl's CPAN making finding an existing module to perform a task a hit-or-miss proposition.

Complimentary Languages

Raku is closely related to Perl and the two languages do share some syntax. Many Perl programmers are at least passingly familiar with Raku. Interestingly, some Haskell programmers have taken a liking to Raku as it's optionally highly functional. Some Rust programmers have also been known to use Raku for compile-time utility scripts.

Installation

Raku uses the reference compiler Rakudo:

Emerge

root #emerge --ask dev-lang/rakudo

USE flags

USE flags for dev-lang/rakudo A compiler for the Raku programming language

+moar Use the MoarVM as backend
clang Use Clang to compile the MoarVM backend
java Add support for Java
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)

Environment variables

  • RAKULIB — Modify the module search path.
  • PERL6LIB — Modify the module search path (DEPRECATED).
  • RAKUDO_HOME — Override the path of the Rakudo runtime files.
  • NQP_HOME — Override the path of the NQP runtime files.

Files

  • ~/.raku/ — This directory is where Raku keeps the rakudo-history file and some other odds and ends.

Removal

root #emerge --ask --depclean --verbose dev-lang/rakudo

Usage

Invocation

user $rakudo --help
perl6.moarvm [switches] [--] [programfile] [arguments]
With no arguments, enters a REPL (see --repl-mode option).
With a "[programfile]" or the "-e" option, compiles the given program
and, by default, also executes the compiled code.

  -                    read program source from STDIN or start REPL if a TTY
  -c                   check syntax only (runs BEGIN and CHECK blocks)
  --rakudoc            extract documentation and print it as text
  --rakudoc=module     use RakuDoc::To::[module] to render inline documentation
  -e program           one line of program, strict is enabled by default
  -h, --help           display this help text
  -n                   run program once for each line of input
  -p                   same as -n, but also print $_ at the end of lines
  -I path              adds the path to the module search path
  -M module            loads the module prior to running the program
  --target=stage       specify compilation stage to emit
  --optimize=level     use the given level of optimization (0..3)
  --rakudo-home=path   Override the path of the Rakudo runtime files
  -o, --output=name    specify name of output file
  -v, --version        display version information
  -V                   print configuration summary
  --stagestats         display time spent in the compilation stages
  --ll-exception       display a low level backtrace on errors
  --doc                extract documentation and print it as text
  --doc=module         use Pod::To::[module] to render inline documentation
  --repl-mode=interactive|non-interactive
                       when running without "-e" or filename arguments,
                       a REPL is started. By default, if STDIN is a TTY,
                       "interactive" REPL is started that shows extra messages
                       and prompts, otherwise a "non-interactive" mode is used
                       where STDIN is read entirely and evaluated as if it were
                       a program, without any extra output (in fact, no REPL
                       machinery is even loaded). This option allows to bypass
                       TTY detection and force one of the REPL modes.
  --profile[=name]     write profile information to a file
                       Extension controls format:
                           .json outputs in JSON
                           .sql  outputs in SQL
                           any other extension outputs in HTML
  --profile-compile[=name]
                       write compile-time profile information to a file
                       Extension controls format:
                         .json outputs in JSON
                         .sql  outputs in SQL
                         any other extension outputs in HTML
  --profile-kind[=name]
                       choose the type of profile to generate
                         instrumented - performance measurements (default)
                         heap - record heap snapshots after every garbage
                         collector run
  --profile-filename=name
                       provide a different filename for profile.
                       Extension controls format:
                         .json outputs in JSON
                         .sql  outputs in SQL
                         any other extension outputs in HTML
                       This option will go away in a future Rakudo release
  --profile-stage=stage
                       write profile information for the given compilation
                       stage to a file. Use --profile-compile to set name
                       and format
  --full-cleanup       try to free all memory and exit cleanly
  --debug-port=port    listen for incoming debugger connections
  --debug-suspend      pause execution at the entry point
  --tracing            output a line to stderr on every interpreter instr
                       (only if enabled in MoarVM)

Note that only boolean single-letter options may be bundled.

The following environment variables are respected:

  RAKULIB     Modify the module search path
  PERL6LIB    Modify the module search path (DEPRECATED)
  RAKUDO_HOME Override the path of the Rakudo runtime files
  NQP_HOME    Override the path of the NQP runtime files

Troubleshooting

How do I get the Star Bundle?

There isn't an official Gentoo ebuild of the star bundle just yet. A popular stop-gap approach is to use Rakubrew. There are a few ways to install and configure Rakubrew, but a common way is to leverage CPAN to get the job done.

First, install a modern CPAN client, such as CPAN Minus:

root #emerge --ask dev-perl/App-cpanminus

Once this is done, install Rakubrew via CPAN:

user $cpanm App::Rakubrew

Rakubrew is now installed to $HOME/perl5/ but ~/.bashrc needs to be updated so that this is in the user's path:

FILE ~/.bashrcAdding Rakubrew to the User's Path
# Rakubrew
if [ -d $HOME/perl5/lib/perl5 ]; then
	export PATH="$HOME/perl5/bin/:$PATH"
	PERL5LIB=${PERL5LIB:+$PERL5LIB:}$HOME/perl5/lib/perl5
	MANPATH=${MANPATH:+$MANPATH:}$HOME/perl5/man
	export MANPATH PERL5LIB
fi
eval "$($HOME/perl5/bin/rakubrew init Bash)"

Now the profile needs to be reloaded in order for these changes to take effect:

user $source ~/.bashrc

To build the most recent version of Raku, run the following:

user $rakubrew build

That done, the Raku package manger Zef still needs to be built:

user $rakubrew build-zef

Zef can now install all of Raku's Star Bundle modules with the following Bash script:

FILE zef-install-star.shInstall Rakudo Star Modules via Zef
#!/usr/bin/env bash

raku_packages=('File::Find' 'Log' 'Log::Colored' 'Terminal::ANSIColor'
               'Terminal::ANSIParser' 'Terminal::LineEditor' 'Hash::Merge'
               'IO::Glob' 'Config' 'Crane' 'Config::TOML' 'MIME::Base64'
               'YAMLish' 'JSON::Fast' 'File::Directory::Tree' 'File::Temp'
               'NativeHelpers::Blob' 'NativeLibs' 'DBIish' 'URI'
               'JSON::OptIn' 'JSON::Name' 'JSON::Unmarshal' 'JSON::Marshal'
               'JSON::Class' 'META6' 'License::SPDX' 'Test::META'
               'XML::Writer' 'SVG' 'SVG::Plot' 'OO::Monitors' 'Test::Mock'
               'Grammar::Profiler::Simple' 'Grammar::Debugger' 'JSON::Tiny' 
               'PathTools' 'OpenSSL' 'IO::Socket::SSL' 'Digest::MD5'
               'HTTP::Status' 'Template::Mustache' 'PSGI' 'HTTP::Easy' 
               'Template::Mojo' 'Test::When' 'Digest' 'Testo' 'Temp::Path' 
               'Pod::Load' 'Test::Output' 'DateTime::Parse' 'Encode' 
               'HTTP::UserAgent' 'Pod::To::HTML' 'Pod::To::BigPage' 
               'File::Which' 'Shell::Command' 'LibraryMake' 'IO::String'
               'DateTime::Format' 'IO::Capture::Simple' 
               'Test::Util::ServerPort' 'JSON::RPC' 'Getopt::Long' 'TAP' 
               'Path::Finder' 'Pod::Usage' 'sigpipe' 'App::Prove6' 
               'LibraryCheck')

# Working modules with expected test failures...
testy_raku_mods=('Config::Parser::yaml' 'Config::Parser::json' 'LWP::Simple' 'Readline')

# Packages with dependency issues...
declare -A trouble_raku_mods=(["Config::Parser::toml"]="Config::TOML:ver<0.1.1>:auth<atweiden>")


for module in "${raku_packages[@]}"
do
	zef install "$module"
done

for module in "${testy_raku_mods[@]}"
do
	zef install --force-test "$module"
done

for module in ${!trouble_raku_mods[@]}
do 
    zef install --exclude="${trouble_raku_mods[${module}]}" "${module}"
done

Done.

How do I upgrade MoarVM and the Rakudo Star Bundle?

Build the latest version of MoarVM:

user $rakubrew build moar-<NEW_VERSION>

Once the build process is complete, switch to the new version of Raku:

user $rakubrew switch moar-<NEW_VERSION>

Confirm switching to the new version of MoarVM worked as expected:

user $raku --version

Install the most recent version of the Zef package manager

user $rakubrew build-zef

If you forget the above step, Zef will be missing!

Confirm switching to the new version of Zef worked as expected:

user $zef --version

Install the Rakudo Star modules via Zef:

Rerun the zef-install-star.sh module installation script. If you forget this step you will not have any of the modules from the Rakudo Star bundle.

Delete the old version of MoarVM, if desired:

user $rakubrew nuke moar-<OLD_VERSION>

Upgrade complete.

See also

  • MoarVMRakudo compiler's virtual machine for the Raku Programming Language.
  • NQP — a lightweight Raku-like environment for MoarVM, JVM, and other virtual machines.
  • Bash — the default shell on Gentoo systems and a popular shell program found on many Linux systems.
  • Perl — a general purpose interpreted programming language with a powerful regular expression engine.
  • Java — a programming language, originally developed by Sun Microsystems, which uses a platform-independent virtual machine to execute Java bytecode in real-time.

External resources

Learning Raku

Popular Raku Libraries and Frameworks

  • Sparrow a versatile automation framework written in Raku.
  • Cro Cro is a set of libraries for building distributed systems written in Raku.
  • Red an Object-Relational Mapping (ORM) database management tool for Raku.
  • Raku Roast Raku's test suite. Any implementation of Raku that passes Roast is a valid implementation.

Raku's Inner Workings