Template::Extract is a lit bit cooler

brian_d_foy on 2005-07-15T01:48:08

I wanted to use Template::Extract today, but found it couldn't handle my template. It turned out that by changing the options to the Template::Parser object that Template::Extract::Compile uses I could get the behaviour I needed. I modified the module so I could pass in my own Template::Parser object.

Thank god it was that simple, because I didn't want to mess with those regexen.

So, now Template::Extract has a patch waiting in RT.


There's a Bug!

chromatic on 2005-07-15T02:08:27

Calling UNIVERSAL::isa() is almost always wrong.

Re:There's a Bug!

brian_d_foy on 2005-07-15T02:19:02

It's done elsewhere in the module, so if Autrijus decides he doesn't want to do it like that, he can change it.

Re:There's a Bug!

nothingmuch on 2005-07-15T21:58:35

When I wrote UNIVERSAL::isa autrijus was right next to me helping me pitch ideas...

I said that it stupid people do UNIVERSAL::isa, and after a while I said it again, and on the third time or so he implied that he doesn't like being called stupid... ;-)

Either way, he said he'll use the isa function exported from UNIVERSAL::isa directly so that he doesn't need to call Scalar::Util::blessed.

Since I exposed this deep dark secret, now you both have to do it, or else.

Re:There's a Bug!

brian_d_foy on 2005-07-15T22:26:15

Besides Test::MockObject breaking, why else do you hate UNIVERSAL?

I don't really like using subsets of Perl only because somebody's black magic mojo can't deal with it (and even more so when I don't want to use that mojo anyway ;)

Re:There's a Bug!

nothingmuch on 2005-07-15T22:36:28

Well, for me Test::MockObject is reason enough... Either way, since ->isa is OOP, and oop is about encapsulation, it should be encapsulated, IMHO.

Polymorphism is all about an object adhering to some interface. If it can do that, it's consumers should be happy.

If an object claims it is of some class, then it's impolite (and black magic mojoish, in a way) to interfere with that.

Re:There's a Bug!

brian_d_foy on 2005-07-15T23:01:34

Perl is not a object language, and there are a lot of things you can't call methods on. Since any of those things might show up at any time, you can't simply use a method call to check to see if it is the right sort of object if you don't have an object. You can't impose object rules on everything in Perl.

The function form of isa() exists to handle things that aren't objects. No matter what we think the world should be like, we know that users do all sorts of wacky things.

I'm not going code a certain way simply because Test::MockObject needs me too. Testing modules are nice but I don't think their black magic should set project requirements. If you want to do fancy things like that, it's up to you, not the coder, to handle those cases. You can't take part of Perl away just because a module can't deal with it, especially since people may not even want that module.

Besides all that, surely there is some other clever solution. Apparently, UNIVERSAL::isa (the module) gets around the problem. Can Test::MockObject handle that itself? And if it can, what's the problem?

Re:There's a Bug!

nothingmuch on 2005-07-15T23:17:33

I'm not arguing that perl is an object language, I'm arguing that in the OOP subset of perl you want objects to play nice, and this includes not calling

        Some::Class::method($object);

instead of

      $object->method;

because $object might be a subclass..

UNIVERSAL::isa is a method that knows to double as a function, but it's still a method..

UNIVERSAL::isa (the module) replaces UNIVERSAL::isa (the subroutine), so no, Test::MockObject can't do the magic on it's own. That's the whole issue - it never gets control, because the caller is taking control away from it.

The function form of isa is useful for doing things like UNIVERSAL::isa($thing, "ARRAY") or whatever, and it won't die when it's being fed crap, that's true, but if you are going to check for a class, then you're probably going to call methods on it - are you going to call all of them as functions and expect that they will magically work even though the thing you got was undef? Params::Validate is there to solve that problem.

Re:There's a Bug!

brian_d_foy on 2005-07-16T00:47:47

Params::Validate solves the problem that UNIVERSAL::isa() already does? No thanks. Note that perlobj and UNIVERSAL specifically mention the use of isa() the function for testing inheritance. It's why isa() exists. Indeed, the can( OBJECT, METHOD ) syntax makes no sense if this wasn't the intent.

We'll just have to disagree on this one, and you'll just have to include me with the other stupid module authors. If I have to use Test::MockObject for something, I'll just make the fix to it myself.

Re:There's a Bug!

autrijus on 2005-07-16T04:43:05

I choose "or else". Look, my UNIVERSAL::isa call in Template::Extract is testing whether it's a scalar reference:
$var = $$var if UNIVERSAL::isa($var, 'SCALAR');
I think this use is legit, especially as I suspect people who actually overload ${} do not usually bother redefining their isa method to include SCALAR.

Thanks, see 0.39.

autrijus on 2005-07-15T06:00:20

I end up choosing another API. Easy things should be easy -- i.e. directly passing parser options into Template::Extract->new() -- and hard things should be possible -- namely subclassing T::E to use another parser subclass instead of relying on "isa" relationship. Thanks for the suggestion!

Re:Thanks, see 0.39.

brian_d_foy on 2005-07-15T06:14:31

Great! Thanks.

I'm not so concerned about the API as long as I can play with the parser. :)