if statement oddity

J

Jens Luedicke

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
 
G

Gunnar Hjalmarsson

Jens said:
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;
}
 
F

Felix Geerinckx

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 =)
 
J

Jens Luedicke

Gunnar said:
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.
 
P

Paul Lalli

Jens said:
Hiya..

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

if ((my $r = &{$self->{onFile}}($path) != SUCCESS)) {
return $r;
}
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
 
F

Felix Geerinckx

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.
 
J

Jens Luedicke

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

shit. little typo. big confusion.

mea culpa! shame on me...

/me hides
 
B

Brian McCauley

Jens said:
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.
 
T

Tad McClellan

Jens Luedicke said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top