Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Forcing context of dot operator in Template Toolkit

by agianni (Hermit)
on Oct 24, 2007 at 14:44 UTC ( #646900=perlquestion: print w/ replies, xml ) Need Help??
agianni has asked for the wisdom of the Perl Monks concerning the following question:

In our Web application architecture, I'm changing a component to use objects where data structures were previously used. For backwards compatibility, I'm using Contextual::Return, which works nicely until I get to the templates. We're using Template Toolkit for our presentation layer and I'm running into trouble when the data in question comes into play. I have a method in my controller logic that uses Contextual::Return to return either a hashref or an object depending on context. Unfortunately, it seems that Template Toolkit always tries to evaluate the results of the method call as an object first, so I can never seem to access the hash.

My understanding is that Template Toolkit attempts to evaluate the return value of the dot operator in order, and I'm assuming that it tries the object first, which Contextual::Return happily provides.

The upshot is that I would like to be able to do this:

foo.get_thing # the get_thing method
or
foo.bar.baz.thing # the bar => { baz => 'thing' } data structure

But the second example gives me an error along the lines of:

undef error - Can't call method 'bar' on OBJREF value returned by My::Module

Any suggestions?

perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

Comment on Forcing context of dot operator in Template Toolkit
Select or Download Code
Re: Forcing context of dot operator in Template Toolkit
by merlyn (Sage) on Oct 24, 2007 at 15:38 UTC
    Contextual::Return does one kind of magic. Template Toolkit is a different kind of magic. The magic overlaps. The magic goes Boom, and fails.

    Seriously, the problem is that C::R returns a highly overloaded singleton object to "sense" the right context, which fails in TT because TT has its own simple probes for "is this an object? Yes, call a method. No, well is it a hash ref then?" and so on.

    You're unlikely to get them to play together. You'll need to build a Perl shim that you can insert between the thing that returns a C::R object and that TT can actually call. Sorry about that.

      'kay, that's what I thought. Reminds me of a little experiment I did a while ago, letting Eliza talk to herself:
      Eliza1: Hi. I'm Eliza. Tell me your problems! Eliza2: How long are you Eliza Tell you my problems? Eliza1: Why are you so interested wether I am Eliza Tell me your probl +ems or not? Eliza2: Why are you so interested wether I am so interested wether you + are Eliza Tell you my problems or not or not? Eliza1: Why do you think that I am Eliza Tell me your problems or not +or not? Eliza2: You say that you are Eliza Tell you my problems or not or not +- do you come therefore? Eliza1: Do you believe that I am Eliza Tell me your problems or not or + not - do me come therefore? Eliza2: You think that you are Eliza Tell you my problems or not or no +t - do you come therefore - Is this normal? Eliza1: Perhaps you would like to be Eliza Tell me your problems or no +t or not - do me come therefore - Is this normal? Eliza2: I think that you aren't sure. Eliza1: Do you really think so? Eliza2: What does this mean for you, saying " Do me really think so "? Eliza1: Why do you ask? Eliza2: Is this question interesting for you? Eliza1: Perhaps you would like to talk about a problem? Eliza2: Why are you so unsteadily? Eliza1: Would you prefer it, if i were so unsteadily? Eliza2: I understand!

      You understand!?!?!

      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
Re: Forcing context of dot operator in Template Toolkit
by agianni (Hermit) on Oct 26, 2007 at 19:11 UTC

    Hey, so it turns out I got this working after all. There was a bug in my test that was forcing the object context. I missed the fact that the original author of the test had overridden my method to explicitly return an object all of the time :)

    This is to say that now the template always pulls back the hashref instead of the object from Contextual::Return. This works fine for me since it is backwards compatibility from a hashref model that I'm looking for. So in the mean time, I have bi-directional flexibility in my controller code and access to hashrefs in my templates. I have accounted for this by creating entries in the hashref with names identical to the method accessors, so I can call:

    foo.bar.baz

    or

    foo.get_baz

    which both give me the same value, and while the later looks like a method call, it's actually just a hash entry. It's a total kludge, but that's all I'm after right now.

    perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://646900]
Approved by Corion
Front-paged by northwind
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (16)
As of 2014-08-29 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (282 votes), past polls