πŸ“˜ Variables in Perl 6: Twigils

πŸ“˜ Variables in Raku: Twigils

N. B. Perl 6 has been renamed to Raku. Click to read more.


In Perl 6, a variable name may be preceded by either a single-character sigil, such as $, @ or %, or with a double character sequence. In the latter case, this is called a twigil. The first character of it means the same thing that a bare sigil does, while the second one extends the description.

For example, the second character of the twigil can describe the scope of the variable. Consider *, which symbolises dynamic scope (more on this in Chapter 3). The following call prints the command line arguments one by one:

.say for @*ARGS;

Here, the @*ARGS array is a global array containing the arguments received from the command line (note that this is called ARGS and not ARGV as in Perl 5). The .say construction is a call of the say method on a loop variable. If you want to make it more verbose, you would write it like this:

for @*ARGS {
Β Β Β  $_.say;
}

Let’s list a few other useful predefined dynamic variables with the star in their twigils. The first element of the twigil denotes the type of a container (thus a scalar, an array, or a hash):

$*PERL contains the Perl version (Perl 6)

$*PID β€” process identifier

$*PROGRAM-NAME β€” the name of the file with the currently executing programme (for a one-liner its value is set to -e)

$*EXECUTABLE β€” the path to the interpreter

$*VM β€” the name of the virtual machine, which your Perl 6 has been compiled with

$*DISTRO β€” the name and the version of the operation system distribution

$*KERNEL β€” similar, but for the kernel

$*CWD β€” the current working directory

$*TZ β€” the current timezone

%*ENV β€” the environment variables

In my case, the variables above took the following values:

Perl 6 (6.c)
90177
twigil-vars.pl
"/usr/bin/perl6".IO
moar (2016.11)
macosx (10.10.5)
darwin (14.5.0)
"/Users/ash/Books/Perl 6/code".IO
{Apple_PubSub_Socket_Render => /private/tmp/com.apple…., DISPLAY => /private/tmp/com.apple…, HISTCONTROL => ignorespace, HOME => /Users/ash, LC_CTYPE => UTF-8, LOGNAME => ash ...

The next group of the predefined variables include those with the ? character as their twigil. These are β€œconstants” or so-called compile-time constants, which contain information about the current position of the programme flow.

$?FILE β€” the name of the file with a programme (no path included; contains the string -e for one-liners)

$?LINE β€” the line number (is set to 1 for one-liners)

$?PACKAGE β€” the name of the current module; on a top level, this is (GLOBAL)

$?TABSTOP β€” the number of spaces in tabs (might be used in heredocs)

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code