Cover | Table of Contents | Colophon
$ for scalars, @ for
arrays, and % for hashes. The sigils provide a
valuable visual distinction by making it immediately obvious what
kinds of behavior a particular variable is likely to have. But,
fundamentally, there's little difference between the
three. Each variable is essentially a container for a value, whether
that value is single or collective. (This statement is an
oversimplification, as you'll soon see.)$ for scalars, @ for
arrays, and % for hashes. The sigils provide a
valuable visual distinction by making it immediately obvious what
kinds of behavior a particular variable is likely to have. But,
fundamentally, there's little difference between the
three. Each variable is essentially a container for a value, whether
that value is single or collective. (This statement is an
oversimplification, as you'll soon see.)$string = "Zaphod's just this guy, you know?";
$int = 42;
$float = 3.14159;
$arrayref = [ "Zaphod", "Ford", "Trillian" ];
$hashref = { "Zaphod" => 362, "Ford" => 1574, "Trillian" => 28 };
$subref = sub { print $string };
$object = Android.new;
$filehandle = open $filename;
@ sigil is part of the name of the variable
and stays the same no matter how the variable is used:@array = ( "Zaphod", "Ford", "Trillian" ); $second_element = @array[1]; # Ford
.length method. The
.last method returns the index of the last element
in an array—that is, the highest index in an array.=
operator is for ordinary assignment.
It creates a copy of the values on the right-hand side and assigns
them to the variables or data structures on the left-hand side:$copy = $original; @copies = @originals;
$copy and $original both have
the same value, and @copies has a copy of every
element in @originals.:=
operator is for binding assignment.
Instead of copying the value from one variable or structure to the
other, it creates an alias. An alias is an additional entry in the
symbol table with a different name for the one container:$a := $b; # $a and $b are aliases @c := @d; # @c and @d are aliases
$a also changes
$b, because they're just two
separate names for the same container. Binding assignment requires
the same number of elements on both sides, so both of these would be
an error:# ($a, $b) := ($c); # error # ($a, $b) := ($c, $d, $e); # error
::= operator is a variant of the binding
operator that binds at compile time.+),
subtraction (-),
multiplication
(*),
division (/),
modulus (%), and
exponentiation
(**). Each has a corresponding
assignment operator
(+=,
-=, *=, /=,
%=, **=) that combines the
arithmetic operation with assignment:$a = 3 + 5; $a += 5; # $a = $a + 5
++) and
autodecrement (--) operators. The prefix operators
modify their argument before it's evaluated, and the
postfix operators modify it afterward:if,
unless, and given.if
statement checks a condition and
executes its associated block only if that condition is true. The
condition can be any expression that evaluates to a truth value.
Parentheses around the condition are optional:if $blue {
print "True Blue.";
}
if statement can also have an unlimited number
of elsif statements that check additional
conditions when the preceding conditions are false. The final
else statement executes if all preceding
if and elsif conditions are
false:if $blue {
print "True Blue.";
} elsif $green {
print "Green, green, green they say...";
} else {
print "Colorless green ideas sleep furiously.";
}
unless
statement is the logical opposite of
if. Its block executes only when the tested
condition is false:unless $fire {
print "All's well.";
}
elsunless statement, though
else works with unless.given expression, the switch, to a
series of when statements, the cases. When a case
matches the switch, its block is executed:given $bugblatter {
when Beast::Trall { close_eyes( ); }
when 'ravenous' { toss('steak'); }
when .feeding { sneak_past( ); }
when /grrr+/ { cover_ears( ); }
when 2 { run_between( ); }
when (3..10) { run_away( ); }
}sub
keyword, followed by the name of the
sub, followed by the block that defines the sub:sub alert {
print "We have normality.";
}
@_
array:sub sum {
my $sum;
for @_ -> $number {
$sum += $number;
}
return $sum;
}
sub standardize ($text, $method) {
my $clean;
given $method {
when 'length' { $clean = wrap($text, 72); }
when 'lower' { $clean = lowercase($text); }
...
}
return $clean;
}
sub whole (@names, %flags) {
...
}
# and elsewhere
whole(@array, %hash);
$first is bound to @array[0]
and $second is bound to
@array[1]:sub flat ($first, $second) {
...
}
flat(*@array);
@names[0] is bound to $zaphod,
and @names[1] to $ford:sub slurp (*@names) {
...
}
slurp($zaphod, $ford);
@_ array. In fact, a simple subroutine without
a signature actually has an implicit signature of
*@_:class or module declaration
statement in a file. All code that follows is defined in the
Heart::Gold namespace:class Heart::Gold; # class definition follows ...
class Heart::Gold {
# class definition enclosed
...
}
new
method. A default new
method is provided in the universal base class
Object
:$ship = Heart::Gold.new(length => 150);
has keyword, and always have a
"." after the
sigil:class Heart::Gold {
has $.height;
has $.length;
has @.cargo;
has %.crew;
...
}
$obj.height( ) # returns the value of $.height
is public trait:has $.height is public;
/.../ syntax, or using
subroutine-like syntax with the keyword rule.
Table 4-2 shows the basic syntax for defining
rules.|
Syntax
|
Meaning
|
|---|---|
m/.../
|
Match a pattern (immediate execution).
|
s/.../.../
|
Perform a substitution (immediate execution).
|
rx/.../
|
Define an anonymous rule (deferred execution).
|
/.../
|
Immediately match or define an anonymous rule, depending on the
context.
|
object = some_constructor(1, 2, "foo"); object.bar(12);
object is written in, if object
even has a class backing it. It could be Perl 5, Perl 6, Python,
Ruby, or even Java, C#, or Common Lisp; it doesn't
matter.DESTROY
method, has its destruction routine
called. The dead object detector is triggered whenever Parrot runs
out of free objects, and can be explicitly triggered by running code.
Often a language compiler will force a dead object sweep when leaving
a block or subroutine.http://www.parrotcode.org and
the documentation in the distributed code.$ perl Configure.pl $ make $ make test
$ make test IMCC=languages/imcc/imcc
print "He's pining for the fjords.\n" end
$ ./assemble.pl fjord.pasm --output fjord.pbc
--output (or -o) switch.
.pbc is the standard extension for Parrot
bytecode. Finally, run the compiled bytecode file through the
http://www.parrotcode.org and
the documentation in the distributed code.$ perl Configure.pl $ make $ make test
$ make test IMCC=languages/imcc/imcc
print "He's pining for the fjords.\n" end
$ ./assemble.pl fjord.pasm --output fjord.pbc
--output (or -o) switch.
.pbc is the standard extension for Parrot
bytecode. Finally, run the compiled bytecode file through the
parrot interpreter:$ ./parrot fjord.pbc