Tuesday, May 8, 2012

Finding html tag

 Table Expression       => “]*>(.*?)” 
 Header Expression    => “]*>(.*?)” 
 Row Expression        => “]*>(.*?)” 
 Column Expression    => “]*>(.*?)” 
 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 treated as numeric

Sample Commands

sort -k 5 -n

sort -t ":" -k 3 -n /etc/psswd

Sunday, September 18, 2011

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 could have been called and the
return value assigned to a list of scalars.
sub CheckMyWantArray
{
my @info = caller(0);
my $wantarray = $info[5];
$wantarray='undef'
unless(defined($wantarray));
print "wantarray is '$wantarray'\n";
}
CheckMyWantArray; # undef
my $scal = CheckMyWantArray; # 0
my @arr = CheckMyWantArray; # 1
> wantarray is 'undef'
> wantarray is '0'
> wantarray is '1'

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 HowWasICalled {
my @info = caller(0);
print Dumper \@info;
}
HowWasICalled;
>$VAR1 = [
> 'main',
> './test.pl',
> 13,
> 'main::HowWasICalled',
> 1,
> undef,
> undef,
> undef,
> 2,
> 'UUUUUUUUUUUU'
> ];

The caller() function returns a list of information in the following order
0 $package package namespace at time of call
1 $filename filename where called occurred
2 $line line number in file where call occurred
3 $subroutine name of subroutine called
4 $hasargs true if explicit arguments passed in
5 $wantarray list=1, scalar=0, void=undef
6 $evaltext evaluated text if an eval block
7 $is_require true if created by "require" or "use"
8 $hints internal use only, disregard
9 $bitmask internal use only, disregard

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 parens.
# doesn't look like I'm passing any params
# but perl will pass @_ implicitely.
&second_level;
}
first_level(1,2,3);

> $VAR1 = [
> 1,
> 2,
> 3
> ];

Wednesday, September 14, 2011

what is it meants by '$_'?

It is a default variable which holds automatically, a list of arguements passed to the subroutine within parentheses.


Difference between 'my' and 'local' variable scope declarations

Both of them are used to declare local variables. The variables declared with 'my' can live only within the block and cannot gets its visibility inherited functions called within that block, but one defined as 'local' can live within the block and have its visibility in the functions called within that block.