Saturday, June 4, 2011

How to read part of file which is not determined by NEWLINE (\n)

One of the special variable in perl make this possible.

Special variable used :- $\ .

$\ is record separator, newline (\n) by default. $/ may be set to a value longer than one character in order to match a multi-character delimiter. If $/ is undefined (i.e. $\ = undef), no record separator is matched, and <FILEHANDLE> will read everything to the end of the current file.

Hence this can be used for following file which has many xmls:-

For example
________________________________________________________________________________
<DOC>
  <NAME>ABC</NAME>
  <CONTENT>This is first sample doc </CONTENT>
</DOC>

<DOC>
  <NAME>PQR</NAME>
  <CONTENT>This is second sample doc </CONTENT>
</DOC>

_________________________________________________________________________________

Now XML parser will fail for parsing above text:-

Hence we can seperate the xmls while reading from a file by using $/ special variable as follows:-
______________________________________________________________________________
local $/="</DOC>";
open FILE, "ap/ap890101";
while (<FILE>) {
     $value = "$_";
     print $value;
}
close FILE;
______________________________________________________________________________

Courtesy of link which provides various information about perl special variables :-
http://www.kichwa.com/quik_ref/spec_variables.html

Can't locate XML/DOM.pm in @INC:: How to install DOM parser in windows

I was frusterated for an hour to find how to install DOM parser in windows as it is not preinstalled in Strawberry Perl compiler.

Just type in command prompt :-

 c:\> ppm install XML-DOM

This will work perfert if all the "path" environment variables are initialized properly.

Tuesday, May 10, 2011

System.in (Standard input) in perl

Following program perfrom System.in in perl :-

open(INFO,'-');    #Open standard input handler and intiate handler to INFO (>- represent standard
                             #output)
$one = <INFO>; # read one line from the standard input as scalar varible is used
$two = <INFO>; # read second line
print $one;
print $two;
close(INFO)


Multiple Assignments in perl

Following code represents multiple assignments in perl :-
1.  ($a, $b) = (123, "123");
Here :-
$a = 123
$b = "123"

2. ($a , @b) = (123, "123", "456");

Here :-
$a = 123
@b = ("123", "456");

Note :- Same effect when @foo =  (123, "123", "456");
            And code is changed to ($a, @b) = @foo;

3. (@b , $a) = (123, "123", "456");

Here :-
@b = (123, "123", "456");
and $a is undefined as perl is greedy language and @b will swallow up as much of the array passed.

Special character $" in perl

Consider following array varaible :-

@foo = ("one", "two", "three");

When we say :-

print @foo;

We get following output

one two three

i.e. :- All values sperated by a space.

Now we will assign :-

$" = " ++++ ";
print foo;

Now the output will be :

one ++++ two ++++ three

Which means $" acts as a delimiter for array.

NOTE :-
Same behavior is seen when :-

$bar = "@foo"

What $variable and @variable means in perl

$variable signifies that the variable is a scalar variable which means that it can be assigned to a String or Number and the data type is interchangeable which means.

$foo = "Hello World";
print $foo;
$foo = 12;
print $foo;

------------------------------------------------------------------------------------------------------------

@variable signifies that the variable is an array of scalar variable.

Example :-

@bar = ("one", 2, "three");

The index starts from ZERO.

Following expression access the 1st index element.

$first = $bar[1];


Note:- @ is changed to $ as scalar variable is accessed.