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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.