Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Order of operations, mutators, aliasing and whammies

by Abigail-II (Bishop)
on Sep 04, 2003 at 23:06 UTC ( [id://289052]=note: print w/replies, xml ) Need Help??


in reply to Order of operations, mutators, aliasing and whammies

Now this behaviour confused me. Why should the extra meaningless subtraction affect the order of operations?

Well, that's easy. You have more operations! Of course things will change. ;-)

Without the subtraction, all perl needs to do is remember where the value of $ofs is, and get it when it's time to output the resulting string. Of course, in between, the value gets modified. With the subtraction, perl gets the value of $ofs, subtract 0, and scribbles away the result. That result won't get modified when $ofs gets modified.

Don't modify a variable and use its value elsewhere in the same expression. Don't modify a variable twice in the same expression. Don't assume Perl has a defined order of evaluation.

Abigail

  • Comment on Re: Order of operations, mutators, aliasing and whammies

Replies are listed 'Best First'.
Re: Re: Order of operations, mutators, aliasing and whammies
by demerphq (Chancellor) on Sep 04, 2003 at 23:19 UTC

    Without the subtraction, all perl needs to do is remember where the value of $ofs is, and get it when it's time to output the resulting string. Of course, in between, the value gets modified. With the subtraction, perl gets the value of $ofs, subtract 0, and scribbles away the result. That result won't get modified when $ofs gets modified.

    Yep. Thats what I tried to say myself, but youv'e done a much better job. :-)

    Don't modify a variable and use its value elsewhere in the same expression. Don't modify a variable twice in the same expression. Don't assume Perl has a defined order of evaluation.

    Yep, its just that Perls loose rules for this stuff make it so easy to do. Given enough rope to hang themselves... :-)


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: Re: Order of operations, mutators, aliasing and whammies
by asarih (Hermit) on Sep 05, 2003 at 18:04 UTC
    I disagree with the last piece of advice. Perl does have a defined order of evaluation; it is just not the same as your (or my) assumptions. Imagine that Perl did not have such a defined order of evaluation. How can I trust
    print 5+1;
    always prints 6, and not 5 (and a warning)?

    update: I see that I was confusing precedence and evaluation order.

      Perl does have a defined order of evaluation;

      Do you know something that neither p5p nor comp.lang.perl.misc knows? Order of evaluation has been discussed recently on both forums, and noone one these forums has been able to show that Perl has a defined order of evaluation. Please quote the relevant parts of the documentation that define order of evaluation in general. (Order of evaluation is defined for &&, ||, and, or and comma in scalar context, but not generally).

      How can I trust
      print 5+1;
      always prints 6, and not 5 (and a warning)?

      Precedence garantees that. Just like C, Perl does have well defined precedence and associativity rules. But neither language has a defined order of evaluation.

      Abigail

        This one thing amazes me. ML is functional (though not purely), side-effects are pretty rare there, yet it does specify the order of evaluation in all cases. C and Perl are full of side-effects, yet they don't.

        Jenda
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
           -- Rick Osborne

        Edit by castaway: Closed small tag in signature

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found