Doing some XML research, I realized that DOM nodes must make available a lot of information that may or may not be used by a particular program. That means increased processing time for each DOM node created.

If you don't use a particular data member, why define it?

Luckily, Perl gives us a way to delay processing a piece of data until we really need it.

#!/usr/bin/perl -w use strict; package TieLazy; use Tie::Hash; @TieLazy::ISA = qw( Tie::StdHash ); sub new { my $class = shift; my %data; tie %data, "TieLazy", @_; bless \%data, $class; } sub TIEHASH { my $self = shift; my %data = %{ +shift }; bless \%data, $self; } sub FETCH { my $self = shift; my $item = shift; if (defined(my $data = ($self->{$item}))) { if (ref($data) eq 'CODE') { return $self->{$item} = $data->(); } else { return $self->{$item}; } } }
As an example, try the following:
sub do_two { print "Doing two!\n"; return 2; } my $tl = TieLazy->new({one => 1, two => \&do_two}); print join("\t", @$tl{qw( one two two )} ), "\n"; $tl->{three} = 3; print "$$tl{three}\n"; $tl->{four} = sub { print "Four!\n"; return 4; }; print "Four exists!\n" if (exists($tl->{four})); print "$$tl{four}\n"; print "$$tl{four}\n";
The constructor takes an anonymous hash. Keys become the name of the object's attributes. The values should be literal values (if not calculated only when accessed) or subroutine references which process and return the data.

The first time you access an element, the object will run the code reference and store and return the result. All subsequent accesses just use a simple retrieval.

Possible modifications include not deleting the original sub ref, which would allow a DELETE sub to clear out the data value, allowing it to be recalculated on the next access. Each value in the hash should probably hold a two-element anonymous array ([ data, code_ref ]).


In reply to Delayed Data Generation by chromatic

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.