Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: How can I unbless an object?

by mp (Deacon)
on Jul 19, 2002 at 18:22 UTC ( [id://183356]=note: print w/replies, xml ) Need Help??


in reply to Re: How can I unbless an object?
in thread How can I unbless an object?

The intended use is to unbless deeply nested objects for use with Template Toolkit (TT2) so I can add/modify/remove fields in the hash before template processing.

If you pass TT2 an object that is a hash and tell it to access object.element_name, it will return

$object->element_name

if that method exists rather than

$object->{element_name}.

If I could unbless the object, then I could force it to use the latter. There are other workarounds, but if "unbless" existed in some form, it might be simpler.

Replies are listed 'Best First'.
•Re: Re: Re: How can I unbless an object?
by merlyn (Sage) on Jul 19, 2002 at 18:38 UTC
    Ahh, yes. Since I'm so enamoured with Template, it's hard to scream "too much DWIMmery" there. But in this sense, yes, there's a mismatch between that DWIMish design, and what you actually want to hand to it.

    Do you really need two-way communication? Can you not just hand it a copy?

    If you do need two-way communication, perhaps you could hand it a tie'd hash reference, and then map that back to your object.

    -- Randal L. Schwartz, Perl hacker

      I don't need 2-way communication, so a copy works fine and is conceptually cleaner. But it seems wasteful, since handing off the object to the TT2 process method is the very last thing done with the copy and the original object before they both go out of scope and disappear.
Re: Re: Re: How can I unbless an object?
by perrin (Chancellor) on Jul 19, 2002 at 20:33 UTC
    I think you are overly worried about the overhead of method calls. However, if you really want do this I recommend implementing a method on the object that returns an unblessed version of itself appropriate for use with TT. This gives you an opportunity to later do some extra magic in this method, like rearranging the data to make for a simpler template.
Re: How can I unbless an object?
by mp (Deacon) on Nov 12, 2003 at 15:49 UTC
    In answer to my own question, I just discovered a recent CPAN module that can unbless an object. See Data::Structure::Util.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://183356]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found