Perl Variables

Objects and Class

It is essentially a hack on Packages, but bless()ed into a class.
Default Package variables are class-wide variables.
Instance/method variables need to be inside data structure that has been
bless()ed. But at time may need to check whether method was called
as class subroutine or as object's method!!
First param for a method call, $_[0], is a ref to the object (or its class), it is done automatically by Perl (using unshift or something).
Be sure to "use strict", or Perl may think of some closure need to applied
to specific variables declared in methods and those may become like
class-wide data, but in fact "use strict" would mean they should be scoped correctly and destroyed when method is completed.
constructor can be named anything, new() is just the most popular name.
Destructor is called automagically and must be named DESTROY.
ref: perltoot, perltooc, perlref

specifying libraries

perl -I/path/to/my/lib

export PERL5LIB=$PERL5LIB:/path/to/my/lib

export PERL5LIB=$PERL5LIB:/usr/prog/perl/current/lib/5.10.1:/usr/prog/perl/current/lib/site_perl/5.10.1/ # for CPAN installed lib, add path to the point where all files/dirs are added by CPAN.

inside perl program: use lib "/path/to/my/lib"

Note that :: is used as directory separator from the "library root dir". eg
use Bio::Perl::foo;
should find something in $PERL5LIB/Bio/Perl/foo.pm

POD/Perldoc

Plain Old Doc can be embeded inside perl code itself, and perldoc perl.html (this file)
will produce man page like documentation.
pod commands start with = , and most often, need to have blank line before
and after it!
eg:

=head1 Description
Plain text can now go in here, and it will be wrapped.
New line isn't really headed, and formatter will
join them and wrap at end of screen line.
=cut
=pod
= pod doesn't do much, other than it is text to be placed in pod doc.
when space/tab is prefixed in
the begining of the line
then perldoc will treat it like quoted text
and format them verbatim
lastly, for html OL item list stuff, need to have a block delimited as
= over
= back
and add entries with
= item itemName
=cut
=over
=item ItemName
what ever describe an item, these will be indented
=item *
instead of name, * should produce bullet, but seems to be formated as ? :(
in real life, don't mix name with bullet was a best practice...
=back
=cut
# the =cut is needed to indicate the end of pod in the =over/=back block

quick perl ref
for( my $i; $i < 10; $i++ ) { ... }
foreach my $entry ( @list ) { ... }
# specify where perl will search for the libraries, @INC define the search path.
BEGIN {
push(@INC, '/zambeel/lib/perl/site_perl');
push(@INC, '/opt/zambeel/lib');
}
open( FH-IN, "< /path/to/inputfile" );
open( FH-OUT, "> /path/to/outputfile" );
while( ) { print (FH-OUT $_); }
close( FH-IN );
Note that it seems that while the file handle is open, a self-recursive call
will mangle up the FH internal db, thereby closing it when inner code returns.
One solution is read file into memory bugger and close FH before making recursive call.
calling main will all input args:
main($#ARGV+1, @ARGV);
see email ref folder for more perl stuff
---
# Perl module creation
# ch 11.2 of Programming Perl
package Bestiary; # store this in file Bestiary.pm
require Exporter; # copy these two lines verbatim
our @ISA = ("Exporter"); # work as inheritance from exporter class.
# then, have these:
our @EXPORT = qw($camel %wolf ram); # Export by default
our @EXPORT_OK = qw(leopard @llama $emu); # Export by request
our %EXPORT_TAGS = ( # Export as group
camelids => [qw($camel @llama)],
critters => [qw(ram $camel %wolf)],
);
# so, in a program that "use Bestiary",
# by default, it can access ram instead of having to do Restiary::ram
# The export as group need to be imported as "use Bestiary qw( :camelids )"
# colon is needed for tags, not for the export by request list.
# for variables declared in the module w/o my, they can be accessed as
# $Bestiary::emu (funny symbol before package name).
# running cmd in shell.
$| = 1; # avoid buffering
$exitCode = system( 'ls -l' );
$exitCode /= 256;
# Manish said that system use lot of buffering, and can something get confused and stuck if the are closed output... use $! = 1 to get around the problem.
###### styles ########
for sizable script, use a file to define all the commands, scripts, etc that will be called, eg defineCmd.pm.
This way, the defineCmd.pm can be smart enough to detect platform changes of where commands are, or the need to move script to different dir.
if( os == rh )
ping = ...
ifconfig = ...
elsif( os = sunos )
pint = ...
ifconfig = ...
if sys admin, think about setting a dir where all executable are located, independent of platform, but this will result to non-portable code.
############### net stuff #################
C:\refMaterial.YaEnCasa\ref.from.cd\oreileyPerlCDbookShelf\perlnut\c13_001.htm
inet_ntoa
inet_aton
convert from/to dotted ip (and english name) to 32bit number for low level manipulation of the strings.
inet_aton() will convert single integer number (eg 19) and treat that as last octet and create IP (32 bit bin) equivalent to 0.0.0.19, so that two 32bit number can be ORed together (via |)
Numberic addition will not work :(
---
Big endian = MSB = most significant bit first = network order =
ie, normal human looking pattern order for 32bit num, right most bit = 1, left most bit is most significant, highest value.
C:\refMaterial.YaEnCasa\ref.from.cd\oreileyPerlCDbookShelf\cookbook\ch02_05.htm
perl
N format = network order, (those produced by inet_aton).
B32 = bit by bit (the "normal" perl scalar data?)

CPANMINUS

a newer approach to cpan lib install. User will install this module in their own dir.
When installed to say NFS mounted home dir, setting perl5lib to include such dir, then the cpan mod will be avail to the user on any machine with NFS access to the dir

more details...

Download CPAN modules and install automatically as root to local system:
Note that which perl is invoked is important.
sun stock perl used cc for compile, prefer to use sunfreeware.com that install to /usr/local, and use gcc.
Also, remember that module names are case sensitive.
If there are more than one compiler,
def env var may help:
CC=/usr/local/bin/gcc (does it really use a CC compiler??)
MAKE= ... (is it $MAKE?)
Best to config CPAN as root and define the setting of where all the programs are located.
perl -MCPAN -e shell # abreviated as cpan in perl5
o conf prerequisites_policy # learn about the config
o conf prerequisites_policy follow # set to automatically get dependencies (def?)
o conf prerequisites_policy ask # change to prompt before adding prereq
install Mail::SpamAssasin (spell?)
install Digest::Nilsima
install URI::Escape
quit
or in non-interactive mode:
perl -MCPAN -e 'install Chocolate::Belgian'
perl -MCPAN -e 'install Net::DNS'
Files are stored in a variety of places :(
@INC is probably set differently, and multiple version can exist :(
/usr/local/lib/perl5/site_perl/5.8.0...
find /usr/local/lib/perl5/ -name '*SHA1*'
/usr/local/lib/perl5/site_perl/5.8.0/sun4-solaris/auto/Digest/SHA1
/usr/local/lib/perl5/site_perl/5.8.0/sun4-solaris/auto/Digest/SHA1/SHA1.so
/usr/local/lib/perl5/site_perl/5.8.0/sun4-solaris/auto/Digest/SHA1/SHA1.bs
/usr/local/lib/perl5/site_perl/5.8.0/sun4-solaris/Digest/SHA1.pm
/usr/local/lib/perl5/site_perl/5.8.0/Digest/HMAC_SHA1.pm
/usr/local/lib/perl5/site_perl/5.8.0/Digest/SHA1.pm
/usr/local/lib/perl5/site_perl/5.8.0/Mail/SpamAssassin/SHA1.pm
One can set PERL5LIB to desired root location, but this may still happen I think.
--
Clearing CPAN config setup (eg, to change proxy settings, compiler locations, etc)
Need to remove (or edit) Config.pm, at:
SMCperl that install to /usr/local: /usr/local/lib/perl5/5.8.0/CPAN/Config.pm

Troubleshooting CPAN install

Uninstalling/Reinstalling a module

There is no easy way to uninstall a module. CPAN is not a package manager :(
But most package have a .package list. It list all the files and the path they were installed.
Can use this to remove the files.
After this step, cpan can be run and installing the module should start from scratch again.
(cpan should not complain that the module is already installed, given it find the files are not there. partial file delete should work, but if trying to make backup, be careful to copy/move all files).
eg. cpan install of DBD::Pg left package list at
/usr/prog/perl/5.16.3_gcc/lib/site_perl/5.16.3/x86_64-linux/auto/DBD/Pg/.packlist

Manual install of modules

If a CPAN module won't install, go to the shell and
cd to the dir where the build location is, and try to run "make" manually, eg
cd /home/tin/.cpan/build/Inline-Java-0.53-Fyk_8W
perl Makefile.PL