Today I have been mainly fiddling with overloaded stuff. The Template
Toolkit's exception class Template::Exception is
overloaded, meaning that if you try and treat it in string context
then it returns the text of the error, but you're still able to do things like
UNIVERSAL::isa($exception,"Template::Exception")
to work
out if the error you're looking at came from TT or not.
So I was using Test::Exception to check I wasn't getting any exceptions back when I was running some code that used the template toolkit like so:
my $output; lives_ok { $output = template("mytemplate.tmpl", $params) } "runs okay"; is($ouput, $expected_output, "correct output back");The problem is that under the hood Test::Exception it was doing the equivalent of:
$@ eq ""Which was causing everything to go wrong, as Template::Exception can stringify but it doesn't have comparison operations (like
eq
, ne
, etc) defined.
One quick patch to Template::Exception later to have
the fallback => 1
option passed to
overload
in order to tell it to simply convert to string
for any operator that it doesn't understand and do the normal thing,
and everything's working fine.
Of course this required upgrading all the Template Toolkit
installations we have with the CVS version of the Template Toolkit
distribution (TT isn't released to CPAN without doing a whole cycle of
developer testing releases first) in order to run the tests for the module I was writing. What I wanted instead for
Template::Exception to work with existing modules that can only
stringify, for working with TT and all the other exception modules
that have the same problem. So I patched that (and it's tests) too,
so that when it's about to treat $@
as a string it uses
"$@"
instead.
This was a lot harder as coming up with the failing test cases made my head hurt a little, but I'm glad I did as there was one or two odd cases that I probably wouldn't have spotted otherwise.
So, after all this, if nothing else, I think my brain's overloaded. I think I might go home and watch some Stargate.
I would think that this does stringify before even attempting to do a comparison, so any lack of overloaded comparison operators wouldn't even come into play."$@" eq ""
Re:Couldn't you do...
2shortplanks on 2003-01-29T09:41:58
Yes, that's essentially what I patched Template::Exception to do.That would have been clearer if somewhere between my editor, my browser, and a couple of trips though preview we hadn't lost the sentance in the journal that read
I have no idea how that got changed like that - it doesn't seem something that was likely to happen, but it did.-So I $@ as a string it uses "$@" instead.
+So I patched that (and it's tests) too, so that when it's about to treat <code>$@</code> as a string it uses
<code>"$@"</code> instead.