Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Perl vs C

by ikegami (Patriarch)
on Mar 14, 2009 at 17:32 UTC ( [id://750635]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl vs C
in thread Perl vs C

In Perl, sub x (--$i, $i++) behavior is also undefined

Not really. On all platforms, operands are always evaluated from left to right except for assignment operators, where it's right to left. People regularly rely on this.

sub foo { shift->bar('foo', @_)) }
{ my $x = $x; ... }

The latter is even documented.

The problem is that it gets really tricky when lvalues are involved.

f($i, --$i, $i);
The first argument is evaluated first, but it's still affected by the decrement because Perl passes by reference.

Replies are listed 'Best First'.
Re^4: Perl vs C
by Marshall (Canon) on Mar 14, 2009 at 20:34 UTC
    I think we both might be right on this question. There is one issue of lvalue calculation in a single statement. Then there is the issue of what happens in a sub call with say foo (++I, --I). In C, we don't know whether "I" will get incremented or decremented first. This is like when you go to the doctor and you say, "it hurts when I press here", and he says, "don't do that".

    The fact that we are "arguing" about some syntax says to me that the syntax isn't clear enough. I am not gonna argue precedence.. Hey, that there is even a question at this level means it the code was not clear enough to begin with!

    Think about what will happen say 2 years from now? My opinion is that clear code is good code.

      In C, we don't know whether "I" will get incremented or decremented first.

      Yes, you've already said that. And you said it was the same for Perl. I replied that in Perl, you do know.

      The fact that we are "arguing" about some syntax

      Since when were we talking about syntax? Our posts are entirely about operand evaluation order.

      I am not gonna argue precedence.

      Since when were we talking about operator precedence? Our posts are entirely about operand evaluation order.

      Think about what will happen say 2 years from now?

      Nothing. Operand evaluation order has been unchanged for at least a decade, probably since day one, and its the intention of developers not to change it. They couldn't even if they wanted because it would break existing in subtle ways.

      My opinion is that clear code is good code.

      Of course. And I gave two examples of clear code that relies on operand evaluation order.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://750635]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2026-01-20 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (125 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.