Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Higher resolution $^T?

by ikegami (Patriarch)
on Jun 23, 2014 at 02:17 UTC ( [id://1090867]=note: print w/replies, xml ) Need Help??


in reply to Re: Higher resolution $^T?
in thread Higher resolution $^T?

So even if you wrote a module to capture a more precise time, it couldn't be stored in $^T because that scalar is too magic.

If something were to replace $^T, it would also replace the magic.

>perl -E"undef(*^T); $^T = 1400000000.123; say $^T;" 1400000000.123

Well, the floating point number is actually stored correctly in Perl's SV structure.

When assigning to a variable with set magic, the value is assigned as normal, then the magic handler is called.

When fetching from a magical variable with get magic, the magic handler is called to replace the value of the variable, then the fetch happens as normal.

This means you see the value assigned to the scalar in the scalar, but that value is meaningless because it gets clobbered when you read from the scalar.

The magic behind $^T is equivalent to the following:

use Variable::Magic qw( cast wizard ); # The C var $^T mirrors is named PL_basetime # An IV (signed integer) on all systems except OS/2 and VMS. my $PL_basetime; cast($^T, wizard( # Copies into $^T before fetching from it. get => sub { ${ $_[0] } = $PL_basetime; }, # Copies from $^T after assigning to it. set => sub { $PL_basetime = ${ $_[0] }; }, ));

Replies are listed 'Best First'.
Re^3: Higher resolution $^T?
by Tux (Canon) on Jun 23, 2014 at 05:47 UTC

    But now you also kill the magic of -M and friends:

    $ perl -wE'say $^T;say -M ".";sleep 1;say -M ".";$^T=time;say -M ".";u +ndef*^T;$^T=1400000000.123;say -M "."' 1403502321 0.450798611111111111 0.450798611111111111 0.450810185185185185 0.450810185185185185

    Note that the last -M uses what was last stored in $^T before it was undef'd and not the value you put in it.


    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found