I am trying to write a function that makes use of a cache. The problem is that the data in the cache should be initialized as it is used. However, the cache holds onto the raw form, and subsiquent accesses require the data to be initialized again.

Is there a way to make this work?
#!/usr/bin/perl -w use diagnostics; use strict; { my $cache = [Foo->new]; sub access_cache { return @$cache; } } foreach (&access_cache()) { print "before: ", ref($_), "\n"; print "object: ", $_, "\n"; print "after: ", ref($_), "\n"; print "\n"; } foreach (&access_cache()) { print "before: ", ref($_), "\n"; print "object: ", $_, "\n"; print "after: ", ref($_), "\n"; print "\n"; } package Foo; sub new {bless [], $_[0]}; use overload '""' => sub {$_[0] = Bar->new; return $_[0]->[0]}; package Bar; sub new {bless ['barbar'], $_[0]}; use overload '""' => sub {return $_[0]->[0]};
The output of this program is:
before: Foo object: barbar after: Bar before: Foo object: barbar after: Bar
I am trying to get it so that the output will look like this instead:
before: Foo object: barbar after: Bar before: Bar object: barbar after: Bar
I am using This is perl, version 5.005_03 built for i386-freebsd.

I would greatly appreciate any assistance that is offered.

In reply to Implementing a Data Cache by zealot

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.