Perl Notes: Difference between revisions

From Federal Burro of Information
Jump to navigationJump to search
(New page: =What modules are installed?= So many ways of doing it. ==ExtUtils::Installed== <pre> #!/usr/bin/perl use ExtUtils::Installed; my $instmod = ExtUtils::Installed->new(); foreach my $mod...)
 
No edit summary
Line 219: Line 219:


myscript.cgi vaiable=blah stuff=300 ares=uk
myscript.cgi vaiable=blah stuff=300 ares=uk
[[Category:Computers]]

Revision as of 18:45, 27 January 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 redhat systems ( or system that use rpm). Also won't show modules install via CPAN.

perllocal.pod

perldoc -otext /home/dathornton/lib/perl5/i386-linux-thread-multi/perllocal.pod  | grep Module

There may be more than one perl install on you computer. (locate perllocal.pod )

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', '/var/db/serverlist.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 my.cfg.template | perl -e 'while(<STDIN>){~s/\@\@TEMPLATEVAR1\@\@/$ARGV[0]/g;~s/\@\@TEMPLATEVAR1\@\@/$ARGV[1]/g;print}' ARG0 ARG1

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

myscript.cgi vaiable=blah stuff=300 ares=uk