Thursday, November 23, 2006

Perltidy

Perltidy is a great utility for formatting your perl codes. It splits long lines into short lines, does formatting with blocks thus increasing the readability of your codes. You can save your own settings related to formatting in a file called .perltidyrc. This file should saved in $HOME directory.

For more information and download of Perltidy, visit
http://perltidy.sourceforge.net/

Perltidy is coool !

Monday, November 20, 2006

Linux : Killing processes

Sometimes you have to kill several processes generated by a service/server (the main process) and killing one process is not enough. Recently, I had trouble with 'apachectl stop'. This would not stop all the processes of httpd for some strange reason. Killing all processes by entering the process id's would take quite some time. I figured a way out -

Here's the time-saver

ps -ef | grep httpd | awk '//{print $2}' | xargs kill

You can use the same logic for other processes. You need to replace httpd with ..... you know. As you can see, awk separates just the process ids and it becomes easier for us to kill the processes !

Sunday, November 19, 2006

Perl : Is this module Installed ?

How do you check if a certain perl module is installed and is in @INC ?

On the command-line, issue this command

perl -e 'use X::Y'

X::Y is the module
With -e, you can executes the perl code in quotes on command line.

If the module is found, there won't be any errors (no output).

Friday, November 17, 2006

Perl : Generating password with a subroutine

A subroutine to generate a password :-) When you find it hard to create one.

sub generatePassword
{
    my $passwd_size = shift;
    my @alphanum = ('a'..'z', 'A'..'Z', 0..9,'!','#','$','%','&','*','@','`');
    my $password = join '', map $alphanum[rand @alphanum], 0..$passwd_size;

    return $password;
}

Usage

&generatePassword(10); ### returns password of 10 chars

Tuesday, November 14, 2006

Perl : Installing CPAN modules

This post if for those who find it hard or don't know how to install CPAN modules.

1 Become root

2. Run

perl -MCPAN -e shell


First time you run this command, you'll be asked some questions regarding where to download/extract files etc. Just select the defaults.

3. This opens the cpan prompt. Now, run

install Module::Name

That's all you have to do. Isn't this really simple ?

Saturday, November 11, 2006

osix.net : A good website for programmers

I would like everybody to know I am a registered user of www.osix.net. There, I have cleared Geek Level 6 from the "Geek Challenge". These levels are programming puzzles on solving which you earn some reward points. You can exchange these reward points for hints from other geeks (experts), creating polls,  email address or pop access to email. There are some other redemption possibilities.

A great site for geeks !

My profile there
http://www.osix.net/modules/viewprofile/?name=ketan404

Wednesday, November 01, 2006

Perl : use Tie::File

With Tie::File module you can read all lines of a file in an array. Updating array elements updates the corresponding lines in the file. This is more efficient than reading the whole file, doing replacements and writing it back.

Following code snippet is an example

use Tie::File;

tie @line_array, 'Tie::File', 'file' or die "Can not tie file:$!";
for (@line_array) {
        s/pattern/replacement/g;
        ### or do anything that you wish to
        ### updating elements of array will update corresponding lines in the tied file
        ### pushing an element will add a line
}
untie @line_array;

Tie::File is very efficient for updating large files.