> I am having some problems with hash keys that start with '_'
being
> treated as private variables and getting nothing back when
trying to
> access the associated value. I have a web interface that uses
hashes
> to contain various user input which can (and often does) have
keys
> starting with '_'.
>
> I've read the relevant bits in the badger book but I cannot find
any
> comments on a workaround. I have never needed this "private
variable"
> functionality so I am quite happy to turn it off completely
(with a
> patch to the source, if necessary). Anyone have suggestions as
to what
> I can do? I really do not want to have to avoid all usage of
hashes in
> my code (it would mean a lot of rewriting).
I think you know about the 'item' virual method. In some old
versions of TT it allows you to access such private fields:
[% myhash.item( "_some_private_value" ) %]
But fortunately ;) the bug was fixed. The easiest method to
achieve the "broken" behavior is to create custom virtual method.
Just add anything to $Template::Stash::HASH_OPS hash before
processing your template:
$Template::Stash::HASH_OPS->{anyitem} = sub {
my ($hash, $item) = @_;
$item = '' unless defined $item;
$hash->{ $item };
};
Now you can access any hash value using:
[% myhash.anyitem( "_some_private_value" ) %]
You may even redefine the original 'item' vmethod, assigning to
$Template::Stash::HASH_OPS->{item}.
NB! (mostly for Andy):
By the way - there is still a security hole for accessing private
hash fields within template: 'each' vmethod returns full content
of hash without any filtering. The same about 'keys', 'values' and
'list' vmethods. The following code dumps all values of 'myhash',
including "private" data:
[%
myhash_each = myhash.each;
WHILE myhash_each.size;
key = myhash_each.shift;
value = myhash_each.shift;
"$key = $value\n";
END;
%]
While it is not a serious bug for most purposes, it may have sence
in some environments, where web user can modify templates.
--
Sergey Martynoff
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates