Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: The Comma Operator

by petral (Curate)
on Jul 20, 2001 at 11:48 UTC ( [id://98347]=note: print w/replies, xml ) Need Help??


in reply to The Comma Operator

I once worked out this convoluted way to access time only once per reading without using a temporary.
perl -lwe'$oldtime = $^T; print( -(($oldtime+0) - ($oldtime = time)) ) while sleep 1'
(Not to be obscure: the normal way to do this is something like:my $tm = time;  print $tm - $oldtm;  $oldtm = $tm; The only complicating factor being that you don't want to call time() twice since it may roll over in between.  In order to avoid using a temporary, the old value of oldtime must be put into the calculation before oldtime is set to its new value which must also be used. A substraction requires that its arguments be ordered, and sub-expressions for both parts seems to work.)

But note well, the docs often note that the order of evaluation of arguments to functions is not defined. (This is a long-time caveat carried over from c.) For instance, the above can easily be broken:( -(($oldtime || $^T) - ($oldtime = time)) ) returns a stream of -0's!

Udpate: And here's a fibonacci series that depends on $b being returned *before* it is updated.
perl -e'$\=$,=$";$b=1; print $b, $a += $b += $a while $a <1000; print"\n"'
Update the second: Note also, that any halfway ambitious optimizer could reduce -( (x + 0) - (x = y) ) to (x = y) - (x) to x - x to 0 and produce:print 0;
  p

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found