Perl

Perl 101



Perl is an interpreted language, but exist tool to turn them into binary. Perl script can be setuid!!

Setting up the program




Snipplets in stand alone program

...



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?)


CPAN

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
o conf prerequisites_policy ask
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

Perl Debugger





perl debugger.

invoked by calling perl with -d option:
perl -d 


perl debugger is really just perl running with addional support (from DB module to store state info).  It is not totally seprated from the program as traditional debugger.

To execute any perl code w/in a debugging program, just enter the perl code.  Anything that is not understood by debugger is considered perl code and executed via eval, in the context at the debugging point of the program.

Debugger uses csh-like cmd edit.  ! num, ! cmd, etc to excute cmd, H for history

debugger cmd (commoner stuff):

t				: toggle trace mode (on emulate sh -x, print all code as executed)
R				: reload/restart the debugger

b fn-name		: set breakpoint at fn
b LINE			: set breakpoint at line num (breakable lines has colon after num)
b sub-name		: set breakpoint at a subroutine name
b load FILE		: set breakpint when FILE is first loaded (by use, require?)
d LINE			: remove breakpoint at line
D				: remove all breakpoint
L				: list all breakpoint

c				: continue 
s				: step (will enter into subroutine)
n				: next (execute subroutine completely and return, ie "step over")
			: previous n or s cmd is repeated
r				: return from current running subroutine

T				: backTrace of fn call seq
S pattern		: list all subroutines (with optional pattern for name)
w lineNum		: look at "window" of source code around break point (or around lineNum)
w w				: display more lines than single w by itself.
-				: print previous few lines
l LINE|SUB		: display Lines of code around LINE, SUB, (etc).
/PATTERN		: search fwd in prog for PATTERN (think of vi search)

p $varname		: print value of $varname.
f				: something about switching file (to be read, set breakpoint?)
o				: set lot of option stuff about the dbg


h				: help.  Use h h for concise usage.
h CMD			: help on the given command.
H				: cmd line History

use pipe preceding cmd will run cmd thru the pager.  eg |h to page thru help


Inlined debugger command inside perl programs.  If debugger isn't running, these command will be harmless.

$DB::single = 1		: s = stop program, return command to debugger.
$DB::single = 2		: n
$DB::trace  = 1		: t = trace on ?

TBD






[Doc URL: http://www.cs.fiu.edu/~tho01/psg/perl.html]
(cc) Tin Ho. See main page for copyright info.


"LYS on the outside, LKS in the inside"
"AUHAUH on the outside, LAPPLAPP in the inside"