Perl
- How to find if a perl module is installed?
perl -MMODULENAME -e1
Eg: perl -MHTML::Embperl -e1 If there is no msg then the module is present - How to check the version number of an installed module?
Assumes the module uses $VERSION, but then, most CPAN modules do)
perl -MMODULENAME -e'print "$MODULENAME::VERSION\n";'
For example:
perl -MHTML::Embperl -e'print "$HTML::Embperl::VERSION\n";' - How to determine where a module is installed?
Lists every dir used by the module, including man pages, etc. Should be entered on one line.
perl -MExtUtils::Installed -e'$,="\n";print ExtUtils::Installed->new()->directories("MODULENAME")," "'
For example:
perl -MExtUtils::Installed
-e'$,="\n";print ExtUtils::Installed->new()->directories("HTML::Embperl")," "'
This technique relies on ExtUtils::Installed, which is part of the standard Perl distro these days. - Installing things from CPAN?
$ perl -MCPAN -e shell // Shell appears.
install DBD::mysql - How to including newly installed Perl modules?
perl -I... - How to create an array of file handles?
my %fh_hash=();
if(!exists $fh_hash{$f_name}) {
local *FILE;
open(FILE, ">$f_name") || die;
$fh_hash{$f_name}=*FILE;
}
Later to print to the file:
print {$fh_hash{$f_name}} $_; - How to sort one array using the sort order of another array?
my @sk = @k[sort { $v[$a] <=> $v[$b] } 0 .. $#v];
Sort the array @k using the same sort order as that of sorting array @v. Use '<=>' for numerical sort order and 'cmp' for lexicographic sort order. - Generate random numbers in perl?
my $range = 100;
my $random_number = int(rand($range)); - Obtain difference between two date-time strings?
use Date::Manip;
my $err; my $date1=ParseDate("02/Jul/2007:15:06:27");
my $date2=ParseDate("02/Jul/2007:15:16:27");
my $diff = DateCalc($date1, $date2, \$err); # Compute $date2-$date1 my $diff_in_mins= Delta_Format($diff, 0, "%mt"); # Difference in minutes.
Python
http://code.activestate.com/recipes/286222/