Monday, January 18, 2010

How to break from if - else in Perl

One of the ways to break from if - else in Perl is to use goto

if ($num =~ /^\d+$/){
if($num == 13){
goto ENDOFIF;
}
} else {
print "Number not found.\n";
ENDOFIF:
}

So if the number is 13 then it will break from the if and place the control right at the end of outer if.

Sunday, January 10, 2010

How to calculate the time of execution of a Perl script

Just add the following line any where in your Perl script and it will print its time of execution right at the end:

# This calculates the time of execution of the Perl script.
END {warn "\nTime of execution - ", time - $^T, " second(s)\n"}