File: tiedhash.t

package info (click to toggle)
libtemplate-perl 2.14-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 5,496 kB
  • ctags: 667
  • sloc: perl: 15,349; makefile: 62; xml: 7; sh: 5
file content (175 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#============================================================= -*-perl-*-
#
# t/tiedhash.t
#
# Template script testing variable via a tied hash.
#
# Written by Andy Wardley <abw@kfs.org>
#
# Copyright (C) 1996-2001 Andy Wardley.  All Rights Reserved.
# Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Id: tiedhash.t,v 2.4 2002/08/12 11:07:17 abw Exp $
#
#========================================================================

use strict;
use lib qw( blib/lib blib/arch lib ../blib/lib ../blib/arch ../lib );
use Template::Test;
use Template::Stash;
$^W = 1;

eval {
    require Template::Stash::XS;
};
if ($@) {
    warn $@;
    skip_all('cannot load Template::Stash::XS');
}

#print "stash: $Template::Config::STASH\n";
#$Template::Config::STASH = 'Template::Stash::XS';

#------------------------------------------------------------------------
package My::Tied::Hash;
use base qw( Tie::Hash );
use vars qw( $AUTOLOAD );

sub new {
    my ($class, $meths) = @_;
    my %hash;
    tie %hash, $class, $meths;
    return \%hash;
}

sub TIEHASH {
    my ($class, $meths) = @_;
    bless $meths, $class;
}

sub FETCH {
    my ($self, $key) = @_;
    my $action = $self->{ FETCH } || return undef;
    &$action($key);
}

sub STORE {
    my ($self, $key, $value) = @_;
    my $action = $self->{ STORE } || return undef;
    &$action($key, $value);
}

# sub DELETE   { }
# sub CLEAR    { }
# sub EXISTS   { }
# sub FIRSTKEY { }
# sub NEXTKEY  { }

sub AUTOLOAD {
    my $self = shift;
    my $item = $AUTOLOAD;
    $item =~ s/.*:://;
    return if $item eq 'DESTROY';
    my $action = $self->{ $item } || return undef;
    &$action(@_);
}

#------------------------------------------------------------------------
package main;

my $DEBUG = grep(/-d/, @ARGV);
my $data = callsign();
$data->{ zero } = 0;
$data->{ one  } = 1;

my $hash = My::Tied::Hash->new({
    FETCH => sub { my $key = shift; 
		   print "FETCH($key)\n" if $DEBUG;
		   $data->{ $key };
	       },
    STORE => sub { my ($key, $val) = @_; 
		   print "STORE($key, $val)\n" if $DEBUG;
		   $data->{ $key } = $val;
	       },
});

#------------------------------------------------------------------------

my $stash_perl = Template::Stash->new({ hash => $hash });
my $stash_xs   = Template::Stash::XS->new({ hash => $hash });
my $tt = [
    perl => Template->new( STASH => $stash_perl ),
    xs   => Template->new( STASH => $stash_xs ),
];
test_expect(\*DATA, $tt);

__DATA__
-- test --
[% hash.a %]
-- expect --
alpha

-- test --
[% hash.b %]
-- expect --
bravo

-- test --
ready
set:[% hash.c = 'cosmos' %]
go:[% hash.c %]
-- expect --
ready
set:
go:cosmos

-- test --
[% hash.foo.bar = 'one' -%]
[% hash.foo.bar %]
-- expect --
one

-- test --
-- use xs --
[% hash.a %]
-- expect --
alpha

-- test --
[% hash.b %]
-- expect --
bravo

-- test --
ready
set:[% hash.c = 'crazy' %]
go:[% hash.c %]
-- expect --
ready
set:
go:crazy

-- test --
[% hash.wiz = 'woz' -%]
[% hash.wiz %]
-- expect --
woz

-- test --
[% DEFAULT hash.zero = 'nothing';
   hash.zero
%]
-- expect --
nothing

-- test --
[% DEFAULT hash.one = 'solitude';
   hash.one
%]
-- expect --
1