Perl is an interpreted, procedural, language.
Often, Perl programs are referred to as scripts, but they differ from
Unix shell scripting language in that they require Perl to be loaded
as an optional system component. Scripts, however, are a series
of Unix commands that are executed as a batch.
This means that Perl is similar to Basic, in that both programs are
run through the interpreter process, rather than as their own process.
Unlike Java, which also requires an interpreter, Perl programs do not
need to be compiled before they are executed.
Perhaps the main advantage to Perl is that the interpreter is tightly
integrated with most web server programs (ie: Apache).
This allows Perl to be used as a common gateway interface (CGI) with
little modification.
Thus, a program written in Perl can be executed on the server, and the
output can be directed to either the console or a web browser.
The narrow width of the examples allows for easier export to mobile
computing platforms, such as Palm and 3G devices.
-----------------------------------------------------
#!/usr/path/to/perl
Identify file as a Perl program.
Must be first line.
Path may differ by server.
#!/usr/bin/perl
no output
-----------------------------------------------------
Exit condition
Halt program execution.
Must be last line.
Can be used anywhere.
exit 0;
no output
-----------------------------------------------------
Content-type
Tell display of output type.
* PC browser: text/html
print"Content-type:x/x\n\n";
Content-type:x/x
-----------------------------------------------------
Declare variables
Good programmers declare variables.
at the start of the program.
$one=1; $two=2;
$var1=0; $var2=0; $var3=0;
$j=0; $k=0;
no output
-----------------------------------------------------
$ENV{'VARIABLE'}
Read an environment variable.
Often used:
* QUERY_STRING
* HTTP_USER_AGENT
* REMOTE_ADDR
print"$ENV{'REMOTE_ADDR'}\n";
38.107.191.101
-----------------------------------------------------
if (what) {code}
Conditional if/then.
* ==, !=, <=, >=, <, >
* eq, ne, le, ge, lt, gt
* () && (), () || ()
print"$one==$one is ";
if ($one==$one) {
print"equal <br>\n";
}
1==1 is equal
-----------------------------------------------------
if (what) {code} else {code}
Two state if/then.
print"$one==$two is ";
if ($one==$two) {
print"equal <br>\n";
}else{
print"not equal <br>\n";
}
1==2 is not equal
-----------------------------------------------------
if (what) {code} elsif (what) {code}
Two state if/then.
print"$one==$two is ";
if ($one==$two) {
print"equal <br>\n";
}elsif ($one==$one) {
print"elusive <br>\n";
}
1==2 is elusive
-----------------------------------------------------
while (what) {code}
Conditional Loop.
print"VAR1: $var1 ";
while ($var1<=2) {
$var1++;
print"$var1 ";
}
VAR1: 0 1 2 3
-----------------------------------------------------
until (what) {code}
Conditional Loop.
print"VAR2: $var2 ";
until ($var2>=2) {
$var2++;
print"$var2 ";
}
VAR2: 0 1 2
-----------------------------------------------------
do {code} while (what)
Conditional Loop.
print"VAR3: $var3 ";
do {$var3++;
print"$var3 ";
} while ($var3<=2);
VAR3: 0 1 2 3
-----------------------------------------------------
for ($j=0; $j<4; $j++) {code}
Finite for/next loop.
print"J: ";
for ($j=0; $j<4; $j++) {
print"$j ";
}
J: 0 1 2 3
-----------------------------------------------------
for ($j=0; $j<4; $j--) {code}
Finite for/next loop, inverted.
print"J: ";
for ($j=3; $j>=0; --$j) {
print"$j ";
}
J: 3 2 1 0
-----------------------------------------------------
$new = $one + $two
Arithmatic equation: +, -, *, /, ()
$j=$one+$two;
print"+=$j ";
$k=$two*$j;
print"*=$k ";
$j=$k-$one;
print"-=$j ";
$k=$j/$two;
print"/=$k ";
$j=$one+($two*$k);
print"()=$j ";
+=3 *=6 -=5 /=2.5 ()=6
-----------------------------------------------------
abs($value)
Returns absolute value.
$j=-3.1415;
print"J: $j ";
$j=abs($j);
print"$j ";
J: -3.1415 3.1415
-----------------------------------------------------
int($value)
Returns integet value.
print"J: $j ";
$j=int($j);
print"$j ";
J: 3.1415 3
-----------------------------------------------------
rand
Generates a number between 0 and 1.
print"J: $j ";
$k=rand;
print"$k ";
print int($k*$j+1);
J: 3 0.0372451549201323 1
-----------------------------------------------------
sleep($value)
Pauses execute, in seconds.
sleep(2);
Done.