5% assure discount from here only

Friday, March 8, 2013

(32) What is use of forcearrayref option in XML::Simple cpan module?

Solution :-

Here are some basic use of forcearray option in XMLin subroutine of XML::Simple module.

Here are 3 examples :-

use XML::Simple;

my $xml = qq~<user>
<fname>Lalit</fname>
<pnr>WERTED</pnr>
<pnr>TREFDS</pnr>
<passenger>BCDE</passenger>
</user>~;

Example 1 :-

my $data = XMLin($xml); # Without forcearray

By default XML::Simple return elements as scalar - unless no duplicate node found.
In duplicate node case it return arrayref.

In this example we have to check the elements is arrayref or not.

foreach my $keys (keys %$data) {
my $element = $data->{$keys};
foreach my $value(ref($element) =~ /ARRAY/ ? @$element : $element) {
print $keys."=>".$value."\n";
}
}

Example 2 :-

my $data = XMLin($xml, forcearray => 1);

In this example we do not have to check the elements is arrayref or not, because we are setting the forcearrayref 1. It always represent child elements as arrayrefs - even if there's only one.

foreach my $keys (keys %$data) {
my $element = $data->{$keys};
foreach my $value(@$element) {
print $keys."=>".$value."\n";
}
}

Example 3 :-

my $data = XMLin($xml, forcearray => ['pnr']);

In this example only those element will be arrayref where key is pnr, so we can access the element by using key name also.

foreach my $keys (keys %$data) {
my $element = $data->{$keys};
foreach my $values ($keys =~ /pnr/ ? @$element : $element) {
print $keys."=>".$values."\n\n";
}
}

Tuesday, August 7, 2012

(31) How to concatenate hashref or merge hashref?

Solution:

Perl provide  very easy way to concatenate or merge hashref data.

my $a = {1 => 2, 3 => 4};
my $b = {5 => 6, 7 => 8};

my $c = {%$a, %$b};

print Dumper($c);

Output will be like this :-

$VAR1 = {
 '1' => '2',
 '3' => '4',
 '5' => '6',
 '7' => '8',
};

Tuesday, July 10, 2012

(30) How to read and print the content of file, using line number?

Solution:

Sometime we need to print or show content from file on basis of line number.

Suppose we have a file, which contains 1000 of lines and we need to print the content from line number 30 to 50.

Following example will show, how can we do this :-

open FILE, "/location/filename";
while(<FILE>){
if($. > 29 && $. < 51){ # Here $. represents the line number
print $_; # Here $_ represents the content
}
}
close FILE;

Tuesday, July 5, 2011

(29) What is wantarray in perl?

Solution:

wantarray is a special keyword which returns a flag indicating which context your subroutine has been called in.

It will return one of three values.

true: If your subroutine has been called in list context
false: If your subroutine has been called in scalar context
undef: If your subroutine has been called in void context

For example:-

You have a subroutine named "display" and from that subroutine sometimes you want to return an array and sometimes a scalar.

So the syntax would be

sub display(){
return(wantarray() ? qw(A B C) : '1');
}

my @ab = display();
my $bd = display();

then output is

#@ab = (A B C);
and
#$bd = 1;

The output will be depend upon the left hand side
assignment. If you put an array (@ab) it will call
wantarray() else a scalar value will return.

Wednesday, June 1, 2011

(28) How to use ForkManager in perl code?

Solution:

This post is to demonstrate how to use Parallel::ForkManager, a simple and powerful Perl module available from CPAN that can be used to perform a series of operations in parallel within a single Perl script.

You need to instantiate the ForkManager with the "new" constructor. You must specify the maximum number of processes to be created. If you specify 0, then NO fork will be done; this is good for debugging purposes.

use strict;
use Parallel::ForkManager;

# set a number of maximum process you want to run
my $max_procs = 3;

my @names = qw( print online show );
# hash to resolve PID's back to child specific information

my $pm = new Parallel::ForkManager($max_procs);

# Setup a callback for when a child finishes up so we can
# get it's exit code
$pm->run_on_finish(
sub { my ($pid, $exit_code, $ident) = @_;
print "** $ident just got out of the pool ".
"with PID $pid and exit code: $exit_code\n";
}
);

$pm->run_on_start(
sub { my ($pid,$ident)=@_;
if($ident eq 'print'){
my $a = results(1);
print $a."\n";
}elsif($ident eq 'online'){
my $b = online(2);
print $b."\n";
}else{
my $c = show(3);
print $c."\n";
}
}
);

$pm->run_on_wait(
sub {
print "** Have to wait for one children ...\n"
},
0.5
);

foreach my $child ( 0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
$pm->finish($child); # pass an exit code to finish
}

print "Waiting for Children...\n";
$pm->wait_all_children;
print "Everybody is out of the pool!\n";

sub results{
my $process1 = shift;
return "Process number is $process1\n";
}

sub online{
my $process2 = shift;
return "Process number is $process2\n";
}

sub show{
my $process3 = shift;
return "Process number is $process3\n";
}

Do not use Parallel::ForkManager in an environment, where other child processes can affect the run of the main program, so using this module is not recommended in an environment where fork() / wait() is already used.