Template::Plugin::DBM::Deep - Template Toolkit plugin for DBM::Deep


Template-Plugin-DBM-Deep documentation Contained in the Template-Plugin-DBM-Deep distribution.

Index


Code Index:

NAME

Top

Template::Plugin::DBM::Deep - Template Toolkit plugin for DBM::Deep

SYNOPSIS

Top

  [% USE db = DBM.Deep(file = "my.db" locking = 1 autoflush = 1);
     db.lock;
     db.flintstones = { "flintstone" = ["fred" "wilma"]
                        "rubble" = ["barney" "betty"] };
     db.castaways = ["gilligan" "skipper" "professor" "and the rest" ];
     db.unlock;
  -%]
  ...
  [% db.flintstones.rubble.0; %] -- barney
  [% db.castaways.3; %] -- and the rest

DESCRIPTION

Top

This module permits the direct use of DBM::Deep, a persistent sharable store for nearly arbitrarily nested hashes and arrays, within Template Toolkit code.

The initial constructor returns the top-level DBM::Deep object. All keyword arguments are passed to the DBM::Deep constructor as a flat list. Any single positional argument is presumed to be the file keyword parameter, permitting simple invocations like:

  USE db = DBM.deep("my.db");

All appropriate method calls described in OO Interface in DBM::Deep are available. In addition, hash-like objects can use direct keys to get at values:

  db.foo = "bar";
  db.foo; # gets "bar"

And array-like objects can use direct indicies:

  db.mylist = ["a" "b" "c"];
  db.mylist.2; # gets "c"

Caution: VMethods do not work against the hash-like or array-like objects. However, you can use the export method to get an unattached cloned copy of that portion of the database, and then the normal vmethods work:

  "$key\n" FOR key IN db.export.keys;

Failure to export first will result in attempting to access a hash element called keys, which doesn't exist.

There are probably other limitations. If you find other weirdness, let me know so I can update this document.

METHODS

Top

new

Used by Plugin interface.

dbm_deep_class

Used by Plugin interface.

SEE ALSO

Top

DBM::Deep

AUTHOR

Top

Randal L. Schwartz, <merlyn@stonehenge.com>

COPYRIGHT AND LICENSE

Top


Template-Plugin-DBM-Deep documentation Contained in the Template-Plugin-DBM-Deep distribution.

package Template::Plugin::DBM::Deep;

use 5.006;
use strict;
use warnings;

use base qw(Template::Plugin);

our $VERSION = '0.02';

sub new {
  my $class = shift;
  my $context = shift;		# passed by Template's USE

  my %ARGS;
  ## do we have any named args?
  if (ref $_[-1] and ref $_[-1] eq "HASH") {
    %ARGS = %{+pop};
  };
  ## do we have any positional args?
  if (@_) {
    $ARGS{"file"} = shift;
  }
  ## do we have too many?
  if (@_) {
    die "extra arguments to $class: @_";
  }

  die "$class: missing db file" unless $ARGS{"file"};

  ## now create the DBM::Deep object and return it:
  return $class->dbm_deep_class->new(%ARGS);
}

## lazy load DBM::Deep, and also return constant
## this permits overriding of the class
sub dbm_deep_class { require DBM::Deep; "DBM::Deep" }

1;
__END__