Type | Character | Example | Is a name for: |
---|---|---|---|
Scalar | $ | $cents | An individual value (number or string) |
Array | @ | @large | A list of values, keyed by number |
Hash | % | %interest | A group of values, keyed by string |
Subroutine | & | &how | A callable chunk of Perl code |
Typeglob | * | *struck | Everything named struck |
eg:
1 .$camels = '123';
print $camels + 1, "/n";
2.@home = ("couch", "chair", "table", "stove");
($potato, $lift, $tennis, $pipe) = @home;
If you want to assign to one array element at a time, you could write the earlier assignment as:
$home[0] = "couch"; $home[1] = "chair"; $home[2] = "table"; $home[3] = "stove";($alpha,$omega) = ($omega,$alpha);
3.These are called list assignments. They logically happen in parallel, so you can swap two variables by saying:
4.%longday = ("Sun", "Sunday", "Mon", "Monday", "Tue", "Tuesday",
"Wed", "Wednesday", "Thu", "Thursday", "Fri",
"Friday", "Sat", "Saturday");
Because it is sometimes difficult to read a hash that is defined like this, Perl provides the =>
(equal sign, greater than) sequence as an alternative separator to the comma. Using this syntax (and some creative formatting), it is easier to see which strings are the keys, and which strings are the associated values.
%longday = ($wife{"Adam"} = "Eve";
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
);
5.Linguistically, the relationship encoded in a hash is genitive or possessive, like the word "of" in English, or like "'s". The wife of Adam is Eve, so we write:
二,
Since you can use the open function to create filehandles for various purposes (input, output, piping), you need to be able to specify which behavior you want. As you would do on the UNIX command line, you simply add characters to the filename.
open(SESAME, "filename"); # read from existing fileprint STDOUT "Enter a number: "; # ask for a number
open(SESAME, "<filename"); # (same thing, explicitly)
open(SESAME, ">filename"); # create file and write to it
open(SESAME, ">>filename"); # append to existing file
open(SESAME, "| output-pipe-command"); # set up an output filter
open(SESAME, "input-pipe-command |"); # set up an input filter
The empty angle operator,<>
, will read lines from all the files specified on the command line, orSTDIN
, if none were specified. (This is standard behavior for many UNIX filter programs.)
$number = <STDIN>; # input the number
print STDOUT "The number is $number/n"; # print the number
三. operators
Example | Name | Result |
---|---|---|
$a + $b | Addition | Sum of $a and $b |
$a * $b | Multiplication | Product of $a and $b |
$a % $b | Modulus | Remainder of $a divided by $b |
$a ** $b | Exponentiation | $a to the power of $b |
String Operators
There is also an "addition" operator for strings that does concatenation. Unlike some languages that confuse this with numeric addition, Perl defines a separate operator (.
) for string concatenation:
$a = 123;
$b = 456;
print $a + $b; # prints 579
print $a . $b; # prints 123456
There's also a "multiply" operation for strings, also called the repeat operator. Again, it's a separate operator (x) to keep it distinct from numeric multiplication:
$a = 123;
$b = 3;
print $a * $b; # prints 369
print $a x $b; # prints 123123123
print $a . ' is equal to ' . $b . "/n"; # dot operator
print $a, ' is equal to ', $b, "/n"; # list
print "$a is equal to $b/n"; # interpolation
Example | Name | Result |
---|---|---|
$a && $b | And |
|
$a || $b | Or |
|
! $a | Not | True if $a is not true |
$a and $b | And |
|
$a or $b | Or |
|
not $a | Not | True if $a is not true |
Comparison | Numeric | String | Return Value |
---|---|---|---|
Equal | == | eq | True if $a is equal to $b |
Not equal | != | ne | True if $a is not equal to $b |
Less than | < | lt | True if $a is less than $b |
Greater than | > | gt | True if $a is greater than $b |
Less than or equal | <= | le | True if $a not greater than $b |
Comparison | <=> | cmp | 0 if equal, 1 if $a greater, -1 if $b greater |
Example | Name | Result |
---|---|---|
-e $a | Exists | True if file named in $a exists |
-r $a | Readable | True if file named in $a is readable |
-w $a | Writable | True if file named in $a is writable |
-d $a | Directory | True if file named in $a is a directory |
-f $a | File | True if file named in $a is a regular file |
-T $a | Text File | True if file named in $a is a text file |
四, Breaking out: next and last
The next and last operators allow you to modify the flow of your loop. It is not at all uncommon to have a special case; you may want to skip it, or you may want to quit when you encounter it. For example, if you are dealing with UNIX accounts, you may want to skip the system accounts (like root or lp). The next operator would allow you to skip to the end of your current loop iteration, and start the next iteration. The last operator would allow you to skip to the end of your block, as if your test condition had returned false. This might be useful if, for example, you are looking for a specific account and want to quit as soon as you find it.
foreach $user (@users) {
if ($user eq "root" or $user eq "lp") {
next;
}
if ($user eq "special") {
print "Found the special account./n";
# do some processing
last;
}
}
It's possible to break out of multi-level loops by labeling your loops and specifying which loop you want to break out of. Together with statement modifiers (another form of conditional we haven't talked about), this can make for very readable loop exits, if you happen to think English is readable:
LINE: while ($line = <ARTICLE>) {
last LINE if $line eq "/n"; # stop on first blank line
next LINE if $line =~ /^#/; # skip comment lines
# your ad here
}