Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   if statement oddity (http://www.velocityreviews.com/forums/t891541-if-statement-oddity.html)

Jens Luedicke 03-25-2005 06:43 PM

if statement oddity
 
Hiya..

I encountered a strange problem with an if () statement:

if ((my $r = &{$self->{onFile}}($path) != SUCCESS)) {
return $r;
}

The above code seems to ignore the return value of the function
and somehow overrides it. the return value of the called function
returns FAILED, but $r doesn't reflect that!?

The following code, works as expected:

my $r = &{$self->{onFile}}($path);

if ($r != SUCCESS)) {
return $r;
}

fyi, SUCCESS is a constant. use constant SUCCESS => 1;

both code examples should work the same. no matter what!?

do I miss something? I'm a little disturbed right now.


jens

--
Jens Luedicke
web: http://perldude.de

Gunnar Hjalmarsson 03-25-2005 06:53 PM

Re: if statement oddity
 
Jens Luedicke wrote:
> I encountered a strange problem with an if () statement:
>
> if ((my $r = &{$self->{onFile}}($path) != SUCCESS)) {
> return $r;
> }
>
> The above code seems to ignore the return value of the function
> and somehow overrides it. the return value of the called function
> returns FAILED, but $r doesn't reflect that!?


How about enabling warnings? Using the string 'FAILED' in a numerical
comparison isn't very wise.

But the behaviour is about precedence. The return value of the function
is compared with the SUCCESS constant, and $r is assigned the bolean
value resulting from that comparison. This should do what you want:

if ( ( my $r = &{$self->{onFile}}($path) ) ne SUCCESS ) {
return $r;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Felix Geerinckx 03-25-2005 07:06 PM

Re: if statement oddity
 
On 25/03/2005, Jens Luedicke wrote:

> if ((my $r = &{$self->{onFile}}($path) != SUCCESS)) {
> return $r;
> }


[...]

> my $r = &{$self->{onFile}}($path);
>
> if ($r != SUCCESS)) {
> return $r;
> }


[...]

> both code examples should work the same. no matter what!?
> do I miss something? I'm a little disturbed right now.



Look up the operator precedence rules at the top of

perldoc perlop

(!= is evaluated before =)

--
felix

Jens Luedicke 03-25-2005 07:17 PM

Re: if statement oddity
 
Gunnar Hjalmarsson wrote:

> How about enabling warnings? Using the string 'FAILED' in a numerical
> comparison isn't very wise.


I use warnings.

FAILED and SUCCESS are numerical constants.

the specific code is on the Filer/DirWalk.pm module of my Filer program.

> But the behaviour is about precedence. The return value of the function
> is compared with the SUCCESS constant, and $r is assigned the bolean
> value resulting from that comparison. This should do what you want:
>
> if ( ( my $r = &{$self->{onFile}}($path) ) ne SUCCESS ) {
> return $r;
> }
>


using 'ne' doesn't make a difference and wouldn't be practical in a
numerical comparison.

to avoid problems with precedence the assignment is enclosed in
parentheses. so the assignment should hbe evaluated first and its return
value compared afterwards. At least that's what I expect.


--
Jens Luedicke
web: http://perldude.de

Paul Lalli 03-25-2005 07:23 PM

Re: if statement oddity
 
Jens Luedicke originally wrote:
> Hiya..
>
> I encountered a strange problem with an if () statement:
>
> if ((my $r = &{$self->{onFile}}($path) != SUCCESS)) {
> return $r;
> }
>


Jens Luedicke then later wrote:
> to avoid problems with precedence the assignment is enclosed in
> parentheses. so the assignment should hbe evaluated first and its return
> value compared afterwards. At least that's what I expect.



Exactly where in your code are you using parentheses around the
assignment? You're enclosing the entire if statement in parentheses,
not any particular part of it.

Paul Lalli

Felix Geerinckx 03-25-2005 07:27 PM

Re: if statement oddity
 
On 25/03/2005, Jens Luedicke wrote:

> to avoid problems with precedence the assignment is enclosed in
> parentheses. so the assignment should hbe evaluated first and its
> return value compared afterwards. At least that's what I expect.


Look at your code (copied from your original post for reference):

> if ((my $r = &{$self->{onFile}}($path) != SUCCESS)) {
> return $r;
> }


The assignment isn't enclosed in parentheses. The full statement is
enclosed in double parentheses.

--
felix

Jens Luedicke 03-25-2005 07:33 PM

Re: if statement oddity
 
Felix Geerinckx wrote:

>
> The assignment isn't enclosed in parentheses. The full statement is
> enclosed in double parentheses.
>


****. little typo. big confusion.

mea culpa! shame on me...

/me hides

--
Jens Luedicke
web: http://perldude.de

Brian McCauley 03-25-2005 07:34 PM

Re: if statement oddity
 


Jens Luedicke wrote:

> Hiya..
>
> I encountered a strange problem with an if () statement:
>
> if ((my $r = &{$self->{onFile}}($path) != SUCCESS)) {
> return $r;
> }
>
> The above code seems to ignore the return value of the function
> and somehow overrides it. the return value of the called function
> returns FAILED, but $r doesn't reflect that!?


Er? Assuming FAILED != SUCCESS then $r will be set to false. I can't
recell off the top of my head which false but since it immediately goes
out of scope this doesn't matter.


>
> The following code, works as expected:
>
> my $r = &{$self->{onFile}}($path);
>
> if ($r != SUCCESS)) {
> return $r;
> }


No, that contains a syntax error and will not compile.

> fyi, SUCCESS is a constant. use constant SUCCESS => 1;
>
> both code examples should work the same. no matter what!?
>
> do I miss something? I'm a little disturbed right now.


I'm betting you meant

if ((my $r = &{$self->{onFile}}($path)) != SUCCESS) {
return $r;
}

Can I suggest that in future you produce a _minimal_ but _complete_
example that you have actually run and found to behave as you don't
expect. Post it together with a precices description of what it did and
what you expected.

This advice and much other useful advice can be found in the posting
guidelines.


Tad McClellan 03-25-2005 07:47 PM

Re: if statement oddity
 
Jens Luedicke <jens.luedicke@gmail.com> wrote:

> I encountered a strange problem with an if () statement:


[snip]

> do I miss something?



Yes.

You missed posting a short and complete program that we can run
that illustrates the problem you are having.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Gunnar Hjalmarsson 03-25-2005 08:07 PM

Re: if statement oddity
 
Jens Luedicke wrote:
> Gunnar Hjalmarsson wrote:
>> How about enabling warnings? Using the string 'FAILED' in a numerical
>> comparison isn't very wise.

>
> I use warnings.


Good.

> FAILED and SUCCESS are numerical constants.


I see. But you failed to explain that as regards FAILED.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


All times are GMT. The time now is 05:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.