PerlIntro
1. Scalars: A scalar represents a single value. The values can be strings, integers or floating point number, and Perl will automatically convert between them as required. Begin with “$”
Arrays: An array represents a list of values. Begin with “@”
Hashes: A hash represents a set of key/value pairs.
2. my creates lexically scoped variables. Using my in combination with a use strict; as the top of your Perl scripts means that the interpreter will pick up certain common programming errors.
3. if
if ( condition ) {
...
} elseif ( other condition ) {
...
} else {
...
}
There's also a negated version of it:
unless ( condition ) {
...
}
# the Perlish post-condition way
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;
while
while ( condition ) {
...
}
There's also a negated version, for the same reason we have unless:
until ( condition ) {
...
}
You can also use while in a post-condition:
print "LA LA LA\n" while 1;
for
Exactly like C:
for ($i = 0; $i <= $max; $i++) {
...
}
The C style for loop is rarely needed in Perl since Perl provides the more friendly list scanning foreach loop.
Foreach
foreach (@array) {
print "This element is $_\n";
}
print $list[$_] foreach 0 .. $max;
# you don't have to use the default $_ either...
foreach my $key (keys %hash) {
print "The value of $key is $hash{$key}\n";
}
-Katrina
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment