problem parsing conditional operator. 
Author Message
 problem parsing conditional operator.

Hello all,

I recently ran into a problem while writing a test script for a module
that I wrote.  (Actually, I'm not sure it's a problem which is why I'm
writing this post.)

Let me include a simplified example:

#!/usr/local/bin/perl -w
$variable1 = 2;
$variable2 = 2;

print $var1 == $var2 ? "equal\n" : "not equal\n";  # TEST 1

print ($var1 == $var2) ? "equal\n" : "not equal\n";   # TEST 2

If you run this little script the TEST 1 line will produce "equal" while
the TEST 2 line will produce "1" and a warning about Useless use of a
scalar in a void context.

This is because the line TEST 2 is being parsed such that the print is
called as a function with the expression $var1 == $var2, while the
conditional operator is just sort of left{*filter*}.

For this simple case the solution is obviously to remove the ()'s but
when I first encountered this problem I was trying to compare two more
complex expressions--something like:

print (EXPR1) == (EXPR2) ? "equal\n" : "not equal\n";

Again the parser sees the opening ( and passes EXPR1 to perl rather than
evaluating the conditional and passing it along.

This can be fixed in a number of ways including:

print "",(EXPR1) == (EXPR2) ? "equal\n" : "not equal\n";

Is it unreasonable to expect the parser to "look ahead" in such
situations and recognize the conditional?  I have only thought about
this particular case.  Any comments?  Is this a bug?

Thanks for your time,
--Rob



Fri, 23 Jun 2000 03:00:00 GMT  
 problem parsing conditional operator.

Quote:

> #!/usr/local/bin/perl -w
> $variable1 = 2;
> $variable2 = 2;

> print $var1 == $var2 ? "equal\n" : "not equal\n";  # TEST 1

> print ($var1 == $var2) ? "equal\n" : "not equal\n";   # TEST 2

> If you run this little script the TEST 1 line will produce "equal" while
> the TEST 2 line will produce "1" and a warning about Useless use of a
> scalar in a void context.

I think your problem here is one of precedence.  The parens create a
term,
which binds to the function "print" and the result of the print is then
tested by the question mark.  What you probably want to do is not to
drop
the parens, but add another pair around the entire expression whose
value
you want to print:
  print (($var1 == $var2) ? "equal\n" : "not equal\n");

Jack



Sat, 24 Jun 2000 03:00:00 GMT  
 problem parsing conditional operator.

Rob> print ($var1 == $var2) ? "equal\n" : "not equal\n";   # TEST 2

Rob> If you run this little script the TEST 1 line will produce "equal" while
Rob> the TEST 2 line will produce "1" and a warning about Useless use of a
Rob> scalar in a void context.
[...]
Rob> This can be fixed in a number of ways including:

Rob> print "",(EXPR1) == (EXPR2) ? "equal\n" : "not equal\n";

I prefer:

        print +(EXPR1 == EXPR2) ? "eq" : "ne", "\n";

That leading plus is merely discarded in the syntax tree, but it's
enough to make print a operator instead of a function.  I generally
don't do this on "code for long term", however.  I'll go the otherway
and make print the "ugly print with parens" :-):

        print ((EXPR1 == EXPR2) ? "eq" : "ne", "\n");

I suppose it doesn't look ugly to C hackers, but I've been away from C
for so long that I just can't bear putting parens around *every*
function call any more.  Not when they're optional! :-)

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 238 more days

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Sat, 24 Jun 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. A question on the Conditional Operator

2. Conditional Operator behavior?

3. Three Operand Conditional Operator

4. Ternary Conditional Operator Question

5. push onto trinary conditional operator?

6. newbie questions re conditional operators

7. Q: conditional operator and variable assignment

8. ternary conditional operator

9. conditional operator "?:"

10. Parsing the arrow operator -> into English

11. Conditional require - any problems?

12. Help: Problem with simple parsing and Parse::RecDescent

 

 
Powered by phpBB® Forum Software