31 Dec 2010

Perl New year wish

perl -le 'print pack "H*","4861707079204e657720596561722032303131"'

22 Dec 2010

Download youtube videos - Perl

Perl one-liner to download youtube videos

perl -MWWW::Mechanize -e '$m = WWW::Mechanize->new; $_=shift; ($i) = /v=(.+)/; s/%(..)/chr(hex($1))/ge for (($u) = $m->get($_)->content =~ /l_map": .+(?:%2C)?5%7C(.+?)"/); $m->get($u, ":content_file" => "$i.flv")'

Use it as following:

$ perl ... http://www.youtube.com/watch?v=ID

It will save the video to a file named "ID.flv".

It was written by Peteris Krumins (peter@catonmat.net).
His blog is at http://www.catonmat.net -- good coders code, great reuse.


Framework for getting process information

Find it at: cpan


This modules gets the process information

Who should use it?

Very useful for sysadmins

Good features:

Good to extract process related information and it is very fast

What are the drawbacks or problems?

Not working on some Linux flavors
There are 13 open bugs

Example code:

#!/usr/bin/perl -w

use strict;

print join("\n", &memusage), "\n";
exit 0;

# memusage subroutine
# usage: memusage [processid]
# this subroutine takes only one parameter, the process id for
# which memory usage information is to be returned. If
# undefined, the current process id is assumed.
# Returns array of two values, raw process memory size and
# percentage memory utilisation, in this order. Returns
# undefined if these values cannot be determined.

sub memusage {
use Proc::ProcessTable;
my @results;
my $pid = (defined($_[0])) ? $_[0] : $$;
my $proc = Proc::ProcessTable->new;
my %fields = map { $_ => 1 } $proc->fields;
return undef unless exists $fields{'pid'};
foreach (@{$proc->table}) {
if ($_->pid eq $pid) {
push (@results, $_->size) if exists $fields{'size'};
push (@results, $_->pctmem) if exists $fields{'pctmem'};
return @results;

Perl monks
Perl monks

5 Dec 2010

Facebook Puzzle - Perl - Breathalyzer

Facebook puzzle Breathalyzer can be solved easily using Levenshtein distance calculation Algorthim. More details about this algorithm is here Levenshtein distance

If you are trying to solve using Perl have a look at Text::Levenshtein

This module is available in cpanText::Levenshtein

Facebook Puzzle - Perl - hoppity

Facebook has added Puzzles section under its careers section. You can follow this procedure to get your test puzzle submission done.

1. Save this script in a file hoppity.


use strict;
use warnings;

my $fh;

my $path = $ARGV[0];

die("Missing Filename. Run $0 \n") if(! $path);
die("File not found ") if(! -f $path);

open($fh,"<$path") || die("Error reading file");
my $number = <$fh>;

$number = trim($number);

die("Invalid input $number") if($number !~ /\d+/);

$\ = "\n";
for(my $i=1;$i<=$number;$i++) {
if ($i % 3 == 0 && $i % 5 == 0) {
print "Hop";
} elsif ($i % 3 == 0) {
print "Hoppity";
} elsif ($i % 5 == 0) {
print "Hophop";

sub trim {
my ( $str) = @_;
$str =~ s/^\s*//g;
$str =~ s/\s*$//g;
return $str;

2. Send this file to 1051962371@fb.com with the file attached and subject as hoppity

3. After four hours facebook puzzle bot will reply you with your submission results

2 Dec 2010

Perl Best practices - Coding

Ten Essential Coding Practices by Damien conway in PBP book

1. Always use strict and use warnings.

2. Use grammatical templates when forming identifiers.

3. Use lexical variables, not package variables.

4. Label every loop that is exited explicitly, and every next, last, or redo.

5. Don’t use bareword filehandles; use indirect filehandles.

6. In a subroutine, always unpack @_ first, using a hash of named arguments if
there are more than three parameters.

7. Always return via an explicit return.

8. Always use the /x ,/m , and /s flags, and the \A and \z anchors.

9. Use capturing parentheses in regexes only when deliberately capturing, then give
the captured substrings proper names.

10. Never make variables part of a module’s interface.

Perl Best practices - Development

Ten Essential Development Practices

1. Design the module’s interface first.

2. Write the test cases before the code.

3. Create standard POD templates for modules and applications.

4. Use a revision control system.

5. Create consistent command-line and configuration interfaces.

6. Agree upon a coherent layout style and automate it with perltidy.

7. Code in commented paragraphs.

8. Throw exceptions instead of returning special values or setting flags.

9. Add new test cases before you start debugging.

10. Don’t optimize code—benchmark it.

vimrc for Perl coding from PBP by damien conway

vim is the desired editor for most of the Perl programmers. This .vimrc is recommended by Damien conway in Perl Best practices book

set autoindent "Preserve current indent on new lines
set textwidth=78 "Wrap at this column
set backspace=indent, eol, start "Make backspaces delete sensibly
set tabstop=4 "Indentation levels every four columns
set expandtab "Convert all tabs typed to spaces
set shiftwidth=4 "Indent/outdent by four columns
set shiftround "Indent/outdent to nearest tabstop
set matchpairs+=<: > "Allow % to bounce between angles too
"Inserting these abbreviations inserts the corresponding Perl statement. . .
iab phbp #! /usr/bin/perl –w
iab pdbg use Data: : Dumper ' Dumper' ; warn Dumper [ ] ; ^[ hi
iab pbmk use Benchmark qw( cmpthese ); ^Mcmpthese -10, {}; ^[ O
iab pusc use Smart: : Comments; ###
iab putm use Test: : More qw( no_plan );
iab papp ^[ : r ~/. code_templates/perl_application. pl^
iab pmod ^[ : r ~/. code_templates/perl_module. pm