Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: DBD::mysql Unusual Behavior

by tadman (Prior)
on Aug 23, 2002 at 17:40 UTC ( #192388=note: print w/replies, xml ) Need Help??

in reply to Re: DBD::mysql Unusual Behavior
in thread DBD::mysql Unusual Behavior

The correct way to check for the presence of a key is to use exists. You'll note, though, that these keys exist since they're from the keys call in the foreach. The concern is that the value will be undefined which is going to generate warnings if used.

Now regarding this so-called autovivification. People get so paranoid about that sort of thing that they don their tin-foil hats at the merest hint of trouble. It's a lot harder to do than you think. Merely thinking of a key is not enough:
use Data::Dumper; my $foo = {}; 1 if (defined($foo->{bar})); print Dumper($foo); 1 if (defined($foo->{bar}->{baz})); print Dumper($foo);
The second one causes autovivification since you're actually extending some pseudo-predefined structure. You can't dereference an undefined value, so it just makes one up for you.

Replies are listed 'Best First'.
Re: Re^2: DBD::mysql Unusual Behavior
by bart (Canon) on Aug 23, 2002 at 20:13 UTC
    The concern is that the value will be undefined which is going to generate warnings if used.

    Eh, no. Not in this particular case, anyway. The thing is that DBI's placeholder mechanism will convert every undef into the unquoted string "NULL", so that

    will effectively mean

    And some people's database setup will complain about that, if the field isn't allowed to be NULL. Personally, I like NULLs in my databases, for values that indeed have no value.

      The NULL fields actually consume an astounding extra bit per record, so while they're convenient and all, if the difference between "empty" and NULL is trivial, it's often easier to just throw in an empty value.

      Since the column in question was not allowed to be NULL, you're required to check before INSERTion.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-10-21 20:14 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (223 votes). Check out past polls.