Wednesday, August 29, 2007

Optimize for speed

Perl comes with a very handy module to help you evaluate the relative speed of different code snippets.

The Benchmark module comes with a function that will take a hash of code snippets and a number. The module will run each code snippet that many times and report the speed of each for you to compare.

use Benchmark;
$string = "The bitter end.\n";
$code{chomp} = 'chomp $string';
$code{regex} = '$string =~ s/\n$//';
timethese(10_000_000, \%code);

Devel::DProf

The Devel::DProf package is a Perl code profiler. This will collect information on the execution time of a Perl script and of the subs in that script. This information can be used to determine which subroutines are using the most time and which subroutines are being called most often. This information can also be used to create an execution graph of the script, showing subroutine relationships.

So to profile script test.pl the following command should be used:

perl -d:DProf test.pl

When the script terminates (or when the output buffer is filled) the profiler will dump the profile information to a file called tmon.out.

A tool like dprofpp can be used to interpret the information which is in that profile.
dprofpp

To print an execution graph of the subroutines in the script use the following command:
dprofpp -T

Tuesday, August 28, 2007

Smart::Comments

Smart comments provide an easy way to insert debugging and tracking code into a program. They can report the value of a variable, track the progress of a loop, and verify that particular assertions are true.

All smart comments start with three (or more) # characters. That is, they are regular #-introduced comments whose first two (or more) characters are also #'s.

use Smart::Comments;
### [] Acquiring data...
for $i (0 .. 100) { ### Processing ==[%]
think_about($i);
}
sub think_about {
sleep 1; # deep ponder
}

the above program would print a cool animated progress bar.

B::Fathom

A module to evaluate the readability of Perl code.

perl -MO=Fathom ScriptName

B::Fathom is a backend to the Perl compiler; it analyzes the syntax of your Perl code, and estimates the readability of your program.

Perl-Tidy

Perltidy is a tool to indent and reformat perl scripts. It can also write scripts in html format.

Try it in your command prompt as:

perltidy perlprgname.pl

For safety, perltidy never overwrites your original file. In this case, its output will go to a file named perlprgname.pl.tdy

Not only does it nicely format code, it does an excellent job of telling you where your syntax errors are.

Error report will be in

perlprgname.pl.ERR

If you want the Perl as html just type in:

perltidy -html perlprgname.pl

Use Perl to create PDF

The PDF::API2 package is used widely to create and manipulate PDF.

CODE:
use PDF::API2;
my $pdf = PDF::API2->new(-file => "HelloWorld.pdf");
$pdf->mediabox(595,842);
my $page = $pdf->page;
my $fnt = $pdf->corefont('Arial',-encoding => 'latin1');
my $txt = $page->text;
$txt->textstart;$txt->font($fnt, 20);
$txt->translate(100,800);
$txt->text("Hello Sheela! left-aligned");
$txt->translate(500,750);
$txt->text_right("Hello Sheela! right-aligned");
$txt->translate(300,700);$txt->text_center("Hello Sheela! center-aligned");
$txt->textend;$pdf->save;$pdf->end();