Syntactic SugarPerl offers many ways of making your programs more concise. Such conciseness may cause a dramatic increase in the 'cryptic coefficient', however! :)Once you learn these idioms of the language they can actually be much clearer than the more long-winded circuitous manner of writing. You can write code that is more to the point and direct. Brevity is the soul of wit. Michaelangelo is purported to have said:
List LiteralsInstead of:
use this:
You save all kinds of pesky punctuation! Here is another way of writing it which makes it easy to add or delete elements:@names = qw(one two three four);
For the delimiter after the 'qw'
you can use any non alphanumeric character.
(), {}, [], <> can be used in pairs.
You can't have elements with embedded spaces.
unless and untilInstead of negating the boolean of an if statement you can use the keyword 'unless' instead. These two are equivalent:
Similarily there is 'until':
Depending on how the boolean is worded these can sometimes be clearer.
'unless' and 'until' are not as commonly used in English
as 'if' or 'while'.
Statement modifiers.Remember when we said that even when there is only one statement in the body of an 'if' statement, still the curly braces are required? Statement modifiers to the rescue! Rather than this:
You can say this:
Using the new syntax saved 8 troublesome keystrokes!
Modifiers can also be used with qw/while for unless until/. English has such modifiers so their usage can sound quite natural:
A common use of such statement modifiers is with 'last' and 'next':
next if substr($line, 0, 1) eq "#"; last if $i > 10; The totally cryptic $_ - the default variableThis piece of code:
can be written more concisely:
The lines are being read into the variable $_
and it serves as the 'default' variable for
length and print. If you do not provide
a parameter for these two functions (and several others)
they will assume that you are talking about the
variable named $_. It is like a pronoun
such as 'it' or 'he'. It is assumed that
you know who you are talking about based
on context.
He pushed it for the next hour.
ShiftThe 'shift' function that operates on arrays (by shifting off the first element of the array) has a default argument.
WITHIN a subroutine
OUTSIDE of
a subroutine
For StatementThe for loop over an array can omit the loop variable. It will default to using $_.
and since there is only one statement in the body of the loop
the above can be made even more concise:
$tot += length for @names; Sort Subroutine SugarThere are many sweeteners for the sort subroutine. Instead of:
You can do this:
The "<=>" operator returns -1, 0 or 1 depending
on whether its left operand is numerically less than, equal,
or greater than its right operand. It is affectionately
named the "spaceship" operator
(after some shoot-em-up text-based game). If you want string
comparison use "cmp" instead.
Further, one does not really need to use the 'return' keyword. The last expression that is evaluated is the value that is returned. So we have:
For the final brevification one can put the body of
the sort subroutine between the keyword 'sort' and the array.
Clear as mud? Or as clear as an azure sky of deepest summer?
CautionBe careful with such compactions. They can quickly become cryptic and unreadable. There is a fine line between brevity and obfuscation.For an example of how this kind of 'sport' can be taken to an absurd extreme, take a look at Perl Golf! Exercises
|