Search your unanswered perl queries
Google

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.

Tuesday, November 13, 2007

One Liner : To check if a module is already installed

I have always came at this stop where i needed to check if a module is already installed or if so what is the version of the module.

perl -e "use [modulename]" e.g perl -e "use Net::SSH::Perl" will tell you if the module is installed or not. If its installed nothing happens and you get the prompt to perform new task. If it is not installed perl throws an error somewhat like can't locate Net/SSH/Perl.pm

perl -M[modulename] -e "print $[modulename]::VERSION" will print the current version of the module. e.g perl -MCGI -e "print $CGI::VERSION"
Most of the packages has a package variable as VERSION which holds the version of the module.

However the only true way to determine if a module is correctly installed, one can run the test scripts provided in the t folder of the module distribution. If all the scripts can run successfully, then only one can be sure that a particular module is installed fully or properly.

Do let us know what method do you use to see if a perl module is installed on your machine.

Monday, November 12, 2007

One liner to shutdown a windows pc using perl

To shutdown a pc using perl one can write a one liner at command prompt as below

perl -e "sleep(3600); system('shutdown.exe -s')"

Thats it... the sleep(3600) will make the program sleep for 1 hour i.e 3600 seconds. According one can schedule the shutdown delay and have a peacefull sleep or walkaround in the park.

Hello Perlies!!!

Hello to all the perl people out there. I have worked on perl for quite a while now and would really like to share some of my findings which might be helpfull to atleast few of perl programmers across the world.