Kunaljain's Weblog

November 5, 2008

keyword “break” in perl

Filed under: Perl — Kunal Jain @ 10:15
Tags:

If you want to use break in your loop in perl then use keyword “last” instead of break.

December 7, 2007

Removing spaces in Perl

Filed under: Perl — Kunal Jain @ 06:22

Perl trim function to remove whitespaces from the start and end of the string

sub trim($)

{

mys $string = shift;

$string =~ s/^\s+//;

$string =~ s/\s+$//;

return $string;

}

OR

sub trim($)

{

$string = shift;

$string =~ s/^\s+|\s+$//g;

return $string ;

}

Left trim function to remove leading whitespaces

sub ltrim($)

{

my $string = shift;

$string =~ s/^\ s+//;

return $string;

}

Right trim function to remove trailing whitespaces

sub rtrim($)

{

my $string = shift;

$string =~ s/\s+$//;

return $string;

}

November 26, 2007

To print out the system date in perl

Filed under: Perl — Kunal Jain @ 06:45

use Time::localtime;

$tm = localtime;

printf(“%04d-%02d-%02d\n”, $tm->year+1900,

$tm->mon)+1, $tm->mday));

Blog at WordPress.com.