Main Content

Perl - a quick reminder and revision. Test yourself!

Archive - Originally posted on "The Horse's Mouth" - 2011-08-26 19:41:52 - Graham Ellis

At the start of the second day of my second Perl course this week (yes, really!!) I put up a quick revision of what I had covered on the first day for my not - really - new - to - Perl group. How many of these things are you familiar with?

Comments ...

Don't forget to comment your code!
- # to end of line
- after __END__ on the end of the file
- "Documentation comments" via POD ... =head (etc) to =cut

Variables ...

$ - scalar. Integer, Float, String ... Reference, Regex
@ - list. Indexed ordered collection of scalars []
% - hash. Keyed unordered collection of scalars {}
& - code. a.k.a. a "sub"
  - file handle [no special start character)
* - typeglob - one of each of the above

Strings ...

" and ' (operator and literal). operator expanding $ @ \
Also qq and q, and "here documents"

Testing ...

== numeric equality !=
eq string equality ne
=~ string match !~

Regular Expressions contain ...

- literals. A 3 @ \+ \.
- char groups [A-Z] [^aeiou] . \s \d \w \W \D \S
- counts * ? + {2,6}
- anchors ^ $ \b
- captures ()
- alternation |
/.../ m!...! m/.../

Special Variables...

$& - the bit that matched
@_ - parameters to a sub
- first interesting bit ...
@ARGV - incoming parameters to program
%ENV - environment variables
@INC - where modules may load from
%INC - where modules have been loaded from
$_ - default input and pattern matching space

Conditionals ...

if, unless, and, or, &&, ||, ? ... :

Loops ...

while, for, foreach, goto (yuk!), do while, until
last, next, redo

On a public Perl course, I would expect to take much longer than just one day to cover this little lot!