Any Perl victims care to explain this?

Martijn van Oosterhout kleptog at
Thu Apr 4 19:36:25 EST 2002

On Thu, Apr 04, 2002 at 07:22:49PM +1000, Matthew Hawkins wrote:
> I've been tearing what's left of my hair out for two days trying to deal
> with a simple problem.
> 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'};

You can. The value is whatever you put in there.

> 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'}];
> }
> and pray to God that you don't encounter yet another random return type
> and have to add yet another conditional clause to deal with it.  Each
> time you need to use it.

Well, if you start including blessed objects, ref() can return anything. But
seriously, what are you using that doesn't strictly define what the hash

I'd do:

die "Who gave me this rubbish!!!" unless ref( $table->{'key'} ) eq "SCALAR";

> Why?  TIMTOWDI - there is more than one way to do it, and by God we're
> going to force you to use as many of them as possible.
> FWIW %table is tie'd to a MySQL database (using Tie::DBI) and all
> values are integers.
> Unlike sane languages, where you would have something like:
> int return_value_from_hash(int key) {
>         return data[key];
> }

In languages like that, the hash can only contain integers. If your hash
contains opaque types, then the above code won't work there either. Does
that language know how to convert any type to an integer?

> 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.

It's not random. If you only put integers in, you only get integers out. I
don't understand your problem.

> 2/3rds of this script is dealing with this completely brain-dead
> "feature", and reinforces my commitment to take out Larry Wall in
> preference to Bill Gates when I finish my time machine.  The bastard
> even thinks that this insanity is a good thing, as documented here:

I think your problem is something else, but if it's that much of a problem,
define a function to encapsulate it. If that's what it takes, which I doubt.

I think you are confused.
Martijn van Oosterhout <kleptog at>
> Ignorance continues to thrive when intelligent people choose to do
> nothing.  Speaking out against censorship and ignorance is the imperative
> of all intelligent people.

More information about the linux mailing list