Inheriting from Tie::Memoize


Tie::Memoize − add data to hash when needed


  require Tie::Memoize;
  tie %hash, 'Tie::Memoize',
      \&fetch,                  # The rest is optional
      $DATA, \&exists,
      {%ini_value}, {%ini_existence};


This package allows a tied hash to autoload its values on the first access, and to use the cached value on the following accesses.

Only read-accesses (via fetching the value or "exists") result in calls to the functions; the modify-accesses are performed as on a normal hash.

The required arguments during "tie" are the hash, the package, and the reference to the "FETCH"ing function. The optional arguments are an arbitrary scalar $data, the reference to the "EXISTS" function, and initial values of the hash and of the existence cache.

Both the "FETCH"ing function and the "EXISTS" functions have the same signature: the arguments are "$key, $data"; $data is the same value as given as argument during tie()ing. Both functions should return an empty list if the value does not exist. If "EXISTS" function is different from the "FETCH"ing function, it should return a TRUE value on success. The "FETCH"ing function should return the intended value if the key is valid.

Inheriting from Tie::Memoize

The structure of the tied() data is an array reference with elements

  0:  cache of known values
  1:  cache of known existence of keys
  2:  FETCH  function
  3:  EXISTS function
  4:  $data

The rest is for internal usage of this package. In particular, if TIEHASH is overwritten, it should call SUPER::TIEHASH .


  sub slurp {
    my ($key, $dir) = shift;
    open my $h, '<', "$dir/$key" or return;
    local $/; <$h>                      # slurp it all
  sub exists { my ($key, $dir) = shift; return −f "$dir/$key" }
  tie %hash, 'Tie::Memoize', \&slurp, $directory, \&exists,
      { fake_file1 => $content1, fake_file2 => $content2 },
      { pretend_does_not_exists => 0, known_to_exist => 1 };

This example treats the slightly modified contents of $directory as a hash. The modifications are that the keys fake_file1 and fake_file2 fetch values $content1 and $content2, and pretend_does_not_exists will never be accessed. Additionally, the existence of known_to_exist is never checked (so if it does not exists when its content is needed, the user of %hash may be confused).


FIRSTKEY and NEXTKEY methods go through the keys which were already read, not all the possible keys of the hash.


Ilya Zakharevich mailto:perl−module−hash−memoize@ilyaz.org <mailto:perl-module-hash-memoize@ilyaz.org>.

More Linux Commands

ssh-agent(1) - authentication agent - Linux manual page.....
ssh-agent is a program to hold private keys used for public key authentication (RSA, DSA, ECDSA, ED25519). The idea is that ssh-agent is started in the beginnin

is_wintouched(3ncurses) - curses refresh control routines...
The touchwin and touchline routines throw away all optimization information about which parts of the window have been touched, by pretending that the entire win

vcut(1) - cuts Ogg Vorbis files (Commands - Linux man page)
vcut reads an Ogg Vorbis audio file and splits it at the given cutpoint, which is a sample number. If the cutpoint is prefixed with +, the cutpoint is an intege

CPANPLUS::Dist::Autobundle(3pm) - (unknown subject).........
CPANPLUS::Dist::Autobundle is a distribution class for installing installation snapshots as created by CPANPLUS autobundle command. All modules as mentioned in

pinky(1) - lightweight finger (Commands - Linux man page)...
-l produce long format output for the specified USERs -b omit the users home directory and shell in long format -h omit the users project file in long format -p

cdrdao(1) - reads and writes CDs in disc-at-once mode.......
cdrdao creates audio and data CD-Rs in disk-at-once (DAO) mode driven by a description file called toc-file. In DAO mode it is possible to create non standard t

srand48(3) - generate uniformly distributed pseudo-random nu
These functions generate pseudo-random numbers using the linear congruential algorithm and 48-bit integer arithmetic. The drand48() and erand48() functions retu

autofs(5) - Format of the automounter maps - Linux man page
The automounter maps are FILE, NIS, NISPLUS or LDAP maps referred to by the master map of the automounter (see auto.master(5)). These maps describe how file sys

git-apply(1) - Apply a patch to files and/or to the index...
Reads the supplied diff output (i.e. a patch) and applies it to files. With the --index option the patch is also applied to the index, and with the --cached opt

SDL_SetVideoMode(3) - Set up a video mode with the specified
Set up a video mode with the specified width, height and bits-per-pixel. If bpp is 0, it is treated as the current display bits per pixel. The flags parameter i

dracut.cmdline(7) dracut kernel command line options........
The root device used by the kernel is specified in the boot configuration file on the kernel command line, as always. The traditional root=/dev/sda1 style devic

thin_check(8) repair thin provisioning metadata on device or
thin_check checks thin provisioning metadata created by the device-mapper thin provisioning target on a device or file. OPTIONS -q, --quiet Suppress output mess

We can't live, work or learn in freedom unless the software we use is free.