I would (not) do:
#!/usr/bin/perl -w use strict; package TieTimeArray; sub TIEARRAY { bless [], shift } sub FETCH { (localtime)[$_[1]] } sub FETCHSIZE { 9 } package main; my @now; tie (@now, "TieTimeArray"); while (1) { local $"=' '; print "@now\n"; sleep 1; }

It compile and works (sort of). Indeed we hit a problem with the array tie design. you can't ask array content as a whole, so we ask local time for each element of the array. We hit the problem that gray codes are designed to avoid (pointer pending) Suppose that thru one defererence of @now, we ask time at 3:00 59.99999 sec to get the seconds, than again a jiffy later at 3:01 0 sec to get the minutes. We will give as a result 3:01 59 sec. Not good when it is instead 3:01 0 sec

So this is not really a good approach. So consider it only as a rather silly example of array tie code. If you want to tie here, tie to scalar.

-- stefp


In reply to Re: more on tying localtime by stefp
in thread more on tying localtime by blueflashlight

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.