Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Thursday, February 21, 2008

CGI with Perl

Basic. Simple example using CGI module:

#!/usr/bin/perl use CGI qw(:standard); print "Content-type: text/html\n\n"; print "<html>\n"; $name = param("name"); print "<body>$name</body>\n"; print "</html>";

Wednesday, September 19, 2007

Using CPAN Modules

FAQ: Here are some of my frequently used commands for CPAN

# Interactive mode % sudo perl -MCPAN -e shell # Installing a module % sudo perl -MCPAN -e "install Some::Module" # List of versions of installed and available modules % sudo perl -MCPAN -e 'CPAN::Shell->r' # Upgrade all modules % sudo perl -MCPAN -e \ 'CPAN::Shell->install(CPAN::Shell->r)'

Configuration: If you are getting this error

CPAN.pm needs either the external programs tar, gzip and bzip2 installed. Can’t continue.
Try this:
shell> sudo perl -MCPAN -eshell cpan> o conf tar /bin/tar cpan> o conf bzip2 /bin/bzip2 cpan> o conf gzip /bin/gzip cpan> o conf commit

Thursday, September 13, 2007

Parsing XML using XML::Simple

Installing XML::Simple:

shell> perl -MCPAN -e shell cpan> install XML::Simple
Reading XML: You can use XMLin and XMLout to read and write XML file or string.
my $ref = XMLin('/dir/to/file.xml'); # it takes file my $ref2 = XMLin('<xml> .... '); # or string
Example of reading a podcast feed:
#!/usr/bin/perl -w use strict; use warnings; use LWP::Simple; use XML::Simple; use Data::Dumper; my $url = 'http://url/feed.xml'; my $content = get $url; die "Couldn\'t get $url" unless $content; my $ref = XMLin($content); my @items = @{$ref->{'channel'}->{'item'}}; # grabbing link for my $i ( 0 .. $#items ) { print ${$items[$i]}{'link'} . "\n"; }

Wednesday, August 22, 2007

Perl LWP

Installing: LWP

username@ubuntu:~$ sudo perl \ -MCPAN -e 'install Bundle::LWP'
Once you install Bundle::LWP, you can test it out the code by
#!/usr/bin/perl -w use strict; use warnings; use LWP::Simple; my $url = 'http://www.google.com/'; my $content = get $url; die "Couldn't get $url" unless defined $content; print $content;
Update: Probably you might want to do this first
sudo perl -MCPAN -e shell cpan> install Bundle::CPAN cpan> reload cpan

Wednesday, April 18, 2007

Perl data structures

Arrays of Arrays.

#!/usr/bin/perl -w use strict; my @AoA = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ); # accessing arrays of arrays using index for my $i ( 0 .. $#AoA ) { for my $j ( 0 .. $#{$AoA[$i]} ) { print $AoA[$i][$j] . "\t"; } print "\n"; } # accessing arrays of arrays for my $row ( @AoA ) { for my $col ( @{$row} ) { print $col . "\t"; } print "\n"; }
Arrays of Hashes.
#!/usr/bin/perl -w use strict; my @AoH; push @AoH, { a => 1, b => 2 }; push @AoH, { c => 3, d => 4 }; for my $i ( 0 .. $#AoH ) { for my $k ( keys %{$AoH[$i]} ) { print $k . ":" . ${$AoH[$i]}{$k} . " "; } print "\n"; }
Hashes of Hashes.
#!/usr/bin/perl -w use strict; my %HoH; $HoH{"keyA"} = { a => 1, b => 2 }; $HoH{"keyB"} = { c => 3, d => 4 }; for my $kA ( keys %HoH ) { print $kA . ":\n"; for my $kB ( keys %{$HoH{$kA}} ) { print $kB . ":" . $HoH{$kA}{$kB} . " "; } print "\n"; }
Update:

Links