On 2012.5.13 11:36 AM, Peter Corlett wrote:
> I'd like to throw in the fun breakage caused by the combination of adding
> two unnecessary bits of syntactic sugar to Perl. Somebody decided that
> auto-deref would be nice, so you can do "each $hashref" and "pop $arrayref".
> And then somebody else clearly huffed a bit too much PHP and decided that
> hash operations should also work on arrays where possible, even if it
> doesn't make sense.[0] Quick now, what does "each $foo" do, buried 94 levels
> deep in the call stack of your Enterprise Quality Software?

You have the history of that backwards.

Hash operations working on arrays are the unfortunate, ultimate evolution of
an unfortunate failed performance hack called pseudo-hashes where arrays were
made to act like hashes.  This was done years and years ago.

This was back in the day when people were writing things like this to shave
off a little memory and CPU time.

    use constant KEY1 => 0;
    use constant KEY2 => 1;
    use constant KEY3 => 2;

    $obj->[KEY1] = $value;

Because array access was perceived to be faster and use less memory than
hashes.  It was decided to make this a language feature!  So you could write...

    my $struct = [{foo => 1, bar => 2}, "FOO", "BAR"];

    # Equivalent to $struct->[ $struct->[0]{foo} ]
    print $struct->{foo};    # prints "FOO"

But for some very special situations where Perl could do some compile time
optimizations that nobody took advantage of, this was actually SLOWER than a
normal hash because now it required two array look ups and a lot of extra code
and Perl hashes were already pretty fast.  Worse, because all the hash ops had
to check for a psuedo-hash it slowed all hashes down by about 15%.  Whoops.

None of this micro-optimizing really matters because you're going to put an
accessor method around it anyway and method calls are relatively expensive.
But Perl developers didn't really grok OO then.

Nobody noticed that until it was too late and large efforts were made to make
psuedo-hashes work well (a lot of them by a young, naive programmer called
me).  This included enhancing arrays so that all the hash operations were
supported.  Unfortunately, somebody noticed that arrays support all the hash
operations anyway... why not expose it at the language level?  For some reason
that person was not immediately stoned.

Psuedo-hashes were removed, with prejudice, but the hash/array operations were
not. :-(


> And on the subject of Perl and hashes:
> 
> * iterators not general-purpose and only available on hashes
>   * and you can only have one per hash

So much hate for tying the iterator to the data and not the op.


-- 
If you want the truth to stand clear before you, never be for or against.
The struggle between "for" and "against" is the mind's worst disease.
    -- Sent-ts'an

Reply via email to