[Templates] Proposed changes to list.hash and hash.list vmethods
Andy Wardley
abw@wardley.org
Fri, 27 Jan 2006 12:54:32 +0000
I just got bitten (again) by a limitation in Template::Stash. Stephen
Howard provided a patch to fix it and I added it, but left it commented
out pending comments from anyone (and also pending the tuits to dive
into the XS Stash to make the same fix).
http://template-toolkit.org/pipermail/templates/2003-December/005417.html
The essence of the problem is this: if you have an object (list or hash)
which doesn't implement the method that you call, then it should fall
back on hash/list vmethods, or accessing the numerical item you asked
for in a list.
[% hashobject.keys %] # should call hash vmethod, but doesn't
[% listobject.first %] # ditto list vmethod
[% listobject.3 %] # should return $listobject->[3]
Needless to say, the problem is that it doesn't, but with Stephen's
patch, it does (in the Perl Template::Stash at least).
Well we don't seem to have had any comments since then, and with fresh
bite marks to show for it, I'm going ahead and activating it unless I
hear any blood-curdling screams to the contrary.
I'll also have a look at updating the XS Stash to do the same thing
so you may be hearing a few blood-curdling screams from me :-)
While I'm there, I'm also planning to "fix" the hash.list and list.hash
methods to Do The Right Thing.
For some totally half-baked reason that now I can fathom not, I made
the hash.list method return a stupidly complex list of hash references:
{ pi => 3.14 } => [ { key ='pi', value='3.14' } ]
and the list.hash method return an equally useless hash:
[ 3.14, 2.718 ] => { 0 => 3.14, 1 => 2.718 }
Having just activated Stephen's patch, these vmethods kicked in and
made a handful of tests failed because they were expecting some kind
of sensible values returned and instead got this crack-fuelled nonsense.
So I'm proposing that I fix them too and make them do what they should
always have done - a straight conversion between list and hash
hash.list => [ %$hash ]
list.hash => { @$list }
I realise that it might break code that relies on the old behaviour,
but I think it's for the best in the long run.
Any objections?
A