Category Archives: Perl

Logical Defined-Or in Perl

// It’s exactly the same as ||, except that it tests the left hand side’s definiteness instead of its truth. Thus, EXPR1 // EXPR2 returns the value of EXPR1 if it’s defined, otherwise, the value of EXPR2 is returned. For Example : $home= $ENV{HOME} // $ENV{LOGDIR} // (getpwuid($<))[7] // die “You’re homeless!n”;    

Finding html tag

Table Expression        =>    “<table[^>]*>(.*?)” Header Expression     =>    “ <th[^>]*>(.*?)” Row Expression          =>    “<tr[^>]*>(.*?)” Column Expression    =>    “ <td[^>]*>(.*?)” Finding HTML tag          =>    “s]+))?)+s*|s*)/?>”

Sort-command alphanumeric

Sort-command alphanumeric If they all actually begin with “page$” echo “page$1 page$20 page$2 page$10” | sort -k1.5n Answer : page$1 page$2 page$10 page$20 sort -k1.5n -k to set the key 1.5 means that the 5th character of the 1st field is the start of the sort field n means that this field is to be… Read More »

The caller() function and $wantarray

The argument of interest is the $wantarray argument. This indicates what return value is expected of the subroutine from where it was called. The subroutine could have been called in void context meaning the return value is thrown away. Or it could have been called and the return value assigned to a scalar. Or it… Read More »

Using the caller() Function in Subroutines

The caller() function can be used in a subroutine to find out information about where the subroutine was called from and how it was called. Caller takes one argument that indicates how far back in the call stack to get its information from. For information about the current subroutine, use caller(0). # # # sub… Read More »

Implied Arguments in perl

When calling a subroutine with the “&” sigil prefix and no parenthesis, the current @_ array gets implicitely passed to the subroutine being called. This can cause subtly odd behaviour if you are not expecting it. # # # sub second_level { print Dumper @_; } sub first_level { # using ‘&’ sigil and no… Read More »

Why should I use the -w argument with my Perl programs?

Many Perl developers use the -w option of the interpreter, especially during the development stages of an application. This warning option turns on many warning messages that can help you understand and debug your applications. To use this option on Unix systems, just include it on the first line of the program, like this: #!/usr/bin/perl… Read More »


Perl provides access to environment variables via the global “%ENV” hash. However, if using the ENV module, it will create global scalars for all the environment variables. use Env; print “$USER uses $SHELL”; If required to get only a few selected variables then it can be done like: use Env qw[@PATH $USER]; print “$USER’s path… Read More »


Its usage is like: use FindBin; use lib “$FindBin::Bin/../lib”; or use FindBin qw($Bin); use lib “$Bin/../lib”;

Using hash for counting

When we have an array or a list of items and we want to find out the number of occurrences of a particular item then we generally use the following kind of logic: my $count = 0; for (@list) { $count++ if $_ eq “apple”; } This can be made better by using the grep… Read More »