Perl Notes: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=What modules are installed?= | ==What modules are installed?== | ||
So many ways of doing it. | So many ways of doing it. | ||
==ExtUtils::Installed== | ===ExtUtils::Installed=== | ||
<pre> | <pre> | ||
Line 16: | Line 16: | ||
</pre> | </pre> | ||
==rpm== | ===rpm=== | ||
rpm -qa | grep ^perl | rpm -qa | grep ^perl | ||
obviously only good for | obviously only good for rpm based systems ( or system that use rpm). Also won't show modules install via CPAN. | ||
==perllocal.pod== | ===perllocal.pod=== | ||
perldoc -otext /home/ | perldoc -otext /home/david/lib/perl5/i386-linux-thread-multi/perllocal.pod | grep Module | ||
There may be more than one perl install on you computer. (locate perllocal.pod ) | There may be more than one perl install on you computer. (locate perllocal.pod ) | ||
=Data::Dumper= | ===pmall=== | ||
pmall from the pmtools package can do it for you. | |||
http://search.cpan.org/~mlfisher/pmtools/pmall | |||
''not installed on ML production boxes.'' | |||
==Data::Dumper== | |||
One of the hackiest best debuging devy type modules .. | One of the hackiest best debuging devy type modules .. | ||
dump a varaible no matter what type of variable it is. | dump a varaible no matter what type of variable it is. | ||
Line 34: | Line 42: | ||
perldoc Data::Dumper | perldoc Data::Dumper | ||
=CDB_File= | ==CDB_File== | ||
We use cdbs all over the frikken place. But we are so smrt, we don't install the cdbdump command line tools or the perl module CDB_File all over the place. D'uh! | We use cdbs all over the frikken place. But we are so smrt, we don't install the cdbdump command line tools or the perl module CDB_File all over the place. D'uh! | ||
want to get to a cdb via perl? | want to get to a cdb via perl? | ||
my $catref = tie %h, 'CDB_File', '/ | my $catref = tie %h, 'CDB_File', '/tmp/mydata.cdb' or die "tie failed: $!\n"; | ||
foreach my $server ( @{$catref->multi_get($ARGV[0])} ) { | foreach my $server ( @{$catref->multi_get($ARGV[0])} ) { | ||
if ( $server =~ /[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}/ ) { | if ( $server =~ /[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}/ ) { | ||
Line 51: | Line 58: | ||
As an aside, we also don't use cdbs the way they were designed in most cases. We have big hashes with keys that have the data we are looking for, rather than values that have the data we are looking for. Often we have duplicate keys, hence the crazy multi_get usage above. | As an aside, we also don't use cdbs the way they were designed in most cases. We have big hashes with keys that have the data we are looking for, rather than values that have the data we are looking for. Often we have duplicate keys, hence the crazy multi_get usage above. | ||
=non-root module install= | ==non-root module install== | ||
cd ~/src/Statistics-Descriptive-2.6 | cd ~/src/Statistics-Descriptive-2.6 | ||
Line 71: | Line 78: | ||
/usr/lib/perl5/site_perl/5.005 | /usr/lib/perl5/site_perl/5.005 | ||
=Where does perl get it's @INC from ?= | ==Where does perl get it's @INC from ?== | ||
I think that @INC is compiled into libperl.so | I think that @INC is compiled into libperl.so | ||
Line 115: | Line 122: | ||
</pre> | </pre> | ||
=Which module is perl using?= | ==Which module is perl using?== | ||
% perl -e 'use strict; print map {"$_ => $INC{$_}\n"} keys %INC' | % perl -e 'use strict; print map {"$_ => $INC{$_}\n"} keys %INC' | ||
strict.pm => /usr/lib/perl5/5.00503/strict.pm | strict.pm => /usr/lib/perl5/5.00503/strict.pm | ||
=Casting= | ==Casting== | ||
Don't be fooled into thinking that types don't matter in perl. Check this out: | Don't be fooled into thinking that types don't matter in perl. Check this out: | ||
Line 161: | Line 168: | ||
* 5 five | * 5 five | ||
in my list of option. | in my list of option. What happened to 2 4 6? Turns out the {} surrounding my keys "cast" the array from keys to a HASH!!! Ooops. | ||
Once more with gusto: | Once more with gusto: | ||
Line 190: | Line 197: | ||
Score! | Score! | ||
=Theredoc= | ==Theredoc== | ||
from http://my.safaribooksonline.com/0596001738/perlbp-CHP-4-SECT-10?portal=oreilly | from http://my.safaribooksonline.com/0596001738/perlbp-CHP-4-SECT-10?portal=oreilly | ||
Line 209: | Line 216: | ||
} | } | ||
=Templating= | ==Templating== | ||
al a sed | al a sed | ||
cat | cat header.cfg.template | perl -e 'while(<STDIN>){~s/\@\@funcationalunit\@\@/$ARGV[0]/g;~s/\@\@class\@\@/$ARGV[1]/g;print}' 3 job | ||
note that the -p and -n from http://search.cpan.org/dist/perl/pod/perlrun.pod don't help as <> would be the file name passed as an option rathr than STDIN. | note that the -p and -n from http://search.cpan.org/dist/perl/pod/perlrun.pod don't help as <> would be the file name passed as an option rathr than STDIN. | ||
=cgi options via command line= | or in less typing: | ||
$template =~ s/\@\@port\@\@/$port/g; | |||
==Find replace in many files== | |||
perl -pi -e 's/about news\.$/about selling advertising space\./' file* | |||
from: http://www.debian-administration.org/articles/298 | |||
==cgi options via command line== | |||
var/www/cgi-bin/mycgi.cgi jsoncallback=blah interval=300 s=uk | |||
==ParserDetails.ini== | |||
during some putzting around I got this meesage: | |||
could not find ParserDetails.ini in /usr/lib/perl5/site_perl/5.8.0/XML/SAX | |||
I fixed it like this: | |||
touch /usr/lib/perl5/site_perl/5.8.0/XML/SAX/ParserDetails.ini | |||
perl -MXML::SAX -e "XML::SAX->add_parser(q(XML::SAX::PurePerl))->save_parsers()" | |||
as per : http://perl-xml.sourceforge.net/faq/ | |||
this gives: | |||
[XML::SAX::PurePerl] | |||
http://xml.org/sax/features/namespaces = 1 | |||
I note that on rpmbuilder the file looks like this: | |||
[XML::SAX::PurePerl] | |||
http://xml.org/sax/features/namespaces = 1 | |||
[XML::SAX::Expat] | |||
http://xml.org/sax/features/namespaces = 1 | |||
http://xml.org/sax/features/external-general-entities = 1 | |||
http://xml.org/sax/features/external-parameter-entities = 1 | |||
It looks like this file is generated on install of XML perl libs. rpm -qf shows "not owned by any package." | |||
More investigation needed? | |||
==URPM== | |||
ripped from usr/lib/perl5/site_perl/5.8.0/ML/Package/Modules/Special.pm | |||
use URPM; | |||
BEGIN { | |||
$db || ($db = URPM::DB::open()); | |||
} | |||
$db->traverse( | |||
sub { | |||
my ($rpm) = @_; | |||
print $rpm->name() | |||
print $rpm->version() | |||
# .. and so on. | |||
} | |||
== IO::Socket::INET and timeout == | |||
how to deal with timeouts: | |||
<pre> | |||
eval { | |||
local $SIG{ALRM} = sub { die 'Timed Out'; }; | |||
alarm 3; | |||
my $sock = IO::Socket::INET->new( | |||
PeerAddr => inet_ntoa( gethostbyname($host) ), | |||
PeerPort => 'whois', | |||
Proto => 'tcp', | |||
## timeout => , | |||
); | |||
$sock->autoflush; | |||
print $sock "$qry\015\012"; | |||
undef $/; $data = <$sock>; $/ = "\n"; | |||
alarm 0; | |||
}; | |||
alarm 0; # race condition protection | |||
return "Error: timeout." if ( $@ && $@ =~ /Timed Out/ ); | |||
return "Error: Eval corrupted: $@" if $@; | |||
</pre> | |||
taken from : http://www.webmasterworld.com/forum13/3140.htm | |||
== Last $length elements in array == | |||
print @file[-$length .. -1]; | |||
==Why perls is fun== | |||
WHY ???! | |||
* [http://www.perlmonks.org/?node_id=130021 Orcish Maneuver] | |||
==Tac in perl== | |||
useful for sorting things at that organized "backwards" , like mrtg targets. | |||
<pre> | |||
sub tac ($) { | |||
my @arr = split "" , shift; | |||
my $str =''; | |||
while ( my $c = pop @arr ) { | |||
$str .= $c; | |||
} | |||
return $str; | |||
} | |||
</pre> | |||
Do not try this at home , nor with string larger than say , 4 characters. | |||
==POD Documention snippets== | |||
<pre> | |||
=head2 sub myfunc(fish integer) | |||
=over 2 | |||
Does some stuff, and stuff | |||
=back | |||
=cut | |||
sub myfunc($) { | |||
some code | |||
} | |||
</pre> | |||
==using map to loop over array== | |||
map {loggrab($unit, $server, $_)} @$rLogs; | |||
for each @$rLogs | |||
loggrab($unit, $server, $_); | |||
WTF, how does this work? | |||
==Profiling== | |||
===Call profiling=== | |||
collect some stats with Devel::DProf: | |||
example: | |||
cd /tmp | |||
perl -T -d:DProf /usr/local/bin/network-test.pl | |||
dropes a file in . ( /tmp) called tmon.out which has profiling data. | |||
examine the file with dprofpp | |||
dprofpp -u | |||
note: no memory data. | |||
===Memory Profiling=== | |||
use perl -D (debug) option. | |||
example: | |||
perl -T -Dm /usr/local/bin/network-test.pl | |||
allocation and freeing data is sent to STDERR. | |||
Todo: write a script to parse this output to report on number of allocations , max allocation, etc. | |||
== CVS keywords of note == | |||
(my $version) = '$Revision: 1.16 $' =~ /([\d\.]+)/; | |||
[[Category: | [[Category:Perl]] |
Revision as of 15:13, 5 October 2009
What modules are installed?
So many ways of doing it.
ExtUtils::Installed
#!/usr/bin/perl use ExtUtils::Installed; my $instmod = ExtUtils::Installed->new(); foreach my $module ($instmod->modules()) { my $version = $instmod->version($module) || "???"; print "$module -- $version\n"; }
rpm
rpm -qa | grep ^perl
obviously only good for rpm based systems ( or system that use rpm). Also won't show modules install via CPAN.
perllocal.pod
perldoc -otext /home/david/lib/perl5/i386-linux-thread-multi/perllocal.pod | grep Module
There may be more than one perl install on you computer. (locate perllocal.pod )
pmall
pmall from the pmtools package can do it for you.
http://search.cpan.org/~mlfisher/pmtools/pmall
not installed on ML production boxes.
Data::Dumper
One of the hackiest best debuging devy type modules .. dump a varaible no matter what type of variable it is.
perldoc Data::Dumper
CDB_File
We use cdbs all over the frikken place. But we are so smrt, we don't install the cdbdump command line tools or the perl module CDB_File all over the place. D'uh!
want to get to a cdb via perl?
my $catref = tie %h, 'CDB_File', '/tmp/mydata.cdb' or die "tie failed: $!\n"; foreach my $server ( @{$catref->multi_get($ARGV[0])} ) { if ( $server =~ /[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}/ ) { print "server: $server is an ip\n"; } esle { print "server: $server is name\n"; } }
As an aside, we also don't use cdbs the way they were designed in most cases. We have big hashes with keys that have the data we are looking for, rather than values that have the data we are looking for. Often we have duplicate keys, hence the crazy multi_get usage above.
non-root module install
cd ~/src/Statistics-Descriptive-2.6 perl Makefile.PL PREFIX=~ make make test make install
How do I tell perl to use my own module library?
use lib "/tmp";
What is in my @INC?
% perl -e 'print join "\n", @INC' /usr/lib/perl5/5.00503/i386-linux /usr/lib/perl5/5.00503 /usr/lib/perl5/site_perl/5.005/i386-linux /usr/lib/perl5/site_perl/5.005
Where does perl get it's @INC from ?
I think that @INC is compiled into libperl.so
[user@server]$ strings /usr/bin/perl | grep lib /lib/ld-linux.so.2 libperl.so libnsl.so.1 libdl.so.2 libm.so.6 libpthread.so.0 libc.so.6 __libc_start_main libcrypt.so.1 libutil.so.1 /usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE Usage: DynaLoader::dl_unload_file(libref) Usage: DynaLoader::dl_find_symbol(libhandle, symbolname) [user@server]$ file /usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE /usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE: directory [user@server]$ locate libperl.so /usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE/libperl.so [user@server]$ strings /usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE/libperl.so | grep lib libnsl.so.1 libdl.so.2 libm.so.6 libpthread.so.0 libc.so.6 libcrypt.so.1 libutil.so.1 /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl print "\nCharacteristics of this binary (from libperl): \n", /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /lib/ [user@server]$
Which module is perl using?
% perl -e 'use strict; print map {"$_ => $INC{$_}\n"} keys %INC' strict.pm => /usr/lib/perl5/5.00503/strict.pm
Casting
Don't be fooled into thinking that types don't matter in perl. Check this out:
I have a tables with id and name as coloumns. draggged it out of sql to a hash %priorities, where $priorities{$id} = $name;
We use CGI to make building html and forms easier. popup_menu needs a reference to an array of values and a reference to a hash of names (why not just take a hash, geesh). So in the code we see typically:
$q = new CGI %myhash = make_hash(); @myarray = make_array(%myhash); print "Form Fields",$q->popup_menu(-name=>"myfield", -values=>\@myarray, -labels=>\%myhash, );
seems like one too many data structures to me... lets try and extract keys from hash as array inline:
print "Priority ",$q->popup_menu(-name=>"priority", -values=>keys(%priorities), -labels=>\%priorities);
Doesn't work.. need a ref to an array, try again:
print "Priority ",$q->popup_menu(-name=>"priority", -values=>{keys(%priorities)}, -labels=>\%priorities);
This is sort of what I need, I get
- 1 one
- 3 three
- 5 five
in my list of option. What happened to 2 4 6? Turns out the {} surrounding my keys "cast" the array from keys to a HASH!!! Ooops.
Once more with gusto:
print "Priority ",$q->popup_menu(-name=>"priority", -values=>[keys(%priorities)], -labels=>\%priorities);
and I get back my "full" list of options:
- 6 six
- 4 four
- 1 one
- 2 two
- 3 three
- 5 five
however I'm not satified; the array is not sorted... so some more "inline" work:
print "Priority ",$q->popup_menu(-name=>"priority", -values=>[sort { $a <=> $b } keys %priorities], -labels=>\%priorities);
Score!
Theredoc
from http://my.safaribooksonline.com/0596001738/perlbp-CHP-4-SECT-10?portal=oreilly
use Readonly; Readonly my $USAGE => <<'END_USAGE'; Usage: qdump file [-full] [-o] [-beans] Options: -full : produce a full dump -o : dump in octal -beans : source is Java END_USAGE
and later...
if ($usage_error) { warn $USAGE; }
Templating
al a sed
cat header.cfg.template | perl -e 'while(<STDIN>){~s/\@\@funcationalunit\@\@/$ARGV[0]/g;~s/\@\@class\@\@/$ARGV[1]/g;print}' 3 job
note that the -p and -n from http://search.cpan.org/dist/perl/pod/perlrun.pod don't help as <> would be the file name passed as an option rathr than STDIN.
or in less typing:
$template =~ s/\@\@port\@\@/$port/g;
Find replace in many files
perl -pi -e 's/about news\.$/about selling advertising space\./' file*
from: http://www.debian-administration.org/articles/298
cgi options via command line
var/www/cgi-bin/mycgi.cgi jsoncallback=blah interval=300 s=uk
ParserDetails.ini
during some putzting around I got this meesage:
could not find ParserDetails.ini in /usr/lib/perl5/site_perl/5.8.0/XML/SAX
I fixed it like this:
touch /usr/lib/perl5/site_perl/5.8.0/XML/SAX/ParserDetails.ini perl -MXML::SAX -e "XML::SAX->add_parser(q(XML::SAX::PurePerl))->save_parsers()"
as per : http://perl-xml.sourceforge.net/faq/
this gives:
[XML::SAX::PurePerl] http://xml.org/sax/features/namespaces = 1
I note that on rpmbuilder the file looks like this:
[XML::SAX::PurePerl] http://xml.org/sax/features/namespaces = 1
[XML::SAX::Expat] http://xml.org/sax/features/namespaces = 1 http://xml.org/sax/features/external-general-entities = 1 http://xml.org/sax/features/external-parameter-entities = 1
It looks like this file is generated on install of XML perl libs. rpm -qf shows "not owned by any package."
More investigation needed?
URPM
ripped from usr/lib/perl5/site_perl/5.8.0/ML/Package/Modules/Special.pm
use URPM; BEGIN { $db || ($db = URPM::DB::open()); } $db->traverse( sub { my ($rpm) = @_; print $rpm->name() print $rpm->version() # .. and so on. }
IO::Socket::INET and timeout
how to deal with timeouts:
eval { local $SIG{ALRM} = sub { die 'Timed Out'; }; alarm 3; my $sock = IO::Socket::INET->new( PeerAddr => inet_ntoa( gethostbyname($host) ), PeerPort => 'whois', Proto => 'tcp', ## timeout => , ); $sock->autoflush; print $sock "$qry\015\012"; undef $/; $data = <$sock>; $/ = "\n"; alarm 0; }; alarm 0; # race condition protection return "Error: timeout." if ( $@ && $@ =~ /Timed Out/ ); return "Error: Eval corrupted: $@" if $@;
taken from : http://www.webmasterworld.com/forum13/3140.htm
Last $length elements in array
print @file[-$length .. -1];
Why perls is fun
WHY ???!
Tac in perl
useful for sorting things at that organized "backwards" , like mrtg targets.
sub tac ($) { my @arr = split "" , shift; my $str =''; while ( my $c = pop @arr ) { $str .= $c; } return $str; }
Do not try this at home , nor with string larger than say , 4 characters.
POD Documention snippets
=head2 sub myfunc(fish integer) =over 2 Does some stuff, and stuff =back =cut sub myfunc($) { some code }
using map to loop over array
map {loggrab($unit, $server, $_)} @$rLogs;
for each @$rLogs loggrab($unit, $server, $_);
WTF, how does this work?
Profiling
Call profiling
collect some stats with Devel::DProf:
example:
cd /tmp perl -T -d:DProf /usr/local/bin/network-test.pl
dropes a file in . ( /tmp) called tmon.out which has profiling data.
examine the file with dprofpp
dprofpp -u
note: no memory data.
Memory Profiling
use perl -D (debug) option.
example:
perl -T -Dm /usr/local/bin/network-test.pl
allocation and freeing data is sent to STDERR.
Todo: write a script to parse this output to report on number of allocations , max allocation, etc.
CVS keywords of note
(my $version) = '$Revision: 1.16 $' =~ /([\d\.]+)/;