Perl 6
Perl 6 Lookup Tablet: Revision 41
Overview - Chapter: 0:Intr, 1:Hist, 2:Design, 3:Var, 4:Op, 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx - Appendices: A:Index, B:Tables, C:Best of, D:Delta, E:Links
Variables
Sigils
All variables in Perl begin with a sigil. They mark the 3 primary var types. The 4th sigil is for callables (routines) and the 5th is for layers (namespaces) that organize the previous 4.
$. . . . scalar, single value, can be of any data type
@ . . . ordered array, indexed list of scalar
% . . . unordered hash (associative array), 2 column table with unique IDs (keys) and associated values (value)
&. . . . code/rule/token/regex
:: . . . abstraction: package/module/class/role/subset/enum/type/grammar
The first 4 can also be used as prefix operators, called contextualizers, that force different contexts, but there are much more different contexts then 3.
Twigils
Twigil is short for secondary sigil. They follow after a primary sigil and mark special namespaces or variables with special meanings and properties.
$foo . . . ordinary scoping (no twigil)
$.foo. . . object attribute public accessor
$!foo. . . object attribute private storage
$^foo . . self-declared formal positional parameter
$:foo . . .self-declared formal named parameter
$*foo . . .contextualizable global variable
$?foo . . .compiler hint variable
$=foo . . .pod variable
$<foo> . .match variable, short for $/<foo> or even $/{'foo'}
$~foo . . .the foo sublanguage seen by the parser at this lexical spot
Special Variables
$_. . . . . . . . . context variable
$! . . . . . . . . . error msg
-
$/ . . . . . . . . . last created match object of this block
$<...> . . . . . . alias to named capture, see Twigils above
$0 .. $9 . . . . . alias to positional capture (most like in P5 - $1 .. $9)
@() . . . . . . . . array that holds $0 .. $9 and rest and positional submatches
-
$*EXECUTABLE_NAME . former $0
@*ARGS. . . . . Arguments (Parameter) to a program (script), formerly @ARGV
$*ARGFILES. . . magic command-line input handle
$*CWD. . . . . . current working directory (Str)
$*IN . . . . . . . standard input handle (command line mostly)
$*OUT. . . . . . standard output (command line mostly)
$*ERR . . . . . . standard error output (command line mostly)
$*PERL. . . . . . perl version running under
$*LANG. . . . . . LANG variable from %*ENV that defines what human language is used
@*INC . . . . . . include pathes (but not for std library modules), formerly @INC
$*KERNEL . . . . operating system running under
$*DISTRO . . . . SoftwarePackage # Which OS distribution am I running under
$*PID. . . . . . . ID of the running process,
$*GID. . . . . . . real global ID of the running process owner, formerly @(
$*UID. . . . . . . real user ID of the running process owner (user), formerly @<
$*EGID . . . . . . effective global ID, formerly @)
$*EUID . . . . . . effective user ID, formerly @>
$*ON_WARN. . . , formerly $SIG{__WARN__}
$*ON_DIE. . . . . , formerly $SIG{__DIE__}
$*EMERGENCY_MEMORY . . , formerly $^M
%*ENV . . . . . . , formerly %ENV
$*KERNEL. . . . . SoftwarePackage # operating system running under
$*DISTRO . . . . . SoftwarePackage # Which OS distribution am I running under
$*COMPILING . . .
$*DEBUGGING . . .
-
$?KERNEL . . . . . for which operating System was this program compiled?
$?DISTRO . . . . . Which OS distribution am I compiling under?
$?ARCH. . . . . . . SoftwarePackage # Host architecture
$?XARCH. . . . . . SoftwarePackage # Target architecture
$?VM . . . . . . . . Which virtual machine am I compiling under
$?XVM . . . . . . . Which virtual machine am I cross-compiling for
$?PERL . . . . . . . Which Perl am I compiled for?, $?PERL.version for formerly $^V $]
%?LANG . . . . . . Hash of Grammar # What is the current set of interwoven languages?
$?GRAMMAR. . . . current grammar
$?FILE . . . . . . . current filename of source file
$?MODULE. . . . . current module
$?PACKAGE . . . . current package
::?CLASS . . . . . current class (as package name)
$?CLASS. . . . . . current class
@?CLASS . . . . . current classes
$?ROLE . . . . . . . current role (as variable)
@?ROLE . . . . . . current roles
&?ROUTINE . . . . current sub or methode
@?ROUTINE . . . . current subs or methods
&?BLOCK . . . . . . reference to current block
$?LABEL. . . . . . . label of current block
$?LINE. . . . . . . . current line number
-
@=COMMENT. . . All the comment blocks in the file
$=DATA . . . . . . data block handle (=begin DATA ... =end)
@=DATA. . . . . . Same as above, but array
-
$~MAIN . . . . . . the current main language (e.g. Perl statements)
$~Q. . . . . . . . . the current root of quoting language
$~Quasi . . . . . . the current root of quasiquoting language
$~Regex . . . . . .the current root of regex language
$~Trans . . . . . . the current root of transliteration language
$~P5Regex . . . . the current root of the Perl regex language
Scopes
The following pseudo-package names are reserved at the front of a name:
.
MY . . . . . . Symbols in the current lexical scope (aka $?SCOPE)
OUR. . . . . . Symbols in the current package (aka $?PACKAGE)
CORE. . . . . Outermost lexical scope, definition of standard Perl
GLOBAL . . . Interpreter-wide package symbols, really CORE::GLOBAL
PROCESS . . Process-related globals (superglobals), CORE::PROCESS
COMPILING . Lexical symbols in the scope being compiled
CALLER. . . . Contextual symbols in the immediate caller's lexical scope
CONTEXT . . Contextual symbols in my or any caller's lexical scope
.
The following relative names are also reserved but may be used anywhere in a name:
.
OUTER . . . . Symbols in the next outer lexical scope
UNIT . . . . . Symbols in the outermost lexical scope of compilation unit
SETTING. . . Lexical symbols in the unit's DSL (usually CORE)
PARENT . . . Symbols in this package's parent package (or lexical scope)
.
The following is reserved at the beginning of method names in method calls:
.
SUPER . . . . Package symbols declared in inherited classes
low level data types
This is more low level than the last table. These types are more meant to give the compiler optimizing hints.
int1 . .
int2 . .
int4 . .
int8 . .
int16 . .
int32 . . (aka int on 32-bit machines)
int64 . . (aka int on 64-bit machines)
uint1 . . (aka bit)
uint2 . .
uint4 . .
uint8 . . (aka byte)
uint16. .
uint32. .
uint64. .
num32 . .
num64 . . (aka num on most architectures)
num128 . .
complex32 .
complex64 . (aka complex on most architectures)
complex128 .
Str . . .
Object Types
In Perl 6 any variable and value is an object. Here's a list of all different types of values, represented my different classes or roles. The routine types are in a different section.
Undefined types
Mu. . . . . . . . Most Undefined
Failure . . . . . Failure (lazy exceptions, thrown if not handled properly)
Any . . . . . . . Perl 6 object (default routine parameter type, excludes junction)
Cool. . . . . . . Perl 6 Convenient OO Loopbacks
Whatever . . . Wildcard (like Any, but subject to do-what-I-mean via MMD)
Int. . . . . . . . Any Int object
Widget . . . . . Any Widget object
Immutable types
Str. . . . . . Perl string (finite sequence of Unicode characters)
Bit . . . . . . Perl single bit (allows traits, aliasing, undef, etc.)
Int. . . . . . Perl integer (allows Inf/NaN, arbitrary precision, etc.)
Num. . . . . Perl number (approximate Real, generally via floating point)
Rat . . . . . Perl rational (exact Real, limited denominator)
FatRat . . . Perl rational (unlimited precision in both parts)
Complex . . Perl complex number
Bool. . . . . Perl boolean
Exception . Perl exception
Block . . . . Executable objects that have lexical scopes
Seq . . . . . A list of values (can be generated lazily)
Range. . . . A pair of ordered endpoints
Set . . . . . Unordered collection of values that allows no duplicates
Bag . . . . . Unordered collection of values that allows duplicates
Enum . . . . An immutable Pair
EnumMap. . A mapping of Enums with no duplicate keys
Signature. . Function parameters (left-hand side of a binding)
Parcel. . . . Arguments in a comma list
Slicel . . . . Arguments in a semicolon list (or equiv, like Z)
Capture. . . Function call arguments (right-hand side of a binding)
Blob . . . . . An undifferentiated mass of bits
Instant . . . A point on the continuous atomic timeline
Duration. . . The difference between two Instants
HardRoutine A routine that is committed to not changing
Mutable types
Iterator . . . . Perl list
SeqIter . . . . Iterator over a Seq
RangeIter. . . Iterator over a Range
Scalar . . . . . Perl scalar
Array. . . . . . Perl array
Hash . . . . . . Perl hash
KeySet. . . . . KeyHash of Bool (does Set in list/array context)
KeyBag. . . . . KeyHash of UInt (does Bag in list/array context)
Pair . . . . . . . A single key-to-value association
PairSeq. . . . . A Seq of Pairs
Buf . . . . . . . Perl buffer (a stringish array of memory locations)
IO. . . . . . . . Perl filehandle
Routine . . . . Base class for all wrappable executable objects
Sub. . . . . . . Perl subroutine
Method . . . . Perl method
Submethod. . Perl subroutine acting like a method
Macro . . . . . Perl compile-time subroutine
Regex . . . . . Perl pattern
Match . . . . . Perl match, usually produced by applying a pattern
Stash. . . . . . A symbol table hash (package, module, class, lexpad, etc)
SoftRoutine. . A routine that is committed to staying mutable
Object Introspection
WHAT . . . short name of the class that an object belongs to
WHICH. . . object ID (type)
WHO . . . . package, that support that object, long name in string context
WHERE. . . memory address of the object
HOW . . . . object of meta class: "Higher Order Workings"
WHEN . . . (reserved for events?)
WHY . . . . (reserved for documentation)
WHENCE . . autovivification of closures
Operators
Table of precedence
A . Level . . . . . . . . . . . . Examples
= . =========== . . . ==================
N . Terms . . . . . . . . . 42 3.14 "eek" qq["foo"] $x :!verbose @$array
L . Method postfix. . . . meth .+ .? .* .() .[] .{} .<> .«» .:: .= .^ .:
N . Autoincrement. . . . ++ --
R . Exponentiation. . . . **
L . Symbolic unary. . . . ! + - ~ ? | +^ ~^ ?^ ^
L . Multiplicative . . . . . * / % +& +< +> ~& ~< ~> ?& div mod
L . Additive . . . . . . . . + - +| +^ ~| ~^ ?| ?^
L . Replication. . . . . . . x xx
X . Concatenation . . . . ~
X . Junctive and . . . . . & also
X . Junctive or . . . . . . | ^
L . Named unary . . . . . sleep abs sin temp let
N . Nonchaining infix. . . but does <=> leg cmp .. ..^ ^.. ^..^
C . Chaining infix . . . . . != == < <= > >= eq ne lt le gt ge ~~ === eqv !eqv
X . Tight and. . . . . . . . &&
X . Tight or. . . . . . . . . || ^^ // min max
R . Conditional. . . . . . . ?? !! ff fff
R . Item assignment . . . = := ::= => += -= **= xx= .=
L . Loose unary . . . . . . true not
X . Comma operator . . . , p5=> :
X . List infix. . . . . . . . . Z minmax X X~ X* Xeqv ...
R . List prefix. . . . . . . . print push say die map substr ... [+] [*] any $ @
X . Loose and . . . . . . . and andthen
X . Loose or . . . . . . . . or xor orelse
X . Sequencer. . . . . . . <==, ==>, <<==, ==>>
N . Terminator. . . . . . . ; {...}, unless, extra ), ], }
Operator Associativity
Categories
category:<prefix>
circumfix:<( )>
dotty:<.>
infix:<+>
infix_circumfix_meta_operator:{'»','«'}
infix_postfix_meta_operator:<=>
infix_prefix_meta_operator:<!>
package_declarator:<class>
postcircumfix:<( )>
postfix:<++>
postfix_prefix_meta_operator:{'»'}
prefix:<++>
prefix_circumfix_meta_operator:{'',''}
prefix_postfix_meta_operator:{'«'}
q_backslash:<\\>
qq_backslash:<n>
quote_mod:<c>
quote:<q>
regex_assertion:<?>
regex_backslash:<w>
regex_metachar:<.>
regex_mod_internal:<i>
routine_declarator:<sub>
scope_declarator:<my>
sigil:<$>
special_variable:<$!>
statement_control:<if>
statement_mod_cond:<if>
statement_mod_loop:<while>
statement_prefix:<do>
term:<*>
trait_auxiliary:<is>
trait_verb:<of>
twigil:<?>
type_declarator:<subset>
version:<v>
They appear mostly before regular operators and give them different meaning or greater range.
In the example V stands for Value, L for left Value, and R for right. A number is the array index.
op= . . . self assign . . known from P5, $L <op>= $R equals $L = $L op $R
>>op . . hyper. . . . . . processes arrays in parallel or applies a single value to all array elements;
. . . . . . . . . . . . . . . . . @E = $L[0] op $R[0], $L[1] op $R[1], ...;
<<op . . hyper. . . . . . like above, points to the side which determens domensionality;
. . . . . . . . . . . . . . . . . @E = $L[0] op $R[0], $L[1] op $R[1], ...;
[op]. . . reduction . . . applies the operator between all elements of an array;
. . . . . . . . . . . . . . . . . $result = $V[0] op $V[1] op ...;
[\op] . . reduction . . . applies above reduction to a series of lists made of array slices ranging in length from 1 to the complete length of the original list;
. . . . . . . . . . . . . . . . . @result = $V[0], $V[0] op $V[1], $V[0] op $V[1] op $V[2], ...;
Rop . . . reverse. . . . . reverses the order of the operands
Xop . . . combinator . . performs the operator to all the pairs of the Cartesian product of two arrays;
. . . . . . . . . . . . . . . . . @result = $L[0] op $R[0], $L[0] op $R[1], $L[1] op $R[0], $L[1] op $R[1] }}
Unicode operators
The two hyper operators can be written with the Unicode "chevron" signs (also documented as "French Quotes") or with double less than or greater than signs (documented as "Texas Quotes").
« aka <<
» aka >>
Contextualizers
Infix operator that forcing a context.
$ . . . scalar context
@. . . array context (flat array)
@@ . slice c., array that may contain arrayref
%. . . hash c.
? . . . boolean
+ . . . numeric
~. . . string
| . . . capture (mix of positional (array) and named parameter (hash) )
& . . . coderef, routines
Filetest Ops
Use it as in "$file.:X" or "$file ~~ :X".
:r . . . file is readable by effective uid/gid.
:w. . . file is writeable by effective uid/gid.
:x . . . file is executable by effective uid/gid.
:o . . . file is owned by effective uid.
.
:R . . . file is readable by real uid/gid.
:W . . . file is writeable by real uid/gid.
:X . . . file is executable by real uid/gid.
:O . . . file is owned by real uid.
.
:e . . . file exists
:s . . . file has size greater than 0
.
:f . . . file is a plain file.
:d . . . file is a directory.
:l . . . file is a symbolic link.
:p . . . file is a named pipe (FIFO), or filehandle is a pipe.
:S . . . file is a socket.
:b . . . file is a block special file.
:c . . . file is a character special file.
:t . . . filehandle is opened to a tty.
.
:u . . . file has setuid bit set.
:g . . . file has setgid bit set.
:k . . . file has sticky bit set.
Smartmatch
Quoting Ops
The basic quoting operator (Q) does nothing, just taking literally what you quoted as a string. But with several adverbs, it behaves like the well known following ops:
Text Processing
Quoting Adverbs
There are a lot more adverbs to fine tune your quoting. The Adverbs can used with any quoting operator like: q:s/.../.
Regex Modifier
Stay behind the regex op and change the behaviour of the regex, search scope, etc.
Control Chars
These Escape Sequences will be evalueated to invisible control chars, if the ":b" aka ":backslash" quoting Adverbs are set (included in "", qq and <<>>). They are also usable in regexes and thatswhy also included in the next list.
\a . . . . . . . . BELL
\b . . . . . . . . BACKSPACE
\e . . . . . . . . ESCAPE
\f . . . . . . . . FORM FEED
\n . . . . . . . . LINE FEED
\r . . . . . . . . CARRIAGE RETURN
\t . . . . . . . . TAB
Escape Sequences
\0[ ... ] . Match a character given in octal (brackets optional).
\b. . . . . Match a word boundary.
\B. . . . . Match when not on a word boundary.
\c[ ... ] . Match a named character or control character.
\C[ ... ] . Match any character except the bracketed named or control character.
\d. . . . . Match a digit.
\D. . . . . Match a nondigit.
\e. . . . . Match an escape character.
\E. . . . . Match anything but an escape character.
\f. . . . . Match the form feed character.
\F. . . . . Match anything but a form feed.
\n. . . . . Match a (logical) newline.
\N. . . . . Match anything but a (logical) newline.
\h. . . . . Match horizontal whitespace.
\H. . . . . Match anything but horizontal whitespace.
\L[ ... ] . Everything within the brackets is lowercase.
\Q[ ... ] . All metacharacters within the brackets match as literal characters.
\r. . . . . Match a return.
\R. . . . . Match anything but a return.
\s. . . . . Match any whitespace character.
\S. . . . . Match anything but whitespace.
\t. . . . . Match a tab.
\T. . . . . Match anything but a tab.
\U[ ... ] . Everything within the brackets is uppercase.
\v. . . . . Match vertical whitespace.
\V. . . . . Match anything but vertical whitespace.
\w. . . . . Match a word character (Unicode alphanumeric plus "_").
\W. . . . . Match anything but a word character.
\x[ ... ] . Match a character given in hexadecimal (brackets optional).
\X[ ... ] . Match anything but the character given in hexadecimal (brackets optional).
Flow Control
Closure Traits
Every block can contain special named blocks (some are only for loops) that are started at certain times. They are traits (compile time property) of a block object. Those marked with a * can also be used within an expression as in "BEGIN my $x = 3 * 3;".
BEGIN {...}*. . . at compile time, ASAP, only ever runs once
CHECK {...}* . . at compile time, ALAP, only ever runs once
.
INIT {...}*. . . . at run time, ASAP, only ever runs once
END {...}. . . . . at run time, ALAP, only ever runs once
.
START {...}* . . on first ever execution, once per closure clone
ENTER {...}* . . at every block entry time, repeats on loop blocks.
LEAVE {...} . . . at every block exit time
KEEP {...} . . . . at every successful block exit, part of LEAVE queue
UNDO {...}. . . . at every unsuccessful block exit, part of LEAVE queue
.
FIRST {...}*. . . at loop initialization time, before any ENTER
NEXT {...} . . . . at loop continuation time, before any LEAVE
LAST {...} . . . . at loop termination time, after any LEAVE
.
PRE {...} . . . . . assert precondition at every block entry, before ENTER
POST {...} . . . . assert postcondition at every block exit, after LEAVE
.
CATCH {...} . . . catch exceptions, before LEAVE
CONTROL {...}. . catch control exceptions (like next/last/return etc), before LEAVE
Jump Commands
goto . . . . jump to a named label
redo . . . . repeat this loop turn
next . . . . skip to the next loop turn
last. . . . . leave this loop now
break. . . . leave this when clause
leave. . . . leave this block with a return value
return . . . leave this routine with a return value
Conditionals
if . . . . . . when following expression evals in boolean context to "Bool::True", the block that following after that will be executed
elsif . . . . works like if, but only recognized if no preceding "if" or "elsif" clause was executed
else . . . . following block will be executed, when no preceding "if" or "elsif" clause was executed
unless. . . opposite of if, no "elsif" or "else" is allowed to follow
given . . . evals an expression into scalar context assignes it to $_ for the following block
when . . . smartmatches an expression against $_; if the result is "Bool::True", the following block will be executed
default . . following block will be executed, when no "when" clause was executed
Loops
loop . . . . general (endless) loop, unless used as an C-style-loop, evals following expression into void context
repeat. . . initial command for while or until loops that have their condtion at the end
while. . . . loop with negative exit condition (exit when false), condition, evals expression into boolean context
until . . . . loop with positive exit condition(exit when true), evals expression into boolean context
for . . . . . evals expression into lazy list context and iterates over that list from first to last value, sets each time $_ (can be combined with when as well),
Routine Types
sub. . . . . . . . normal routine, named block with parmeters
method . . . . . inheritable object methods
submethod. . . not inheritable methods
regex. . . . . . . routine that executes a regular expression
rule. . . . . . . . alias to regex :ratchet :sigspace
token . . . . . . alias to regex :ratchet
macro . . . . . . routine that is executed at BEGIN (ASAP, compile time) and returns an AST
Routine Modifier
multi. . . . . marks routines, witch can have siblings with same name but different signature; when called, the one with matching sig is executed
only . . . . . routines which dont' allow to have an multi sibling (is default, you may leave it out)
proto . . . . fallback for multi, if no multi signature matches the caller, a proto with same name is executed
Routine Traits
phasers (closure traits) can also be seen as routine traits
will do . . . . block of code executed when the subroutine is called. Normally declared implicitly, by providing a block after the subroutine's signature definition
signature . . signature of a subroutine. Normally declared implicitly, by providing a parameter list and/or return type
as. . . . . . . inner type constraint that a routine imposes on its return value
of. . . . . . . official return type of the routine
cached. . . . marks a subroutine as being memoized
rw. . . . . . . marks a subroutine as returning an lvalue
parsed . . . . macro is parsed once, is hygienic, only parsed can be used
reparsed. . . macro parsed twice, not hygienic, later parsed can be used
tighter. . . . specifies the precedence of an operator higher than an existing operator as seen here
looser . . . . specifies the precedence of an operator lower than an existing operator as seen here
equiv. . . . . specifies the precedence of an operator same as an existing operator as seen here
assoc. . . . . specifies the associativity of an operator explicitly as seen here
Parameter Traits
Overview - Chapter: 0:Intr, 1:Hist, 2:Design, 3:Var, 4:Op, 5:IO, 6:{}, 7:Sub, 8:OOP, 9:Rx - Appendices: A:Index, B:Tables, C:Best of, D:Delta, E:Links
|