Perl

Table of Contents:
\ % # Assignment
Command-Line Environment Examples Format
Functions Io Looping Math
Operators Pattern Precedence Random
Stacks Subroutines Switch Text
Unix Variables Windows


\
\\ backslash \" double-quote \a bell
\b backspace \cC control-'C' \e escape
\E terminate \L or \U \f formfeed \l lowercase next char
\L lowercase 'til \E \n newline \r carriage return
\t tab \u uppercase next char \U uppercase 'til \E
\v vertical tab \177 octal 'delete' \ooo octal char
\xhh hex char


%
%%
%
%s
string
%2d
2-space integer
%02d
2-space integer (leading zeros)
%2c
2-space character
%6.2f
6-space, 2-decimal floating point number


#
#
comment line(everything after is ignored)
#!/usr/local/bin/perl
has to be the first line of each (unix) perl program
use diagnostics;
explain warnings in greater detail
use strict;
generates errors for unsafe variable reference & subroutine constructs.


Assignment
$a = $b = $c;
this works from right to left. $b = $c & then $a = $b.
+= -= *= /= %= **= .=(string concatenation)


Command-Line
-c
check syntax only. do not run.
-d
perl debugger
-e
specify a line of perl code on the command line
-i
ammounts to changing a file in place.
perl -i.old -p -e "s/foo/bar/g" wiffle.bat
(take each line of wiffle.bat (-p), store the original in (wiffle.old)(-i), replace all foo with bar (-e) and write result(-p) to the original file(-i))
-p
assume 'while (<>) { }' around the line of command-line code
-P
run through the c pre-processor before perl compiles it.
-T
don't evaluate expressions given on the command line. Useful with doing web work.
-w
prints warnings only about dubious constructs
-W
prints all warnings
-0
use the NULL character as a record separator
-W0
combined -W and -0
.pl
perl library file
.pm
perl module file


Environment
%ENV
environment variables associative array.
$ENV{'PATH'}
returns path


Format
example:
     .
     format someformatname =
     fieldline
     value1, value2, value3
     fieldline
     value1, value2
     .
     .
     format ADDRESSLABEL =
     ====================================
     | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
     $name
     | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
     $address
     | @<<<<<<<<<<<<<<<<<<<<<, @< @<<<< |
     $city, $state, $zip
     ====================================
     .
   
Using the ADDRESSLABEL format:
     open(ADDRESSLABEL,">file-to-write-to");
     ($name,$address,$city,$state,$zip) = split(/:/);
     #read in the fields
     write ADDRESSLABEL; #this will write appropriately
  
STDOUT
to write the label to STDOUT, name the label such.
@<<<<<<
left justified field data
@>>>>>>
right-justified field data
@||||||
centered field data
^<<<<<<
line continuation (break on word boundaries)
preceed with ~ to ignore of no data left
preceed with ~~ to go until no data is left
@###.##
number format
      to include special signs (-/$/etc...) use the sub:
      sub cool
      {
       local($n,$width) = @_;
       #back off for negative stuff;2
       $width -= 2; 
       @n = sprintf("%.2f",$n);
       if ($n < 0)
       {
        #negative numbers get brackets
        sprintf("[%$width.2f]", -$n);
       }
       else
       {
        #positive #'s get spaces
        sprintf(" %$width.2f]", $n);
       }
      }
  
@*
if the variable has \n's in it, this has to be between the fieldline and its associated variables


Functions
crypt PLAINTEXT,SALT
-encrypt data. Useful for password entry.
     $pwd = (getpwuid($<))[1];
     $salt = substr($pwd, 0, 2);
     system "stty -echo";
     print "Password: ";
     chop($word = );
     print "\n";
     system "stty echo";
     if(crypt($word, $salt) ne $pwd)
     { die "Sorry...\n"; }
     else { print "ok\n"; }
   
defined EXPR
returns boolean value wether EXPR has a real value or not.
to distinguish between undefined null scalar & defined null scalar.
do 'stat.pl';
execute contents of file 'stat.pl' as a perl script. primarily used for including subroutines from a perl library. superceedes 'eval'.
fcntl FILEHANDLE,FUNCTION,SCALAR
implements fcntl(2) function. Will probably need 'use Fcntl;'
     use Fcntl;
     fcntl($filehandle, F_GETLK, $packed_return_buffer);
   
see also: flock, ioctl


Io
rename
rename($old,$new); or system("mv",$old,$new);
single-character input
     my $BSD_STYLE = 1;
     sub getsingle()
     {
      if ($BSD_STYLE)
      { system "stty cbreak /dev/tty 2>&1"; }
      else
      { system "stty", '-icanon', 'eol', "\001"; }
      my $tmp = getc(STDIN);
      if ($BSD_STYLE)
      { system "stty -cbreak /dev/tty 2>&1"; }
      else
      { system "stty", 'icanon', 'eol', '^@'; # ascii null }
      print "\n";
      return $tmp;
     }
   
standard filehandles
STDIN keyboard ($a = <STDIN>;)
STDOUT
screen
STDERR
error output
open(FILEHANDLE,"<filename");
open a file for reading (returns T/F)
open(FILEHANDLE,">filename");
open a file for writing (returns T/F)
open(FILEHANDLE,">>filename");
open a file for appending (returns T/F)
open(FILEHANDLE,"+<filename");
open a file for reading & writing
open(FILEHANDLE,"+>filename");
open a file for reading & writing
open(FILEHANDLE,"|name");
command to pipe output to
open(FILEHANDLE,"name|");
command that pipes into us
close(FILEHANDLE);
opendir
closedir(DIRHANDLE)
print OUTFILE "text";
print "text\n","text2\n";
while () { print OUT $_; }
print a file by line
translate '~' to a filename
     $filename =~ s{
     ^ ~             # find a leading tilde
     (               # save this in $1
      [^/]        # a non-slash character
          *     # repeated 0 or more times (0 means me)
           )
     }{
       $1
       ? (getpwnam($1))[7]
       : ( $ENV{HOME} || $ENV{LOGDIR} )
        }ex;
   
read a file by paragraphs
use $\. set it to '""' to eliminate empty paragraphs or '\n\n' to accept empty paragraphs.
File Tests: if (-x $filename) { }
-r readable -w writable
-x executable -o owned by user
-e exists -z exists & 0 size
-s size in bytes -f plain file
-d directory -l symlink
-k sticky-bit set -T text file
-B binary file -M modification age (days)
-A access age in days -C inode-modification age
stat(FILEHANDLE);
returns size 13 array of file info ($def,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) (see also: stat(2))
if (-r $filevar && -w _)
tests the file twice with 1 system call
test existance on open
unless (open(FILE,"<$FILENAME")) { print " "; exit; }


Looping
do { <block> } while( <condition> );
for ( initial; while condition; inc/dec ) { }
foreach $i (@list) { }
$i is local to the loop, is restored when exited.
changing $i locally changes the @list elements!
foreach (@list) { }
$_ is the effective loop variable
last;
exit current loop
next;
skip directly to the next instance in the loop
redo;
pass control directly to innermost loop (or LABEL)
until (condition) { }
while (condition) { }
while (<>) { print $_; } (/bin/cat)


Math
atan2(Y,X)
arctangent of Y/X in the range of -PI to PI
cos(EXPR)
cosine of EXPR in radians. takes $_ if ommited
eval '$answer = $a / $b'; warn $@ if $@;
1/0 non-fatal
exp EXPR;
exponential.


Operators
Numbers
+ - * / % (always double)
** (power)
< <= == >= > !=
++ -- (also works with floats)
<=> (returns -1(less),0(equal),1(more)
Strings
subject strings numbers
concatenation .  
equality eq ==
not equal ne !=
less than < <
greater than > >
less than/equal <= <=
greater than/equal >= >=
<>
@ARGV array
cmp
returns -1(less),0(equal),1(more). numbers & strings are 'truncated'.
repetition operator
'"fred" x 3' is "fredfredfred"
" "
de-compose variables within
' '
literal string (doesn't change variables


Pattern
grep
if (/abc/) { print "$_"; } - single line
=~ (with string other than $_)
in front of the slashes
s replace ! not
tr/searchlist/replacementlist/
translates all occurrences of the characters found in the searchlist with the corresponding character in the replacement list
match-operator optional switches
replace all (cannot be used with tr)
case insensitive
complement the searchlist
delete found but unreplaced characters
squash duplicate replaced characters
evaluate the regular expression one time only
matching inside the slashes
^ start of string $ end of string \b word boundry
\B not word boundry \d digits \D not-digits
\f form feed \t tab \n newline
\r carriage return \w alphanumeric \W not alphanumeric
\s whitespace \S not whitespace a|b match only one of a or b
atoms (Regular Expressions)
.
any character except \n (b.b = bob ! bb)
[ab]
any of the included characters (^[Bb] = Bob,bob ! Rbob)
[a-c]
same as [abc]
atom quantifiers
* 0 or more + 1 or more
? 0 or 1 {n} 'n' instances
{n,} at least 'n' instances {n,m} at least n, at most m
after the match
\1, \2, \3
the 'words' from a previous match
$&
part that matched $' (the part before)
$'
part after
examples
warn "has nondigits" if /\D/;
warn "not a whole number" unless /^\d+$/;
warn "not an integer" unless /^-?\d+$/; # reject +3
warn "not an integer" unless /^[+-]?\d+$/;
warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2
warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/;
warn "not a C float" unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
s/\..*//; (remove filename extensions)


Precedence
Lowest precedence on top
associativity operator
none the 'list' operators
left ,
right += ('assignment' operators)
right ?: (trinary if/then/else)
none .. (range operator, list constructor)
left || (or)
left && (and)
left | ^ (bitwise or & exclusive or)
left & (bitwise and)
none == != <=> eq ne cmp ('equal' operators)
none < <= > >= lt le gt ge ('not-equal' operators)
none named unary operators
none file test operators
left << >> (bit shift)
left + - .(string concatenation)
left * / % x(string repetition)
left =~(match) !~(doesn't match)
right ** (exponentiation)
right ! ~(bitwise not) -
none ++ --


Random
bring in a random line from a file
srand;
rand($.) < 1 && ($line = $_) while <>;


Stacks
push(@mylist,3,4,5);
adds 3,4,5 to the end of @mylist
$a = pop(@mylist);
removes last member of @mylist to $a & returns undef if empty
unshift(@list,3,4,5);
adds 3,4,5 to beginning of @list
$x = shift(@mylist);
removes first member of @mylist to $x & returns undef if empty


Subroutines
sleep(sec)
sleeps the terminal for the specified number of seconds
read(handle,variable,integer - bytes of data);
reads 'integer' bytes from file 'handle' into a variable
seek(filehandle,position,start);
sets file pointer within a file.
handle: filehandle
position: number of bytes to move (relative to 'start')
start: (0=start,1=current position,2=end of file)
tell(filehandle);
returns position in a file (see: seek) & defaults to last file accessed
($s,$m,$h,$d,$mo,$y,$wd,$yd,$isdst)=localtime(time);$mo++;$y+=1900;
second, minute, hour, day, month(starts at 0. add 1), year(since 1900. add 1900), weekday(days since sunday(0-6), days since jan. 1 (0-365), daylight savings time (flag) (0/1)
time()
returns # of non-leap seconds since 00:00:00 UTC (January 1, 1970) (useful with tmp files)
local(?variable_name)
creates a local variable in a subroutine
@a = reverse @a;
reverse @a in place
@y = sort(@y);
sort @y by 'ascii' value
chop($name);
removes the last character from 'name' returns the removed last character
chomp($name);
removes '\n' only.
chop(@list);
removes last character of last element
@array = split(/:/);
split on the ':' key.
@array = split (/:/,variable,split-column);
@array = split;
splits on whitespace like (/\s+/)
print substr ($_,0,1),"\n";
print first character of array
join($glue,@list);
joins elements of the list with the $glue
ord($string_or_char);
returns ascii value of first char
chr(##);
returns character of the ascii value
hex() oct()
sub subroutine { ; ; ; }
all vars. are global. the last item calculated or referred to in the subroutine is the return value.
&subroutine;
invoke the subroutine
&subroutine($x,$y,$z);
send arguments to the subroutine
in the subroutine, these vars. are given to @_ (local) elements are in the form $_[0], $_[1], etc.
retrieving a hash (send with %hash) use $$_[0]{"key"}.
return; returns.
Switch
if (condition) { } elsif (condition) { } else { } (braces are required)
unless (condition) { } else { }


Text
Re-Format paragraphs
Use Text::Wrap (part of the standard perl distribution):
      use Text::Wrap;
      print wrap("\t", '  ', @paragraphs);
   


Unix
chdir EXPR
default is home directory
chmod
chmod 0755, $filename, 'filename2.txt';
chmod 0600, "filename";
chmod 0755, @executables;
chown $uid, $gid, $filename;
uid & gid must be numerical
system "shell_command"


Variables
false
0, "", undef, 1-1
true
1,"00", "1", "0.000"
$ARGV[0]
first command-line argument
$0
name of perl script being executed
$_
assumed variable, ie: loops, etc.
$.
current line number of latest filehandle read
$$
process id # of the running perl script
$|
zero=buffered output,non-zero=unbuffered output
$!
current system error number (errorno)
(1..5)
equal to (1,2,3,4,5)
$name (scalar)
($a,$b,$c) = (1,2,3);
($a=1,$b=2,$c=3)
($a,$b) = ($b,$a);
(swap $a & $b)
($e,@fred) = @fred;
remove first of @fred -> $e. any excess values are discarded
$a = @fred;
$a = length of @fred
$#fred;
(last element index value)
@words (array)
arrays can be added into arrays
@fred = (1,2,3);
@fred = ("one","two","three");
@huh = 1; ($huh[0] = 1)
@all = (3,@fred,@huh); (= 3,1,2,3,1)
@fred[0,1] ($fred[0],$fred[1])
@fred[0..4] (fred 0, 1, 2, 3 & 4)
"".@a (# of elements)
@a = (each array element is a line)
$number = scalar @fred =# of elements
%words (hash/associative array - cannot be sorted)
%words=("a","z","b","y") (creates new whole hash)
$words{"a"} = "z"; (creates new %words)
%words{"c"} = "x"; (adds onto %words)
keys(%array); (list the keys (a,b,c))
values(%array); (list the values (z,y,x))
while (($first,$last) = each(@lastname))
delete $fred{"aaa"};
$numkeys = scalar keys $words (# of keys)


Examples
The Perl Online Faq
@files = sort (grep (!/^\./, readdir (DIRHANDLE)));
reads in & sorts files not beginning with '.' into @files
$foo =~ tr/[A-Z]/[a-z]/;
tolower() everything
$foo =~ /^[a-z]$/;
true if begins & ends w/same [a-z]


Windows
When sending windows directory commands through the shell that have 'spaces' in the filenames, you need to use the dos equivelant filename file~1. These can only be a maximum of 8 characters. This is handy when dealing with 'start menu' & 'program files'.