Perl
http://perldoc.perl.org/perlfaq4.html
http://www.perl.com/pub/a/1999/10/DBI.html DBI
http://www.xav.com/perl/lib/Pod/perltoot.html
http://www.modernperlbooks.com/drafts/modern_perl/chapter_01.html
http://www.modernperlbooks.com/drafts/modern_perl/chapter_10.html
The bless function takes two arguments: a reference to the variable to be marked, and
a string containing the name of the class. It then sets an internal flag on the variable,
indicating that it now belongs to the class. You can check that the blessing succeeded by applying the built-in ref function to variable. Normally when ref is applied to a reference, it returns the type of that
reference. Hence, before $nextbug was blessed, ref($nextbug) would have returned the string 'HASH'.
Once an object is blessed, ref returns the name of its class instead. So after the blessing,
ref($nextbug) will return 'Bug'. Of course the object itself still is a hash, but now it’s a
hash that belongs to the Bug class.
Perl objects: a class is a package. An object is a blessed reference. A method is a function which takes its invocant as its first parameter. Use Moose.
Perl scoping rules: lexical variables are visible only in their static (that is, you understand it by reading the source code) scopes. The outermost scope is file scope. Lexicals declared in inner scopes shadow lexicals declared in outer scopes. Use strict.
#!/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";
}
==== oneliner:
perl -MFile::Find=find -MFile::Spec::Functions -Tlwe \
'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'
===== another oneliner:
perl -le 'print `ls -lR $_` for @INC'
===== another oneliner:
perldoc perllocal
http://www.ibm.com/developerworks/views/xml/libraryview.jsp?search_by=XML+for+Perl+developers
http://perl-xml.sourceforge.net/faq/
LibXML : http://www.perlmonks.org/index.pl?node_id=490846
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'library.xml';
use XML::LibXML;
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($filename);
foreach my $book ($doc->findnodes('/library/book')) {
my($title) = $book->findnodes('./title');
print $title->to_literal, "\n"
}
File library.xml is below
<library>
<book>
<title>Perl Best Practices</title>
<author>Damian Conway</author>
<isbn>0596001738</isbn>
<pages>542</pages>
<image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif"
width="145" height="190" />
</book>
<book>
<title>Perl Cookbook, Second Edition</title>
<author>Tom Christiansen</author>
<author>Nathan Torkington</author>
<isbn>0596003137</isbn>
<pages>964</pages>
<image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif"
width="145" height="190" />
</book>
<book>
<title>Guitar for Dummies</title>
<author>Mark Phillips</author>
<author>John Chappell</author>
<isbn>076455106X</isbn>
<pages>392</pages>
<image src="http://media.wiley.com/product_data/coverImage/6X/07645510/076455106X.jpg"
width="100" height="125" />
</book>
</library>
Output:
Perl Best Practices
Perl Cookbook, Second Edition
Guitar for Dummies
When it is set to a true value, Perl will flush the handle's buffer after each print() or write(). Setting $| affects buffering only for the currently selected default filehandle. You choose this handle with the one argument select() call (see $| in perlvar and select in perlfunc).
Use select() to choose the desired handle, then set its per-filehandle variables.
$old_fh = select(OUTPUT_HANDLE);
$| = 1;
select($old_fh);
#!/usr/bin/perl -Tw # force taint checks, and print warnings
use strict; # install all three strictures
$|++; # force auto flush of output buffer
Perl data type:
1) Scalar $ (number or string or reference.) A scalar value is either defined or undefined. The only undefined value is undef
2) Array @
3) Hash % (associative array)
cmp is for string comparison, it will sort (1, 2, 10) into (1, 10, 2)
@sorted = sort { $a <=> $b } @not_sorted # numerical sort
@sorted = sort { $a cmp $b } @not_sorted # ASCII sort
@sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort
%hash = (
Apples => 1,
apples => 4,
artichokes => 3,
Beets => 9,
);
# sort by keys
foreach my $key (sort keys %hash) {
print "$key = $hash{$key}
";
}
# sort by keys ignore case
foreach my $key (sort {lc($a) cmp lc($b)} keys %hash) {
print "$key = $hash{$key}
";
}
Get a list of hash keys sorted by value.
#-----------------------------------------
@sorted = sort { $hash{$a} cmp $hash{$b} } keys %hash;
Get a reverse sort of a list.
@sorted = sort { $b cmp $a } @list; Which can also be done with @sorted = reverse sort { $a cmp $b } @list;
#!/usr/bin/perl -w
#----------------------------------------------------------------------#
# #
# PURPOSE: Help sort a hash by the hash 'value', not the 'key'. #
# Values are returned in ascending numeric order (lowest #
# to highest). #
#----------------------------------------------------------------------#
sub hashValueAscendingNum {
$grades{$a} <=> $grades{$b};
}
%grades = (
student1 => 90,
student2 => 75,
student3 => 96,
student4 => 55,
student5 => 76,
);
print "\nGRADES IN ASCENDING NUMERIC ORDER:\n";
foreach $key (sort hashValueAscendingNum (keys(%grades))) {
print "\t$grades{$key} \t\t $key\n";
}
( string /Perl/ in this case)
----------------------------------------------------------
my $found;
foreach ( @array ) {
if( /Perl/ ) { $found = $_; last }
}
If you want the array index
-----------------------------
my( $found, $index ) = ( undef, -1 );
for( $i = 0; $i < @array; $i++ ) {
if( $array[$i] =~ /Perl/ ) {
$found = $array[$i];
$index = $i;
last;
}
}