Monthly Archives: March 2009

Error : Can’t do inplace edit without backup

I am writing a command line perl program to replacetextcontentina file .I know that the following instruction executes successfully on one of the unix machine. I am trying to execute it through cygwin. perl -pi -e ‘s/siva/prabu/g;’ TestScript.xml But I get following error. Can’t do inplace edit without backup. I tried to run: perl -pi… Read More »

Special Variables – @ARGV

@ARGV Short Name   :         @ARGV Scope           :          always global This variable is an array of the arguments passed to the script. he first element of this array is the first argument (not the program name). As the arguments are processed, the value of this variable can alter. Example: $TestString = “There were $#ARGV arguments first… Read More »

Perl Handles Numbers

Perl can handle both whole numbers (integers, like 37) and floating-point numbers (real numbers with decimal points, like 17.5 or -235.2). Internally, Perl handles both as ‘double precision floating-point values‘, but in Perl code they are treated the same way and can be used interchangeably. Here are some examples of number literals : 128 (positive… Read More »

Basic Naming Rules in Perl

Variable names can start with a letter, a number, or an underscore, although they normally begin with a letter and can then be composed of any combination of letters, numbers, and the underscore character. Variables can start with a number, but they must be entirely composed of that number; for example, $123 is valid, but… Read More »

Printing a Hash

################### #Solution1 : ################### while(($key,$value) = each%hash){ print”$k => $vn”; } ################ #Solution2 : ################ printmap{“$_ => $hash{$_}n”}keys%hash; ################ #Solution3 : ################ print”@{[ %hash ]}n”; ################ #Solution4 : ################ { my@temp = %hash; print”@temp”; } ################ #Solution5 : ################ foreach$key(sortkeys%hash){ print”$key => $hash{$key}n”; }  

How to Take a Hash Slice

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   %hash = ( 1 => ‘one’, 2 => ‘two’,3 => ‘three’); @array = @hash{‘1′,’2’};   If you have a hashref and want to… Read More »

Establishing a Default Value

# # # # use $b if $b is true, else $c $a = $b || $c; # set $x to $y unless $x is already true $x ||= $y # use $b if $b is defined, else $c $a = defined($b) ? $b : $c; $foo = $bar || “DEFAULT VALUE”; $dir = shift(@ARGV)… Read More »

Get Pathname Of Current Working Directory

Windows use File::Spec; my $current_directory = File::Spec->rel2abs(“.”); print $current_directory; Unix use Cwd; my $current_directory = getcwd; print “$current_directory”; use Cwd; $dir = cwd; use Cwd; $dir = fastgetcwd; use Cwd ‘abs_path’; print abs_path($ENV{‘PWD’}); use Cwd ‘fast_abs_path’; print fast_abs_path($ENV{‘PWD’});    

Ordered Hashes In Perl

Perl hashes have no internal order. A hash consists of a number of “buckets”. When a record is inserted into the hash, the key is transformed, using a “hash function”, into a bucket number. The details of the hash function itself are not important, but a good hash function ensures that even very similar keys… Read More »

How many ways can we express string in Perl

Many. For example ‘this is a string’ can be expressed in: “this is a string” qq/this is a string like double-quoted string/ qq^this is a string like double-quoted string^ q/this is a string/ q&this is a string& q(this is a string)