Wednesday, August 26, 2009

Use of shift() function

Shift() function is mostly used to remove and return the first element from an  array reduces the the size of array by one. First element in a array is the lowest indexed element.While pop() removes the last element from the array, which is highest indexed.

@mynames = ("larry", "james", "peter");
$firstname = shift(@mynames);

# now $firstname will be "larry" and @mynames will be ("james", "peter").

Monday, August 24, 2009

using 2>&1

It's basically shell command for redirecting the standard error.
1. cat food 2>&1 >file
cat: can't open food
2. cat food >file 2>&1
In first command shell sees 2>&1 first and redirects the standard error(file descriptor 2)
to same place as standard outpur(file descriptor 1) goes ie STDOUT, while in second command
standard output goes to "file" so Standard Error(2) will also be redirected to same loaction
i.e.  to the "file"

Friday, August 21, 2009

Perl regular expressions(RE) - Part1

Perl is used by many people just because of Perl. It gives Perl very powerful capability of matching and substitution operations on text.
Regular expressions uses special characters to match text, which makes it very powerful for Text processing.

OPERATORS:
The Matching Operator : m/PATTERN/cgismxo
Substitution Operator : s/PATTERN/REPLACEMENT/egismxo
transliteration Operator : tr/SEARCHLIST/REPLACEMENTLIST/cds

all above operators generally works on $_
================================================================
Modifiers:
Following modifers are related to interpretation of PATTERN. Modifiers that alter the way a regular expression is used by Perl are detailed in PerlRE quote like operators
modifiers for matching:
/i : Ignore alphabetical case(case insens
/m : let ^ and $ match next to embedded \n.
/s : Let . match newline and ignore deprecated $*.
/x : Ignores white space and permit comments in patterns.
/o : to substitute variable only once and it will compile pattern only once .
/g : Globally find all matches, within a string all matches are searched for matching operator.
/gc : Global matching and keep current position after failed match, because failed match or change in target string resets the position, so to avoid, use this modifier.For further explanation refer to using regular expression in perlre tutorial

In substitution operation //c is not used instead //e.
/e : Evaluates right side as expression.

Transliteration Modifier:
/c : Complement SEARCHLIST.(character set in SEARCHLIST complemented so effective list will contain charcters not present in SEARCHLIST)
/d : Delete found but unreplaced characters(Any character specified in SEARCHLIST but not given a replacement in REPLACEMENTLIST are deleted).
/s : Squash duplicate replaced characters( sequences of characters converted to the same character are squashed down to a single instance of the character)
$word =~ tr/a-zA-Z//s;       # bookkeeper -> bokeper
====================================================================
Use of transliteration:
Converting from uppercase to lower case.
$tag = "This is My BLOG";
$tag_c = $tag;
$tag_c =~ tr/A-Z/a-z/; #prints "this is my blog"

$count = ($tag =~ tr/A-Z//); # counts the capital characters in $tag.
$count = ($tag =~ tr/A-Za-z//); # counts all the characters in $tag.
$count = ($tag =~ tr/aeiouAEIOU//); # counts all the vowels in $tag.
================================================================
=~ and =! are called binding operator
=~ means True if pattern matches and
=! means True if pattern doesn't match.

These are used when you want to use m// or s/// or tr/// to operate on other than $_, so the string to be serached is bound with binding operator.
===============================================================
Some useful links for regular expressions:
RegExpression Full Tutorial in Perldoc
Regular expressions quick tutorial in Perldoc
Regular Expressions (a tutorial)
http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

Monday, August 10, 2009

Use of quote like operators

Various "q" type of functions are mentioned below
  • q/STRING/
  • qq/STRING/
  • qr/STRING/
  • qx/STRING/
  • qw/STRING/
q// or q{} :
It represents single-quoted, literal string. It can be used instead of single quotes( ' '). Here // or {} are delimiters.

qq/ / or qq{ } :
It represents Double-quoted interpolated string. It can be used instead of single quotes( '" ").

e.g.
my $blog = "www.batchandperl.blogspot.com";

print q{ This is my blog: $blog \n}; # prints "This is my blog: $blog \n"

print qq{ This is my blog: $blog \n}; # prints "This is my blog: www.batchandperl.blogspot.com"


qr/STRING/msixpo or qr{ }msixpo :
This operator quotes it's string as regular expression. STRING will be interpolated in the same way as PATTERN in m/PATTERN/. If "'" is used as the delimiter, no interpolation is done. It Returns a Perl value which may be used instead of the corresponding /STRING/msixpo expression.
e.g.
$rex = qr/my.STRING/is;
s/$rex/foo/;

is equivalent to
s/my.STRING/foo/is
;

$re = qr/$pattern/;
$string =~ /foo${re}bar/; # can be interpolated in other patterns
$string =~ $re; # or used standalone
$string
=~ /$re/; # or this way

Options for using qr// are:
  1. m Treat string as multiple lines.
  2. s Treat string as single line. (Make . match a newline)
  3. i Do case-insensitive pattern matching.
  4. x Use extended regular expressions.
  5. p When matching preserve a copy of the matched string so
  6. that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be defined.
  7. o Compile pattern only once.

If a precompiled pattern is embedded in a larger pattern then the effect of 'msixp' will be propagated appropriately. The effect of the 'o' modifier has is not propagated, being restricted to those patterns explicitly using it.


qx/ / or qx{ } :
It's an alternative to using back quotes to execute system commands. It's equivalent to ( ` ` ).
e.g.
my @dir_out = qx{dir};
print @dir; #prints returned value by executing `dir` command on shell.


qw/STRING / or qw{};
It's a quick way to specify multiple single quoted words present in STRING and delimited by white space. Simply you can use qw() to create an array. It returns a list consisting of elements of LIST as they were single quoted.

It's equivalent to
 split(' ', q/STRING/);

e.g.
qw(foo bar baz); is equivalent to ('foo', 'bar', 'baz')

A very common mistake in STRING is to try to separate words with comma or to put comments
into multi-line qw() string. For this, pragma use warnings ; produces warnings.

quotemeta EXPR:
It returns a string with all meta characters escaped.
For example, quotemeta("AB*..C") returns "AB\*\.\.C".


print quotemeta("AB*\n[.]*");
#returns
AB\*\
\[\.\]\*

Friday, August 7, 2009

Links for websites for different types of programming

Documentation of all types of book (really good one){contains: Internet, Perl and other scripting languages, Web designing and publishing, JAVA, Data Bases, Image processing, Networking, Unix related, Net ware, Microsoft Products, etc.. }

O'Reilly CD BOOKSHELF (Books for JAVA, Perl, UNIX,Networking, Web Developer,Oracle, XML,etc....)

Web Programming Desktop Referencing (A book from above documentation link link)

Quick Reference Guides
( Quick reference guides for Perl's regular exp, special variables, and VI editor)


Special Variables

To know more about special variables see Perl documentation:
C:\>perldoc perlvar

There are some pre-defined in Perl which have special meaning to Perl. They also have some analogous names to them, to use long variable name you need to say in script:
use English;

$_ :
[$ARG]

The default input and pattern-searching space. In following places by default Perl will take $_ as argument. It's a global variable.

* print() and unlink functions.
* pattern matching operations s///, m//, tr/// when used without =~ operator
* default iterator variable in foreach(), if no variable supplied.

@ARGV
It's a array which stores all the command line arguments, with which the Perl program was called.

$(digits):
contains sub pattern matched with corresponding set of capturing parenthesis in last successful pattern match. These all variables are read only and dynamically scoped within the scope of that block.
e.g. m/^(ram:)(\w+ \.)(\d+)/;

In above match $1 will contains"ram:" and $2 contains any alphanumeric character ending with"." and $3 contains and digit character after that.

IO::Handle->input_record_separator(EXPR)
$INPUT_RECORD_SEPARATOR
$RS
$/ : (Global)
It actually splits the input file into records depending upon the value set to it, by default it's newline, so file will be split into lines. This influence Perl's idea of what a "line" is. It should be declared as "local" within any block to make it effective in that block only, otherwise it will remain set till the end of Perl program.

local $/; # Read the whole file in at once
$/ = undef; # Read the whole file in at once
undef $/; # same as above

$/ = ""; # Read in paragraph by paragraph
$/ = "\n%\n"; # Read in Unix fortunes

open(my $fh, "<", $fortunes) or die $!;
while(<$fh>) {
chomp; # remove \n%\n
}
  • We always think that chomp remove newline, it's because $/ is set to newline by default, So changing it's value will also change the way chomp works
  • $/ also changes the way read line(<>) works.
{
local $/ = undef;
open my $fh, "abc.txt" or die $!;
$_ = <$fh>; #whole file is now in $_
}
It is a compile-time error to try and declare a special variable using my.

For more information on Perl's special variables one can refer to any of the following links:

Perl 5 by Example: Using Special variables
Perl Special Variables: chapter 8
Perl In a nutshell: chapter 4
Perl Predefined Names
Perl-Special variables

Array and Hash

my @aray = ( 12, 13, 87, 238, 28, 761);
my $last_index = $#array; # it will be wrong if typed like #$aray, because it will comment out $array.
my $length = @array; # it's similar to typecasting, In this way we treating an array like a scalar

print "last index = ",$last_index,"\n"; #prints: 5
print "length of array = $length\n"; #prints: 6
print "second lat element of array = $array[-2] \n"; # prints: 28
print "fifth element = $array[4] \n"; #prints: 28

Hashes:
my %ageof = {
ram => 23,
shyam => 25,
rita => 30,
sita => 16,
};
my %fav_color = {
ram => "blue",
shyam => "white",
rita => "orange",
sita => "red",
};

"=>" is called as fat comma, it behaves same as normal comma(only it's bigger so easy to see), and it automatically quotes values to it's left, but values on right need to be quoted.

Hash Lookup:

print $ageof{shyam}; # prints 25
print $fav_color{rita}; # prints: orange

Adding Hah Pairs:

$ageof{mohan} = 47; # now mohan is added into hash %ageof;

Hash Size:

my $num_of_pairs = keys(%ageof);

Hashes don't Interpolate in quotes.

Arrays and Hashes shouldn't be passed to subroutines directly because it leads them to lost their identity, So they should be passed as reference.

if( greater_length( @array1, @array2 ) ) {
# .........
}
sub greater_length {
my ( @array1, @array2 ) = @_;
# @array1 now has "all" of the elements and @array2 is "empty"
return @array1 > @array2; # Always true!
}

# by using reference above problem can be solved

if( greater_length( \@array1, \@array2 ) ) {
# ........
}
sub greater_length {
my ( $array1, $array2 ) = @_;
my @array1 = @$array1;
my @array2 = @$array2;
return @array1 > @array2;
}

Monday, August 3, 2009

Quotes and interpolation

Perl has two kinds of qoutes to delimit the strings, double(" ") and single (' ').

my $fruit = "apple";
my $fruit ='apple' ;

but there is one difference that double quotes interpolate, while single quote doesn't. Interpolation allows to have variables inside double quotes and have those variables replaced with their content.

e.g.
print " my favorite fruit is $fruit \n"; #prints: my favorite fruit is apple

print ' my favorite fruit is $fruit \n'; # prints: my favorite fruit is $fruit

But exception is HASH which don't get interpolated within double quotes.
some special characters also can be interpolated within double quotes like \n, \t, \b ....while in case of single quotes these are merely pair of characters.
escape character(\) can be used to escape some characters within quotes like \\, \', \"
to avoid use of \ you can use qq{} instead of " " and q{} instead of ' '

Using Warnings

It's always good to have warnings enabled, so that u can get to know that u are doing something wrong. It's always worth spending some time wondering why?
A few things that trigger warning:
  1. Trying to read from or write to an unopened filehandle, socket or device.
  2. Treating a string of non-numeric characters as if it were a number.
  3. Printing or performing comparisons with undefined values.
  4. Assigning an odd number of elements to a hash (collection of key-value pairs).

using strict

STRICT:-
It ensures that you pre-declare your variables, don;t have barewords and don;t use symbolic references. It also saves from making accidental spelling mistakes:
#without strict;
$num_friend = 2;
print " I have $num_friends \n"; #oops wrong spelling

if you have used strict then case will die with error
Global symbol "$num_friends" requires explicit package name.

A bareword is any combination of letters, numbers, and underscores, and is not qualified by any symbols. That is, while $apple is a scalar and @apple is an array, while apple is a bareword.

There are a couple of areas in Perl where a bareword is allowed. A bareword string does not need to be qualified with quotes, and a subroutine doesn't always need the &. However in most of the cases taking these shortcuts leads to less readable code.

use strict;
use warnings;

sub say_hi {
my $first = shift;
my $name = shift;

print $first, $name, "\n";
return;
}

&say_hi("hi", "gkg");


But you should be aware that there are more than one way of calling a subroutine. In this case since we have already defined the subroutine so we don't need to prefaec it with &, and it can also be called as
say_hi("hi", "GKG."); #valid but not a correct way.

here you can also call it as:
say_hi "hi" , "GKG" ; #valid, but not a good way of coding

say_hi ; #valid, just prints new line character, no params in, just a bareword

while above command can be written in more readable way as :
&say_hi();

but there are some commands where parentheses are invalid:
#Valid syntax
print OUTPUT_FH $message, $name,"\n";

#Invalid Syntax
print(OUTPUT_FILE $message, $name, "\n");
parentheses indicates a list for print and needs comma between each paarmeter

#Okay but weird
print OUTPUT_FILE ($message, $name, "\n");

>>Use qoutes for strings.
>>Always call your subroutines with & and (), even if there are no args.
but when you are using "qw" then there is no need to have qoutes for variables inside a list.

Symbolic references:

the variable $var indicates that $var is a reference variable,and it is expected that the value will be returned to the variable referenced by $var when reference was taken. But if $var is not a refernce variable at all? then Perl tries to check whether it contains any string, if yes then perl mess around with this string.

$x = 0;
$var = "x";
$$var = 30; # Modifies $x to 30 , because $var is a symbolic reference !

so to avoid this situation you can use

use strict 'refs'; # Tell Perl not to allow symbolic references
$var = "x";
$$var = 30;

Strict refs will remain activated tillthe end of block, but it can be deactivated by saying,
no strict; # or
no strict 'refs';

for more on symbolic references refer to chapter 8.4 "Programming Perl" 3rd edition, O'relly.
or perldoc perlref .

Using Perl Documentation

There is lot of information on Perl documented as Perl documentation, to use this documentation you can type "perldoc" and some tags with it.
To get the info about these tags just type "perldoc perl" you will get all the commands which you can give.
some of the important commands which can be used to explore more in perldoc are as following:
perldoc perldoc: Instructions on using perldoc
perldoc perl :to get the overview of Perl documentation
perldoc perlintro : Perl Introduction for beginners
perldoc perltoc: Table of contents
perldoc perlfunc : full list of all functions
perldoc -f : Help with specific function
perldoc perlmodlib : List of all modules installed
perldoc perllocal : locally installed modules

Tutorials: perldoc _ _ _ _

perlreftut: Perl references
perlretut: Perl Regular Expressions
perlboot: perl object oriented tutorial for beginners
perlbot: some OO tricks and examples
perldebtut: debugging tutorial
perlfaq: Frequently asked questions
perlopentut : perl open() tutorial

perlbook & perlfaq2 : gives info about perl books

For reference manuals: perldoc_ _ _ _
perldata : perl data structures,
perlop : perl operators and precedence,
perlre : perl regular expressions,