Search your unanswered perl queries
Google

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.

Wednesday, November 28, 2007

Regular Expressions : email id

Almost everyone of us have sometime or other tried to write a regular expression for email ids and other stuff. \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Here is a link to regular expression site which reports various aspects of regular expressions related to perl, shell and many others. It has a classic example of regex for email id.

Tuesday, November 27, 2007

One Liner : List all files of a directory.

If one wants to list all the files and directories from a particular directory here is the onliner for the same. We use IO::ALL for this purpose in this post.

perl -MIO::All -wle "my $io = io('e:/downloads'); print $io->All;"

and the output will list all the directories and files in downloads folder.

Comments and other ways are alwayz a welcome.

Thursday, November 22, 2007

Some Perl Tips

  1. No doubt the first tip is to use strict in whatever code you write. It helps in avoiding the typo and many other errors which otherwise are real difficult to bebug.
  2. When using if it is not necessary to emboss the condition in ()’s when if is used as the last part of the statement.
  3. @ARGV is the array which holds all the parameters passed to a perl program through a command line.
  4. For conditions in perl “”, 0 , “0”, undefined is false, everything else is true.
  5. It is confusing for some people to remember how unless works. One can remember it in a simple way by saying if not this then this happens. i.e unless (people){do this}; can be interpreted as if not people do this.
  6. When you want to comment a large amount of code you can always use a condition which will always be false. eg. if (1 > 2) { ...... }

If anyone of you have any other perl tips which you follow as a practice, please leave it in the comments section. I would like to add it to the list with your name as contributor and let people efficiently do their perl coding.

Friday, November 16, 2007

Perl Module : How to Install Perl Module on Windows

The most simple task to make your tasks simpler is installing a perl module on your machine.
There are three common ways by which one can install a perl module on your machine. Lets see the installation on windows machine in this post.

There is a ppm utility provided with every active state perl installation, atleast I got it with perl Vesion 5.8.X All you have to do is follow the below steps

1) Click Start
2) Click Run
3) Type cmd and press enter
4) Type ppm and press enter
5) Once you get the ppm utility prompt like ppm> type install modulename
eg. install Net::SSH::Perl
The ppm utility will carry the installation of the perl module for you.

There is workaround for this if you don't have ppm utility or want to install the module manually. Download the zip file from cpan and unzip it with unzip utitliy. Go to the respective directory on command prompt and enter the following commands in sequence.

1) c:\>Net-SSH-Perl>perl Makefil.PL
2) c:\>Net-SSH-Perl>nmake
3) c:\>Net-SSH-Perl>nmake test
4) c:\>Net-SSH-Perl>nmake install

And the module will be installed for you.

Do comment on how you install the new perl modules.