Search your unanswered perl queries

Friday, May 8, 2009

Reading a directory Sorting the files

The following post might help the readers to open a directory. skip the current directory . and previous directory .. and considering only the files which needs to be sorted. In files we use Tie::File to tie the records to the file. So whatever we operate on records changes are automatically made to the file.

Sample Function:

use Tie::File;

sub _sort_deployment_files{

my $deploy_dir

opendir(DIR, $deploy_dir);
@files = grep { /^[^\.]/ && -f "$deploy_dir/$_" } readdir(DIR);
closedir DIR;

foreach my $file_name (@files) {
tie my @records, "Tie::File", $file_name, autochomp => 0, mode => O_RDWR;
sort @records;
untie @records;


Wednesday, December 3, 2008

Redirect to /dev/null

I came across a code snippet like `$ypmatch $host hosts 2>/dev/null` used in one of the perl files I was browsing. Can anyone let me know the significance of redirecting it to /dev/null.

I will post the same when I find it out.

Cleaning A Perl File With REGEXP

Following are the regular expressions which can be used to clean your file of unwanted material

1) Replace all the spaces with nothing.


2) Skip the lines with comments.

next if (m/^\#/);

3) skip blank lines

next if (m/^$/);

4) Replace a line in the file which has commented data on a code line.


5) Replace a line in the file which has data after semicolon


Wednesday, November 5, 2008

To get current date and time. hh:mm::ss mm/dd/yy

To get the current date and time in the format hh:mm::ss mm/dd/yy and if you are looking for a perl one liner here you go.

perl -e "use POSIX; print strftime qq(%X %x), localtime(time); "

Output is

00:00:51 11/06/08

Thursday, October 30, 2008

Get a date in yyyymmdd format

use POSIX;
my $today = POSIX::strftime('%Y%m%d', localtime);

How to take a hash slice.

If you want to assign some variables of a hash to an array. i.e you want to take some of the elements from hash based on there keys into a array here is a trick\

my %hash = (one => 'one', two => 'two', three => 'three');
my @array ;
@array = @{$hash}{'one','two'};

If you have a hashref and want to take the values of some keys into an array you can do the following.

my $hashref = {one => 'one', two => 'two', three => 'three'};
my @array ;
@array = @{%$hash}{'one','two'};

Getopt::Long :s option specifier

: s option of getopt::long come very much handy when one is looking for the following requirement

1) If a command line option is passed a paramter it is taken as a value for the given option

i.e perl program -option1 value1

$option1 will take value1.

2) If a command line option is not mentioned then variable remains undefined.

i.e perl program

$option1 will remain undef.

3) If a command line option is mentioned but paramter is not passed, then a empty string will be assigned to $option if :s is used and 0 if :i is used

i.e perl program -option1

$option will be q{}... an empty string.

Please take a look at for further information.