In case you’ve forgotten it from high school, here is the table of and, or, and exclusive or:
|
First Value |
Second Value |
AND |
OR |
|
True |
True |
True |
True |
|
True |
False |
False |
True |
|
False |
False |
False |
False |
|
False |
True |
False |
True |
Perl recognizes the following characters for use in if, while, or other such blocks for determining whether and how often the block executes:
|
Character |
Meaning |
|
&& |
AND |
|
|| |
OR |
|
! |
NOT |
You can also use parentheses to force some logic to be interpreted before other logic. For example:
if ($isGod || ($isNietzsche && $godIsDead)) { ... }
will act on that if block if $isGod is true, or if both $isNietzsche is true and $godIsDead is true.
Perl has two basic types of comparisons: comparisons between text and comparisons between numbers. Remember that Perl doesn’t care whether a variable is text or a number until you use it in a manner that requires it to care.
|
Text Operator |
Numeric Operator |
Meaning |
|
eq |
== |
are the two items equal? |
|
ne |
!= |
are the two items unequal? |
|
lt |
< |
does the left item come before the right item? |
|
gt |
> |
does the left item come after the right item? |
|
lte |
<= |
is the left item before or equal to the right item? |
|
gte |
>= |
is the left item after or equal to the right item? |
|
cmp |
<=> |
-1 if the left item comes first, +1 if the right item comes first, and 0 if both items are the same |
There are a number of very useful tests you can perform on files. Remember that you’ll usually want to determine if the file exists first, especially for the tests that return a value such as a file size or age.
|
Test |
Tests |
|
-e |
the file exists |
|
-r |
the file is readable by your script |
|
-w |
the file is writable by your script |
|
-x |
the file is executable by your script |
|
-s |
the size of the file in bytes |
|
-f |
the file is a plain file, not a directory or alias |
|
-d |
the file is a directory |
|
-M |
the time since the file was last modified, in days |
Tests can be combined by using the filename for the first test, and the underscore for subsequent tests. For example, to test if a file exists and has been modified less than 24 hours ago:
if (-e $filepath && -M _ < 1) { ... }
There are many more tests for more specialized purposes, but these should cover most of your needs.
Remember that you can add an “i” to the end of a regular expression to make it ignore upper and lower case.
|
Character |
Meaning |
|
. |
a single character |
|
[characters] |
a single character from a list or range of characters |
|
[^characters] |
a single character not from a list or range of characters |
|
? |
zero or one of the preceding piece of text |
|
+ |
one or more of the preceding piece of text |
|
* |
zero or more of the preceding piece of text |
|
(text) |
group pieces of text for remembering later in $0 to $9 or for applying a ?, +, or * |
|
| |
choose between two or more options in a group of pieces |
|
^ |
anchor the expression to the beginning of the line or text being searched |
|
$ |
anchor the expression to the end of the line or text being searched |
|
\character |
backquote the next character so that it means what it is rather than its meaning as a regular expression. |
For example, you might use /Dean ([A-Z]\. )?Martin/ to match Dean Martin, Dean M. Martin, Dean Q. Martin, or Dean Z. Martin.
Use =~ to see if the item on the left matches the regular expression, and !~ to see if the item on the left does not match the regular expression.
If you’re using Mac OS X, I can’t recommend SilverService strongly enough. You may be aware that applications can provide services to service-aware software. They’ll show up in the “Services” submenu of every service-aware application’s application menu and can act on that application’s documents.
SilverService lets you put your scripts in that menu. Select text in a service-aware application (such as Smultron or Safari), choose your script from the Services menu, and the selected text will be piped to your script. Whatever your script prints out will replace your selected text. This is a great way to merge your command-line scripts with the Mac OS X GUI.
You can download SilverService from http://www.rho.org.uk/software/silverservice/
