MAKING A PERL SCRIPT

	% vi hello		# create a perl script with vi, gedit, wordpad, ..


#!/usr/bin/perl or #!/opt/local/bin/perl on MacOS # print "What is your name? "; $name = <STDIN>; print "Hello, $name!\n"; exit;
% chmod 700 hello # to make the text file "executable" % hello # give the command to run the script % ./hello # may need the ./ depending on your PATH env var.
By the end of perl week, you'll learn how to get rid of an unwanted minor side-effect in this program. (what's that?)

CHARACTERIZATION OF THE LANGUAGE

Perl is like other languages in its use of variables, operators, and
functions to create new values which are stored in variables, printed out or
into files, or tested in conditional statements which control the sequence
of the program.  A perl program can open any number of files for input and
read through them to extract or transform data.

Perl has a useful set of functions that can be used on text input and output
formatting cabability.

Perl scripts can read files (or other input) and print output which just
happens to be HTML.  Data from input can be broken up, rearranged and
interspersed with HTML tags.

Thankfully, there are few interactions between perl and html syntax.
For example, an email address (Monica@LewinskyClark.edu) can appear just that
way in html, but in a perl program, the @ denotes an array variable.
so it may complain about syntax.  (Pragma below).


PERL COMMENTS AND REMARKS

# This is a comment to the end of the line

print "This is a print operation\n"; # this is a remark about the print


NUMBERS  take on Real or Integer attributes implicitly.
VARIABLE in fact, a varible can start up as a string and become a number,
         and vice versa, as we will see.

SCALAR and SPECIAL VARIABLES

	$x	scalar, numeric or string
	$_	the current line of input from last-read file.

ARITHMETIC OPERATORS:  + - * / **    UNARY: ++ --

MODULUS OPERATOR: %  (given the remainder, not the quotient)

LOGICAL OPERATORS: && ||    UNARY: !  (not)

TRUE/FALSE?  numeric zero is false, the null string is false.

COMPARISON OPERATORS:
STRING:  eq  ne  gt  ge  lt  le  cmp       "0" ne "00"    "10" lt "5"

NUMERIC: ==  !=  >   >=  <   <=  <=>        0 == 00        10  >   5
                                 -0+
STRING OPS
		$a.$b			# concatenation
		$a x $n			# repetition  (n copies of $a)
		substr($a,$offset,$length) # extraction
		index($a,$b)	# searching

		$string =~ s/^\s+//; #remove leading spaces
		$string =~ s/\s+$//; #remove trailing spaces

ASSIGNMENT OPERATORS:  =  +=  -=  .=

BACKSLASH ESCAPES IN STRINGS:  \t  \n      \f       \0   \\         \'
                               tab newline formfeed null backslash quote

LITERALS and VARIABLE "INTERPOLATION" IN STRINGS

	$pet = 'dog';
	$sign = "Beware of $pet\n";
	$date =  date ;  represents the output of the unix date command
                         (date should be surrounded by single-back-quotes)

PRINT FUNCTION: print "string with interpolated variables like $x.\n";

EXAMPLE:  % random-test   (fails under perl, works under perl5)


LISTS
	($a, $b, $c) = (1, 2, 3);

ARRAY VARIABLES

$a[$n]	array element, simple subscript reference, subscript is numeric.
$a{$k}	array element, associative array lookup, subscript can be a string

ARRAY vs SCALAR CONTEXT

%a	array, indexed by a string, the whole thing in an array context.
@a	array, indexed by a number, the whole thing, in an array context.
	in a scalar context, gives the number of elements in @a.

	$x = <STDIN>;	one line from STDIN is put in $x
	@a = <STDIN>;	all lines from STDIN are put in @a
	$n = @a;	the number of items in @a is assigned to $n

EXAMPLE:  % radio

PERL STATEMENT STRUCTURES

	if ( exp ) {
		statement;
	}
# ---------------------------------------
	if (exp) {
		statement;
	}
	else {
		statement;
	}
# ---------------------------------------
	for ( initializing stmt; test expression; increment exp) {
		statement;
	}
# ---------------------------------------
	while ( ) {
		statement;
	}
# ---------------------------------------
	next; last; die; exit;


FILE OPERATIONS

	FILEHANDLE defined and used in restricted context.
	open (FILEHANDLE,"< $filename"); # also >, >> and |

	<FILEHANDLE>;

	print FILEHANDLE LIST;
	close FILEHANDLE;
	the end of file cometh how?

EXAMPLE: % radio-file

FUNCTIONS

	split(/PATTERN/,EXPR,LIMIT);
	split(/PATTERN/,EXPR);
	split(/PATTERN/);
	split;			default split is $_
	chop(VARIABLE);		returns what is chopped!
	chop;			default chop is $_;
	chdir	changes cwd of your process.
	system	to get something done.  Watch out for system ("cd path");

EXAMPLE: schedule-maker

ARGUMENTS PASSED TO YOUR SCRIPT

@ARGV	array containing the command line arguments intended for your script.
	$#ARGV is the number of arguments MINUS one, since $ARGV[0] is the
	first argument.

PATTERN MATCHING
	/pat/			true of pat in $_
	$a =~ /pat/		true if pat in $a   (this is a FAKE assignment)
	$a =~ s/p/r/		substitutes r for p
	$a =~ tr/a-z/A-Z/	transliteration

PATTERNS:
	^	^first of line
	.	any character
	\d	digit
	\w	alphanumeric word [a-zA-Z0-9_]
	\s	whitespace character (space, tab, newline, ...)
	\n	newline
	\t	tab
	\b	word boundary
	abc	matches all of a, b, c in order
	fee|fie|foe|fum	matches one of fee, fie, foe or fum
	$	end of the line$

PARSING
	ex: reparens

EXAMPLE: % bm2wm  (can't run unless you have a .netscape/bookmarks.html file)


A GOOD PRACTICE

require 'goodies.pl';

 It's a good idea to define items that may be likely to change external to
 your perl program, such as directory and/or file names, email addresses, ...

 1) each script in a system of scripts can source these definitions
    from one place.
 2) even a single script can benefit by having locally dependent items
    outside of the script. portability.  Also decreases the need to touch
    a potentially powerful script.
 3) if you don't put such definitions in a required file, at least
    put them at the top of your script.


EXERCISES

1) modify radio-file to read "alphabet-only" by using the first letter of
each word as the key.  Hint: substr.  Question: What would happen if the
file contained words with the same first letter?

2) Study /usr/bin/newuser - it loads arrays from LISTS contained
in the script.  See other scripts in /usr/bin  (See perl/README).


REFERENCES

"Programming perl",                             QA 76 .73.P22 W348 1990
"sed & awk", Chapter 3,  Regular Expressions    QA 76 .76.U84 D69 1991

A BOOK has all the examples from "Programming perl", by chapter.

http://www.eecs.nwu.edu/perl/perl.html had a comprehensive perl page at one time.

The perl.faq would be about 64 printed pages!  See '% help faq.perl'

John Miller