Perl Notes

Essential Perl Bookshelf

cover
Programming Perl (3rd Edition)
cover
Perl Cookbook
cover
Object Oriented Perl
cover
Effective Perl Programming: Writing...
To intall new modules on Unix.....
perl -MCPAN -e shell
install Bundle::CPAN

To install new modules on Windows/ActiveState Perl run 'ppm' (Perl Package Manager) from the command prompt.

Perl has great documentation bundled with it. 
To access the documentation, use the program 'perldoc'

Example,
	Help on a module:                perldoc CGI
	Help on a built-in function:     perldoc -f split
	Display the Perl FAQ             perldoc perlfaq
	Display the Perl FAQ Page 4      perldoc perlfaq4
	Search the Perl FAQ              perldoc -q input



From perldoc -q 

       How can I pull out lines between two patterns that are
       themselves on different lines?

       You can use Perl's somewhat exotic `..' operator
       (documented in the perlop manpage):

           perl -ne 'print if /START/ .. /END/' file1 file2 ...

       If you wanted text and not lines, you would use

           perl -0777 -ne 'print "$1\n" while /START(.*?)END/gs' file1 file2 ...

       But if you want nested occurrences of `START' through
       `END', you'll run up against the problem described in the
       question in this section on matching balanced text.

       Here's another example of using `..':

           while (><) {
               $in_header =   1  .. /^$/;
               $in_body   = /^$/ .. eof();
               # now choose between them
           } continue {
               reset if eof();         # fix $.
           }



A regular (scalar) variable $a = 5;
$xyz="1234";
An (regular) array @a = ( 123, "abc", 456 );
The second element of the array ( a scalar, indexes start at 0 ) $a[1];
Accessing all elements of an array foreach $item ( @a ) {
   print "$item\n"
}
A hash array.
Why do we have to index items by a number? Can't we do it with a string instead? Thats what a hash is for!
%myHash = (
   bananas => "yellow",
   apples => "red",
);
Accessing an item in the hash print "The color of bananas is: ",$myHash{'bananas'},"\n";
Accessing all elements of an hash foreach $fruit ( keys %myHash ) {
   print "$fruit is the color $myHash{'$fruit'}\n"
}
REFERENCES ( a.k.a. Pointers )
Note:All references are scalar variables.
A reference to a scalar variable. $aRef = \$a;
Printing a reference to a scalar.
The {} are optional.
print "${$a}\n";
A reference to an array $aRef = \@a;
De-Referencing the entire array. @aCopy = @{$aRef};
Printing an element from a reference to an array. print "$aRef->[1]\n";
Creating a reference to an array.
Note the change from our previous example. @a is now $a and the ('s are changed to ['s
$a = [ 123, "abc", 456 ];
Accessing all elements from an referece to an array foreach $item ( @{$a} ) {
   print "$item\n"
}
A reference to a hash $myRef = \%myHash;
Accessing all elements of an hash via a reference. foreach $fruit ( keys %{$myRef} ) {
   print "$fruit is the color $myRef->{'$fruit'}\n"
}
A hash array reference.
Note the change from our previous example. %myHash is now $myRef and the ('s are changed to {'s
$myRef = {
   bananas => "yellow",
   apples => "red",
};
How to use quoted text in Parse::RecDescent From: Dammian Conway comp.lang.perl.misc
	$grammar = <<'EOG';

	{ our $quoted = Text::Balanced::gen_delimited_pat(q{"'}); }

	report:
		assignment(s)

	assignment: 
		tagged | complex | simple | 

	simple:
		keyword value

	complex:
		keyword '{' assignment(s) '}'

	tagged:
		keyword tagname '{' assignment(s) '}'

	keyword:
		/[^\s"'`]+/	# or maybe an explicit list of alternatives

	tagname:
		/[^\s"'`]+/	# or whatever your tagname rules are

	value:
		/$quoted|[^\s{}]+/ 

	EOG