Re: Return value from function

2011-01-13 Thread Dr.Ruud

On 2011-01-12 22:23, Parag Kalra wrote:


On shell, successful command returns exit status of 0.

As a best practice what status value shall a Perl function return.


A function can return other kinds of values too.



Going by the fact that Perl function returns the value of last command
in it, I think function should return non-zero for a success.


It all depends. Read for example DBI(3), and see that some functions and 
methods return handles, other return the number of touched rows (with 
'0E0' as a special true zero), some will return a textual string, etc.


Often a worker function returns an undef (or an empty list) as a sign 
that something didn't work out. This enables coding like:


my $foo = bar( @baz )
or die "huh? bar() failed!";

There are also subs that don't return anything. You can for example 
write a sub that should change its parameters in place if the sub is 
called in void context, for example C.


--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Return value from function

2011-01-13 Thread Dr.Ruud

On 2011-01-13 08:18, John W. Krahn wrote:


If you want to return a false value it is usually better to use return
with no value:

return;


I prefer subs to normally return a single scalar, which can be a direct 
value like undef, or a reference to a more complex data structure, etc.


If you allow subs that do C, then you need to be careful if 
elsewhere in the code there are things like:


my $h = { a => foo(), b => bar() };

because if both foo() and bar() would just return, this ends up in meaning:

my $h = { a => b };

and that is clearly not what was meant by the coder.


To prevent this, you can change the code to

my $h = { a => scalar foo(), b => scalar bar() };

but that feels like fixing it at the wrong end to me.


Yeah, maybe the '=>' should enforce scalar context, though that would 
just lead to other surprises.



Simple rule: a sub that in normal circumstances returns a scalar, should 
not do C but do C in the special case.


--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Perl... Logging to console and Log file

2011-01-13 Thread vasanth
Hi friends,

Kindly help me regarding the issue Im facing with the below perl
script,

I have a below script which send the message to Console.

I need to modify this script it send message to a Log file.

Please find the below steps which i performed to send a message to
log.

Orginal Script:
===
return -1 on error
#0 pkg is starting or running and switchable
#1 pkg is starting or running and not switchable
#2 pkg is not running
_show_pkg() {
local pkg=${1:-ALL}#i.e 'GENPKG1,GENPKG3' or 'ALL' or
'GENPKG1' or ...
local mode=${2:-silent}#i.e 'normal'

clustat -flx | perl -MXML::Twig -e'
my %S = (started => q/UP/, recoverable => q/FAILED/, pending
=> q/FAILED/, stopped => q/DOWN/, failed => q/FAILED/, disabled => q/
DOWN/ );
my @A = split /,/, $ARGV[0];
my ($ret,$cs) = (0,0);
my $twig = new XML::Twig(TwigRoots => {node => 1, group => 1},
 TwigHandlers => { node  => sub {
my ($t,$n) = @_;
$cs = $ret = 1 unless ($n->att(rgmanager) eq q/1/ and $n->att(state)
eq q/1/);
},
   group => sub {
my ($t,$g) = @_;
my $pkg = (split /:/,$g->att(name))[1];
return unless (q/ALL/ eq $A[0] or grep /$pkg/, @A);
my $owner = "on ".substr($g->att(owner),0,-2) if $g->att(state_str) eq
q/started/;
unless ($ARGV[1] eq q/silent/) {
   print("State of $pkg is $S{$g->att(state_str)}");
   print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
switchable") if $owner;
   print "\n";
}
$ret = 1 if $g->att(restarts)>0;
$ret = 2 unless $g->att(state_str) eq q/started/;
}});
print(STDERR "Cannot open XML::Twig\n"), exit -1 unless
defined $twig; $twig->safe_parse(STDIN); exit $ret;
' ${pkg} ${mode}
}

Note  ===>  Steps

1. The  output of the above script will display below  information in
Screen ( console), it
will only display GENPKG{1..5} information on screen.

[root@incciozr0846 hbha]# show_pkg
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
[root@incciozr0846 hbha]#

2. I want to send the same message to log file, So I adopted the
script as shown below.

my $file1 = "/tmp/pmon/usbs_manage_ha.log";
open FILE, ">>$file1" or die "unable to open $file1 $!";

unless ($ARGV[1] eq q/silent/) {
   print("State of $pkg is $S{$g->att(state_str)}");
   print FILE ("State of $pkg is $S{$g->att(state_str)} \n ");
   print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
switchable") if $owner ;
   print FILE (" $owner and ",($g->att(restarts)>0 or $cs)?"not":"
"," switchable \n" ) if $owner ;
   print "\n";
}
  close FILE or die;

3) Kindly note, Now im able to write to $file1 ("/tmp/pmon/
usbs_manage_ha.log"), but its continiously writing to the
$file1  in loop,

< extract of messgae in log >


#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
#H: show_pkgcalled by: bash[2208] at Sat Jan 19 03:46:41 CET 2002
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
#H: show_pkg (ret = 2) terminate at Sat Jan 19 03:46:41 CET 2002
State of GENPKG5 is FAILED
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED
State of GENPKG4 is FAILED
State of GENPKG5 is FAILED
State of GENPKG1 is FAILED
State of GENPKG2 is FAILED
State of GENPKG3 is FAILED


4) Please provide your inputs to get rid of this loop,




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Perl OOP concept in real time scenario

2011-01-13 Thread Sunita Rani Pradhan
Hi All

 

Can anyone explain the Perl OOP concept with one real time
example ? How it is useful or required in our programming life? Any link
also would be helpful . 

 

Thanks

Sunita



Re: Perl... Logging to console and Log file

2011-01-13 Thread shawn wilson
On Jan 13, 2011 4:19 AM, "vasanth"  wrote:
>
> Hi friends,
>
> Kindly help me regarding the issue Im facing with the below perl
> script,
>
> I have a below script which send the message to Console.
>
> I need to modify this script it send message to a Log file.
>
> Please find the below steps which i performed to send a message to
> log.
>
> Orginal Script:
> ===
> return -1 on error
> #0 pkg is starting or running and switchable
> #1 pkg is starting or running and not switchable
> #2 pkg is not running
> _show_pkg() {
>local pkg=${1:-ALL}#i.e 'GENPKG1,GENPKG3' or 'ALL' or
> 'GENPKG1' or ...
>local mode=${2:-silent}#i.e 'normal'
>
>clustat -flx | perl -MXML::Twig -e'
>my %S = (started => q/UP/, recoverable => q/FAILED/, pending
> => q/FAILED/, stopped => q/DOWN/, failed => q/FAILED/, disabled => q/
> DOWN/ );
>my @A = split /,/, $ARGV[0];
>my ($ret,$cs) = (0,0);
>my $twig = new XML::Twig(TwigRoots => {node => 1, group => 1},
> TwigHandlers => { node  => sub {
> my ($t,$n) = @_;
> $cs = $ret = 1 unless ($n->att(rgmanager) eq q/1/ and $n->att(state)
> eq q/1/);
> },
>   group => sub {
> my ($t,$g) = @_;
> my $pkg = (split /:/,$g->att(name))[1];
> return unless (q/ALL/ eq $A[0] or grep /$pkg/, @A);
> my $owner = "on ".substr($g->att(owner),0,-2) if $g->att(state_str) eq
> q/started/;
> unless ($ARGV[1] eq q/silent/) {
>   print("State of $pkg is $S{$g->att(state_str)}");
>   print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
> switchable") if $owner;
>   print "\n";
> }
> $ret = 1 if $g->att(restarts)>0;
> $ret = 2 unless $g->att(state_str) eq q/started/;
> }});
>print(STDERR "Cannot open XML::Twig\n"), exit -1 unless
> defined $twig; $twig->safe_parse(STDIN); exit $ret;
> ' ${pkg} ${mode}
> }
>
> Note  ===>  Steps
>
> 1. The  output of the above script will display below  information in
> Screen ( console), it
> will only display GENPKG{1..5} information on screen.
>
> [root@incciozr0846 hbha]# show_pkg
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> [root@incciozr0846 hbha]#
>
> 2. I want to send the same message to log file, So I adopted the
> script as shown below.
>
> my $file1 = "/tmp/pmon/usbs_manage_ha.log";
> open FILE, ">>$file1" or die "unable to open $file1 $!";
>
> unless ($ARGV[1] eq q/silent/) {
>   print("State of $pkg is $S{$g->att(state_str)}");
>   print FILE ("State of $pkg is $S{$g->att(state_str)} \n ");
>   print(" $owner and ",($g->att(restarts)>0 or $cs)?"not":"   ","
> switchable") if $owner ;
>   print FILE (" $owner and ",($g->att(restarts)>0 or $cs)?"not":"
> "," switchable \n" ) if $owner ;
>   print "\n";
> }
>  close FILE or die;
>
> 3) Kindly note, Now im able to write to $file1 ("/tmp/pmon/
> usbs_manage_ha.log"), but its continiously writing to the
> $file1  in loop,
>
> < extract of messgae in log >
>
>
>
#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*
> #H: show_pkgcalled by: bash[2208] at Sat Jan 19 03:46:41 CET 2002
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> #H: show_pkg (ret = 2) terminate at Sat Jan 19 03:46:41 CET 2002
> State of GENPKG5 is FAILED
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
> State of GENPKG4 is FAILED
> State of GENPKG5 is FAILED
> State of GENPKG1 is FAILED
> State of GENPKG2 is FAILED
> State of GENPKG3 is FAILED
>
>
> 4) Please provide your inputs to get rid of this loop,
>

If the original script worked, why not use tee from the command line?


Re: Return value from function

2011-01-13 Thread John Delacour

At 02:34 -0500 13/01/2011, shawn wilson wrote:


I dig what you're saying about always using return. However I don't (have
never used / seen) a case where a sub returns last expression. An example
maybe?


#!/usr/local/bin/perl
use strict;
use feature qw(say);

say &SUB();
sub SUB {
  my $word = "My word!";
}

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Perl OOP concept in real time scenario

2011-01-13 Thread Shlomi Fish
Hi Sunita,

On Thursday 13 Jan 2011 11:46:38 Sunita Rani Pradhan wrote:
> Hi All
> 
> 
> 
> Can anyone explain the Perl OOP concept with one real time
> example ? How it is useful or required in our programming life? Any link
> also would be helpful .

First of all, note that I don't understand what you mean by "real time". Real 
time in computing normally means that:

http://en.wikipedia.org/wiki/Real-time_computing

Otherwise, I give the motivation for OOP and other paradigms here:

http://perl-begin.org/tutorials/perl-for-newbies/part3/

You should read the introduction, but note that the rest of the tutorial has 
become baroque in time, and you should see this instead:

http://perl-begin.org/topics/object-oriented/

For some examples of object-oriented code in Perl, you may wish to consult my 
CPAN directory ( http://search.cpan.org/~shlomif/ ) and other modules on CPAN:

Best regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Chuck Norris can make the statement "This statement is false" a true one.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




RE: Perl OOP concept in real time scenario

2011-01-13 Thread Sunita Rani Pradhan
Hi Shlomi

Thanks for your help . I meant that , in what kind of scenario
we should use OOP features(in perl) ? 

Thanks
Sunita 

-Original Message-
From: Shlomi Fish [mailto:shlo...@iglu.org.il] 
Sent: Thursday, January 13, 2011 6:22 PM
To: beginners@perl.org
Cc: Sunita Rani Pradhan
Subject: Re: Perl OOP concept in real time scenario

Hi Sunita,

On Thursday 13 Jan 2011 11:46:38 Sunita Rani Pradhan wrote:
> Hi All
> 
> 
> 
> Can anyone explain the Perl OOP concept with one real time
> example ? How it is useful or required in our programming life? Any
link
> also would be helpful .

First of all, note that I don't understand what you mean by "real time".
Real 
time in computing normally means that:

http://en.wikipedia.org/wiki/Real-time_computing

Otherwise, I give the motivation for OOP and other paradigms here:

http://perl-begin.org/tutorials/perl-for-newbies/part3/

You should read the introduction, but note that the rest of the tutorial
has 
become baroque in time, and you should see this instead:

http://perl-begin.org/topics/object-oriented/

For some examples of object-oriented code in Perl, you may wish to
consult my 
CPAN directory ( http://search.cpan.org/~shlomif/ ) and other modules on
CPAN:

Best regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Chuck Norris can make the statement "This statement is false" a true
one.

Please reply to list if it's a mailing list post - http://shlom.in/reply
.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Perl... Logging to console and Log file

2011-01-13 Thread Robert Wohlfarth
 Thu, Jan 13, 2011 at 2:15 AM, vasanth  wrote:

> Kindly help me regarding the issue Im facing with the below perl
> script,
>
> I have a below script which send the message to Console.
>
> I need to modify this script it send message to a Log file.
>
> Please find the below steps which i performed to send a message to
> log.
>

Take a look at 
Log::Log4perl(
http://search.cpan.org/~mschilli/Log-Log4perl-1.31/lib/Log/Log4perl.pm). It
handles multiple outputs seamlessly.

-- 
Robert Wohlfarth


Re: doubt in substring

2011-01-13 Thread Shawn H Corey

On 11-01-12 11:27 PM, Sunita Rani Pradhan wrote:

 I have a string as; $str =  "the cat sat on the mat" .



How the following command works substr($str , 4, -4)  on the string ?
What should be the output?


TITS (Try It To See)

perl -le '$str =  "the cat sat on the mat";print substr( $str, 4, -4 )'


--
Just my 0.0002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Perl OOP concept in real time scenario

2011-01-13 Thread Shlomi Fish
On Thursday 13 Jan 2011 14:55:10 Sunita Rani Pradhan wrote:
> Hi Shlomi
> 
>   Thanks for your help . I meant that , in what kind of scenario
> we should use OOP features(in perl) ?

Well, often you need to create more than one instance of something. In that 
case, consider creating a class and instantiating objects of it. OOP is also 
useful for standardising the interface of your project, and for shortening the 
amount of code needed to delegate records from one subroutine to another and 
to export and import functions.

Inheritance and role composition give you powerful tools to reuse code and 
over-ride parts of it. 

You may wish to read these parts of "The Art of Unix Programming" for some 
discussion of OOP:

* http://www.faqs.org/docs/artu/unix_and_oo.html

* http://www.faqs.org/docs/artu/ch14s04.html

However, I feel that OOP as it is implemented in C++ leaves a lot to be 
desired, and that "C++ supports Object Oriented Programming roughly as much as 
COBOL supports Functional Programming.", and that the way Perl implemented OOP 
is much better than C++'s.

Please read the rest of the resources I've linked to (and other resources 
online and offline). I'm not saying that you should always use OOP, but it's 
important to know, and I found it of great utility.

Regards,

Shlomi Fish


-- 
-
Shlomi Fish   http://www.shlomifish.org/
Stop Using MSIE - http://www.shlomifish.org/no-ie/

Chuck Norris can make the statement "This statement is false" a true one.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Moving through tree's using LWP

2011-01-13 Thread Brandon McCaig
On Wed, Jan 12, 2011 at 11:03 AM, Jim Gibson  wrote:
> Methods and subroutines are not called ("interpolated") within double
> quotes.

Unless you choose to be dirty and "trick"[1] Perl into doing it anyway:

print "@{[ $child->Tag() ]}\n";

Thanks to array dereferencing (i.e., @{} ) an anonymous array
reference (i.e., [] ).

Of course, it's more difficult to read and write (i.e., error-prone),
and probably somewhat wasteful considering the extra steps needed to
make it happen, so Jim Gibson's suggestion to print the result
directly or store it into a scalar first is certainly preferred in
good code. :P As I said, it's dirty. ^^


[1] http://www.perlcircus.org/subs.shtml


-- 
Brandon McCaig  
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software  

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/