PERL Primer


contents

introduction

This little section attempts to give an overview of the Perl language, enough to get you slobbering for more, but probably not enough detail to turn you into an overnite guru.

<Perl 5 is not tackled here>

Though there may be many net resources for Perl, I would strongly recommend the book:


  Programming Perl

  Larry Wall and Randall L. Schwartz

  O'Reilly & Associates, Inc

  ISBN 0-937175-64-1

variables

Variable names have prefixes that mean different things.

    $name;		a string or a number

    @name;		a list

    %name;		an associative array

All variables appear to global unless you declare them as local to a subroutines

strings

one of the best features of Perl is the ease in which strings can be manipulated. Other string operators are: ge, gt, le, lt

subroutines

All variables are global unless you declare them to be local ( as above). Although there is no need to declare variables in perl, it is good practice declare local variables prior to use. This minimises the possibilities of side_effects.

    sub my_sub

    {

      local ( $var1, @my_array, %my_assoc_array);		#declare locals

    

      #  

      #    Do your processing in here

      #



      return (whatever);

    }

If you pass arguments to a subroutine you can initialise them using the @_ array. This array is constructed from the arguments to the subroutine.

      local ( $var1, $var2, %my_assoc_array)= @_;		#read args

control

Perl supports the same sort of flow control as C:

regular expressions

These are used to build expressions which can be used to: You can use variables to form regular expressions.

regular expressions are extended in Perl:


	a*	0 or more occurances of a

	a+	1 or more occurances of a

	\s	whitespace

	\S	non-whitespace

	\w	alphanumeric

	\W	non alpha-numeric



associative arrays

These allow you to hold pairs of information and are used for quick lookup-type tables.

The key_name can be a quoted string or a variable


   #-----------------------basics----------------------------

   %array_name = ( 			#initialise

   	key1, value1, 

	key2, value2,

	..., 

	keyn, valuen);

   );



   $array_name{key_name} = value;	#associate

   $value = $array_name{key_name}; 	#lookup



   #----------------------- using -------------------------------

   @key_array = keys %array_name;	#get a list of keys

   @key_array = sort (keys %array_name);



   foreach $key ( keys %array_name )	#process each key

   {

     $value =  $array_name{$key};	#get value

   }