Introduction
The purpose of this page is to provide you useful codes you can use.

Regular Expression

To match integers with optional +/- sign
/[+-]?\d+/
To match a floating point
/[+-]?(\d+(\.\d*)?|\.\d+)/

Date and Time

Current Time using localtime
$time = localtime; print "$time";

Output:

Fri May 28 10:06:09 2004

Current time using time
$epochTime = time(); print "$epochTime";

Output:

1085710785

Retrieve current time in detail
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year = int($year);
$mon = int($mon) + 1;
$mday = int($mday);
$sec = int($sec);
$min = int($min);
$hour = int($hour);
$year += 1900 if $year < 1900;
Convert epoch time to human readable details
# assume the epoch time is stored at $epochTime
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime ($epochTime);
$year = int($year);
$mon = int($mon) + 1;
$mday = int($mday);
$sec = int($sec);
$min = int($min);
$hour = int($hour);
$year += 1900 if $year < 1900;

File Operation

Read a configuration file into Associative Array
my %conf;
open CONFIG, $configFile or die "couldn't open $configFile for reading";
foreach my $line (){
	chop $line;                       # remove CR
	$line =~ s/#.*$//;                # remove comment line
	next if $line eq "";              # skip empty line
	next if $line !~ m/[^=]+=.+/;     # check format of key=value
	my ($key, $value) = $line =~ /^([^=]+)=(.+)$/;
	$key = &trim($key);               # trim leading and trailing white space
	$value = &trim($value);
	$conf{$key} = $value;             # store in associative array
}
close CONFIG;

Sample configuration file to read

key1=value1
key2=value2
key3=value3
Read a directory and store filename into array
@filenames;
$dir = "c:\\ht\\project\\studyPerl";
opendir DIR, $dir or die "cannot open directory";
while (my $file = readdir(DIR)) {
	push @filenames, $file;
}
close DIR;

String Processing

Join contents of an array into a string with a given character in between the contents
@args = ("one", "two", "three");
$line = join ("-", @args);
print "$line \n";

Output:

one-two-three
Hosted by www.Geocities.ws

1