For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > testing return values









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author testing return values
Mike Lesser

2007-04-22, 9:58 pm

Hiya. I'm looking for the correct Perl style for testing and storing
a return value in a control statement. The solution in any other
language is pretty obvious, but I get the distinct impression that
there's a 'right' way in Perl...

Let's say I want to test a scalar returned from a subroutine, and
also keep a copy for my own use:

$scalar = sub( $argument );

if( $scalar ){
}

Naturally that's no big deal. Now let's say I have a tree I want to
traverse, or some similar open-ended thing to evaluate, and want to
run it until a condition is reached..

while( read_tree( $argument ){
}

Again no biggie. The problem is if I want to keep the result.
Obviously I can't do this:

while( $tree_element = read_tree( $argument ) ){
do_something( $tree_element );
}

I can come up with a brute-force solution of course, but there's
probably a better, Perlish way that I'm not aware of. In addition, I
don't expect a return value from some module to be consistently
undefined or zero; it could change under some circumstances. This
makes me think that the problem has been dealt with long ago, and
just doesn't stick out in the llama/alpaca/whatever books.

Hopefully I explained this correctly!






Yitzle

2007-04-22, 9:58 pm

Now, why can't you do this?

> while( $tree_element = read_tree( $argument ) ){
> do_something( $tree_element );
> }


I would certainly use it. In C this is called operation overflow or something...


On 4/22/07, Mike Lesser <exceptions@earthlink.net> wrote:
> Hiya. I'm looking for the correct Perl style for testing and storing
> a return value in a control statement. The solution in any other
> language is pretty obvious, but I get the distinct impression that
> there's a 'right' way in Perl...
>
> Let's say I want to test a scalar returned from a subroutine, and
> also keep a copy for my own use:
>
> $scalar = sub( $argument );
>
> if( $scalar ){
> }
>
> Naturally that's no big deal. Now let's say I have a tree I want to
> traverse, or some similar open-ended thing to evaluate, and want to
> run it until a condition is reached..
>
> while( read_tree( $argument ){
> }
>
> Again no biggie. The problem is if I want to keep the result.
> Obviously I can't do this:
>
> while( $tree_element = read_tree( $argument ) ){
> do_something( $tree_element );
> }
>
> I can come up with a brute-force solution of course, but there's
> probably a better, Perlish way that I'm not aware of. In addition, I
> don't expect a return value from some module to be consistently
> undefined or zero; it could change under some circumstances. This
> makes me think that the problem has been dealt with long ago, and
> just doesn't stick out in the llama/alpaca/whatever books.
>
> Hopefully I explained this correctly!
>

Rob Dixon

2007-04-22, 9:58 pm

Mike Lesser wrote:
> Hiya. I'm looking for the correct Perl style for testing and storing a
> return value in a control statement. The solution in any other language
> is pretty obvious, but I get the distinct impression that there's a
> 'right' way in Perl...
>
> Let's say I want to test a scalar returned from a subroutine, and also
> keep a copy for my own use:
>
> $scalar = sub( $argument );
>
> if( $scalar ){
> }
>
> Naturally that's no big deal. Now let's say I have a tree I want to
> traverse, or some similar open-ended thing to evaluate, and want to run
> it until a condition is reached..
>
> while( read_tree( $argument ){
> }
>
> Again no biggie. The problem is if I want to keep the result. Obviously
> I can't do this:
>
> while( $tree_element = read_tree( $argument ) ){
> do_something( $tree_element );
> }
>
> I can come up with a brute-force solution of course, but there's
> probably a better, Perlish way that I'm not aware of. In addition, I
> don't expect a return value from some module to be consistently
> undefined or zero; it could change under some circumstances. This makes
> me think that the problem has been dealt with long ago, and just doesn't
> stick out in the llama/alpaca/whatever books.
>
> Hopefully I explained this correctly!


while (my $x = func()) {
:
}

is perfectly valid. In Perl every operator returns a value, and the assignment
operator is no exception. It's value is the expression on the left of the equals
sign, and it's even an lvalue so you can do things like

($x = 3)++;

leaving $x with a value of four. This sort of thing is more useful for things like

$p = $q = 'A';

which is the same as

$p = ($q = 'A');

or

$q = 'A'; $p = $q;

But the bottom line is that your while loop is quite valid.

HTH,

Rob

Paul Lalli

2007-04-23, 7:58 am

On Apr 22, 6:10 pm, excepti...@earthlink.net (Mike Lesser) wrote:

> Obviously I can't do this:
>
> while( $tree_element = read_tree( $argument ) ){
> do_something( $tree_element );
> }


That's not at all *obvious* to me. What makes you believe you can't
do that? How did that solution not meet your needs?

Paul Lalli

nobull67@gmail.com

2007-04-24, 7:58 am

On Apr 23, 1:48 pm, Paul Lalli <mri...@gmail.com> wrote:
> On Apr 22, 6:10 pm, excepti...@earthlink.net (Mike Lesser) wrote:
>
>
>
> That's not at all *obvious* to me. What makes you believe you can't
> do that? How did that solution not meet your needs?


Note that it's very unlikely that $tree_element needs to exist outside
the scope of the block controlled by the while loop. So it's far more
normal (in well written code) to see:

while( my $tree_element = read_tree( $argument ) ) {
do_something( $tree_element );
}



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com