[JoGu]

Cryptology

A Crash Course in Perl

a7Hzq .#5r<
kÜ\as TâÆK$
ûj(Ö2 ñw%h:
Úk{4R f~`z8
¤˜Æ+Ô „&¢Dø

$a »Scalar« variable (number, string).
[Perl evaluates the context; explicit deklaration not necessary.]
$a = 5; Assignment of a number.
[Assignments need a closing »;«.]
$a = "Dies ist ein Text."; Assignment of a string.

$a = <FILE>; Read a line from a file.
$a = <STDIN>; Read a line from standard input.
$a = <>; Read a line from the actual file; default is standard input.
$_ = <FILE>; Assign line to the »actual variable« $_.

while ($a = <>) {...} Assign line to the variable $a.
Condition is true, if there is a line, false, if end of file is met.
[Assignment blocks are enclosed in curly brackets.]
while (<>) {...} Equivalent with while ($_ = <>) {...}

$a =~ tr/ABC/XYZ/; Apply a string operator to $a,
here tr = »translation operator« = monoalphabetic substitution
A --> X, B --> Y, C --> Z
tr/ABC/XYZ/; Equivalent with $_ =~ tr/ABC/XYZ/;
tr/ABC/$a/; A --> $, B --> a, C --> $
eval "tr/ABC/$a/"; At runtime first evaluate $a, for example $a = "XYZ";
then execute tr, in the example: A --> X, B --> Y, C --> Z

print $a; Standard output; equivalent with print(STDOUT,$a);
print; Equivalent with print $_;


The strength of Perl is in programs that read a (text) file line by line, transform that line in a certain way, and then store the transformed line. A typical illustrative program looks like this:

while (<>) { # Read a line, if there is one left,
  ...;       # transform
  print;     # output.
  }

Author: Klaus Pommerening, 1999-Oct-26; last change: 2013-Oct-10.