Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

2shortplanks (968)

2shortplanks
  (email not shown publicly)
http://2shortplanks.com/
AOL IM: trelane2sp (Add Buddy, Send Message)
Yahoo! ID: trelane2sp (Add User, Send Message)

Mark Fowler has never been the same since he was elected leader of the London Perl Mongers. The strain manifests itself mainly in releasing various [cpan.org] modules [cpan.org] to CPAN, giving talks [2shortplanks.com], and use of the Trelane nick on #london.pm for endless procrastination. Doctors are still seeking a cure.
Tuesday January 28, 2003
01:41 PM

use overload "brain" => "to_mush";

[ #10241 ]
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.

Now like all good people should, I don't use the Template module directly, but have an wrapper class that does all the nastiness of locating templates for me and dealing with errors. If the template goes wrong, my template function dies with the error.

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • "$@" eq ""
    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.
    • 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

      -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.

      I have no idea how that got changed like that - it doesn't seem something that w