Search your unanswered perl queries
Google

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 http://search.cpan.org/dist/Getopt-Long/lib/Getopt/Long.pm for further information.

Wednesday, January 30, 2008

Perl is Compiled or Interpreted?

Everyone knows that there are two types of languages interpreted and compiled. Does anyone know what type of language a perl is? If your instant answer is interpreted, maybe this post will make you think otherwise.

When one runs a perl program, perl actually generates a intermediate code called as the bytecode. Now when perl is finished with bytecode where it usually compiles the whole of a program, it takes over the responsibility of being an interpreter and acts accordingly scanning and processing each line of code. So perl is actually an intermediate language. It is neither a strictly compiled language nor a strictly interpreted one.

Tuesday, December 18, 2007

__DATA__ a Virtual File In Perl

It has been occurred a lot of times that one has to test a code using data from a file. It can be anything like checking for a pattern in a file to taking some input. If you want to check the code without opening the file and reading it. One can use the __DATA__ marker provided by perl as a pseudo-datafile.

Steps to follow:
1) At the end of the code use __DATA__ marker and copy paste or write the contents of the file you wish to test exactly from the next line of _DATA_.
2) Use while (){ -- your code here -- }.

#!/usr/bin/perl
use strict;
use warnings;

while () {
chomp $_;
print "Found" if $_ =~ /(t\w+)/;
print $1;
}
__DATA__
hello there how r u

Output of the above program will be “there”.

Note: If you have any queries do leave your comments.

Friday, December 14, 2007

Executing Commands on Remote Machine

Hello Everyone, This time it is not a solution on something. Instead it a a problem which I require a solution to and is related to a critical task at my work. And would require your valuable comments on this.

I want to execute commands on a remote machine via Apache. I will be using the Net::SSH::Perl module to do it. But when I try doing it from shell running the script as root it get executed and the expected results are shown. However when I execute it through CGI with apache as user it doesn't get executed.

After digging it a lot I think it might be the permission issue where apache does not have permissions to execute the script on remote machine. Executing apache script as root will not be secure. SO do you guyz have any comment on this.