Any Perl victims care to explain this?

Peter Barker pbarker at
Thu Apr 4 20:01:07 EST 2002

On Thu, 4 Apr 2002, Matthew Hawkins wrote:

> I've been tearing what's left of my hair out for two days trying to deal
> with a simple problem.

You do have "The Perl Book", don't you?

> Any sane programmer would like to enjoy such wonderful programming
> constructs as obtaining the value of a key in a hash table (okay, a hash
> table reference), like so:
> $value = $table->{'key'};

It does. What goes in, comes out.... _UNLESS_ you start fooling with tied
hashes. Tied hashes can return what they damn well please. The trick is to
design your tied hash so the user (that's you in this case) gets what they

> But thanks to Perl, which enjoys the full power of TIMTOWTDI, one must
> instead do:
> if (ref($table->{'key'}) eq "SCALAR") {
>         $value = ${$table->{'key'}};
> } elseif (ref($table->{'key'}) eq "ARRAYREF") {
>         $value = join(' ', @{$table->{'key'}});
> } else {
>         $value = [$table->{'key'}];
> }

This code block is nonsense. It basically comes to:
If VALUE is a reference to a scalar, set $value to the referenced scalar
If VALUE is a reference to an array, join the values in that array with
spaces and set $value to that
else set $value to be a reference to an array containing a single
element, VALUE

$value won't even be of a consistent type.

> FWIW %table is tie'd to a MySQL database (using Tie::DBI) and all
> values are integers.

Errr.... if that were the case, you wouldn't be having this problem. My
guess is that Tie::DBI is screwing you around.

> and the programmer can rest easy knowing that they will ALWAYS get an
> integer back, Perl likes to keep you on your toes and randomly selects
> the return type from the list of all possible data types.  Then, knowing
> that you've employed nasty secret ninja hacks like the above to work
> around it, will return completely different data types the next day,
> forcing you to expand the list to deal.

OK, nice rant.

> 2/3rds of this script is dealing with this completely brain-dead
> "feature", and reinforces my commitment to take out Larry Wall in

OK, not so good a rant. 2/3rds of the script is probably due to a lack of
understanding the tied hash you're trying to use. This is not a perl
problem - it's whoever wrote Tie::DBI.

I suggest you play around with hashes for a while OUTSIDE of Tie::DBI to
get a feel for them. Hints:

$foo{"BAR"} = 1;
print "1 = $foo{BAR}";

$foo{"BAR"} = [1, 'BAZ'];
print join ',', @{$FOO{"BAR"}};

$foo{"BAR"} = [1, { BAZ => { BLURGH => "Fred" } } ];
print "Complicated";

Oh yeah.... you should not need to use "ref" with Tie::DBI, if it's at all
sanely designed. I work with databases and perl all day, and I don't use
the tied stuff; not explicit enough for me.

Peter Barker                          |   N    _--_|\ /---- Barham, Vic 
Programmer,Sysadmin,Geek              | W + E /     /\                
pbarker at         |   S   \_,--?_*<-- Canberra      
You need a bigger hammer.             |             v    [35S, 149E]   
qq%I've never heard of it or used it, apart from finding it in the Camel
 book and saying "Oh god".% -- Onceler

More information about the linux mailing list