Monks, why does the following code
#! /usr/local/bin/perl -l package P; sub new { use Time::HiRes 'time'; return bless { t => time }, 'P' } package main; print P->new->{t};

produce 1145538655.40264, and yet the following code (note the string eval)

#! /usr/local/bin/perl -l package P; sub new { eval "use Time::HiRes 'time'"; die $@ if $@; return bless { t => time }, 'P' } package main; print P->new->{t};

produce 1145538655 (that is, no fractional accuracy)? %INC does contain a Time::HiRes afterwards...

I have tried requireing and importing but that doesn't work either, and I'm at a bit of a loss. Basically, I'm just trying to attempt to load Time::HiRes on demand, and if it available, then I get improved time accuracy, and if it doesn't, well there's no harm done.

But as it stands, I never get improved accuracy, and I don't know what I'm missing.

Thanks for the clues (and I'll punch anyone who mumbles use strict :)

• another intruder with the mooring in the heart of the Perl


In reply to Importing Time::HiRes::time at run-time (and failing) by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.