jatill has asked for the wisdom of the Perl Monks concerning the following question:

I have a class DBI object called 'person' that I am passing into a template toolkit template.

So the following would work fine:

I am [% person.name %]
If I write the following statement, though, I am accidentally updating the person object in the database. I get a warning because I am updating the person record without doing a commit:
[% person.name = person.name _ ' PHD' %]
DB::Person DB::Person=HASH(0xe57cb0) destroyed with out saving changes to name at...

Is there a way I can update the person.name variable without updating the database record?

CODE tags added by Arunbear

Replies are listed 'Best First'.
Re: Template / Class DBI question
by cees (Curate) on Aug 11, 2005 at 19:40 UTC

    You need to use a temporary variable if you want to do that.

    [% SET proper_name = person.name _ ' PHD' %]

    Or if you really wanted to, you could probably define a TEMP column in your Class::DBI table, which would allow you to do something like this (untested):

    [% person.proper_name = person.name _ ' PHD' %]

    Either way, I think this type of code really doesn't belong in the template though. Unless you have no other option, you should keep this out of the template to leave your templates as simple as possible.

    If you use the TEMP column trick in Class::DBI then just predefine the proper name in your code somewhere, and then just use [% person.proper_name %] directly in your template.

Re: Template / Class DBI question
by shiza (Hermit) on Aug 11, 2005 at 19:41 UTC
    Yes, I think it only updates the database if you call the update method on an object. Unless you have autoupdate turned on.

    Autoupdating is off by default. See Class::DBI for more info. discard_changes is another method that will be of use to you.