Wednesday, July 8, 2009
Printing Environment Variables Using Perl One Liner
Solution 1
C:\Documents and Settings\sivkumar>perl
use Data::Dumper;
print Dumper \%ENV;
Solution 2 - Perl One Liner
perl -MData::Dumper -e "print Dumper \%ENV"
Monday, July 6, 2009
Tilde-Tilde Operator
A bit obscure is the tilde-tilde "operator" which forces scalar context.
print ~~ localtime;
is the same as
print scalar localtime;
and different from
print localtime;
Spaceship Operator.
$a = 2 <=> 5; # $a is set to -1
$a = 5 <=> 2; # $a is set to 1
$a = 2 <=> 2; # $a is set to 0
Non-Obvious features in perl.
For example, did you know that there can be a space after a sigil?
$ perl -wle 'my $x = 3; print $ x'
3
Or that there you can give subs numeric names if you use symbolic references?
$ perl -lwe '*4 = sub { print "yes" }; 4->()'
yes
There's also the "bool" quasi operator, that return 1 for true expressions and the empty string for false:
$ perl -wle 'print !!4'
1
$ perl -wle 'print !!"0 but true"'
1
$ perl -wle 'print !!0'
(empty line)
Other interesting stuff: with use overload you can overload string literals and numbers (and for example make them BigInts or whatever).
Many of these thins are actually documented somewhere, or follow logically from the documented features, but nonetheless some are not very well known.
Update: Another nice one. Below the q{...} quoting constructs were mentioned, but did you know that you can use letters as delimiters?
$ perl -Mstrict -wle 'print q bJet another perl hacker.b'
Jet another perl hacker.
Likewise you can write regexes
m xabcx
# same as m/abc/
Tuesday, March 24, 2009
Error : Can't do inplace edit without backup
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 'Temp.bak' -e 's/siva/prabu/g;' TestScript.xml
It gives me
Can't open -e: No such file or directory.
Can't open s/siva/prabu/g;: No such file or directory.
Can't do inplace edit without backup.
Solution :
perl -pi.bak -e 's/siva/prabu/g;' TestScript.xml
or
perl -pi.bak -e "/siva/prabu/g;" TestScript.xml
Monday, March 23, 2009
Special Variables - @ARGV
Short Name : @ARGVScope : 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 arguments @ARGV[0]\n";
print $TestString;
Monday, March 16, 2009
Perl Handles Numbers
128 (positive integer)
-127 (negative integer)
0
17.5 (positive floating number)
-4.6E13 (negative 4.6 times 10 to the 13th power. E denotes exponential notation)
The last numeric literal above is an example of exponential or scientific notation, used when you need to work with very large or very small numbers.
In addition to decimal literals, Perl supports octal (base 8) and hexadecimal (base 16) literals.
Octal literals are denoted by a leading 0, and hex literals by a leading 0x or 0X.
For example:
0177 # 177 octal, same as 127 decimal
0xf0 # f0 hexadecimal, same as 240 decimal
- 0Xff # negative ff hexadecimal, same as -255 in decimal
Many people writing Perl programs will never need to work with octal or hex numbers - but be careful not to specify decimal numbers with a leading zero, because Perl will interpret them as octal.