Slash Boxes
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 ]

jonasbn (1153)

  reversethis-{gro.napc} {ta} {nbsanoj}
AOL IM: BJonasN (Add Buddy, Send Message)

Perl Programmer located in Copenhagen, Denmark. Active member of Copenhagen Perl Mongers.

Author of:

  • Business::DK::CPR
  • Business::DK::CVR
  • Business::DK::PO
  • Business::OnlinePayment::CashCow
  • Date::Holidays
  • Date::Holidays::Abstract
  • Date::Holidays::Super
  • Date::Pregnancy
  • Games::Bingo
  • Games::Bingo::Bot
  • Games::Bingo::Print
  • Module::Info::File
  • Module::Template::Setup
  • Test::Timer

and maintainer of:

  • Tie::Tools
  • XML::Conf
  • Workflow

Journal of jonasbn (1153)

Thursday November 16, 2006
07:13 AM

Note to self on stringification

[ #31624 ]

I had serious problem with some of my own code the other day.

I am using a DBIx::Class relative to access a database for writing.

So I am building a record structure (not a database record) containing some objects referencing to other objects representing database records and I only want to serialize this new record to the database if everything is ok... so I check 3 points in my shallow record to see if these (foreign) keys are present.

} elsif ($record{'operator'} && $record{'country'} && $record{'currency'}) {

And this just did not work, I was puzzled, I dumped the $record and the data was there. I printed out the three values and I could only see the two, even more puzzled...

The solution was quite obvious when discovered, but until that it was quite hard to figure out (as it often is).

Well the problem was that referring to the objects in that particular way would result in a stringification so the names where returned, for the currency no name was however specified resulting in an empty string, meaning false, so the better to write the above is:

} elsif ($record{'operator'}->id && $record{'country'}->id && $record{'currency'}->id) {

Lessons learned:

1. watch out in the case of stringification if the value used for the stringification is not default at least something or is defined not to be able to be empty
2. when asserting, assert on something you know will be set like the key or id

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.