Hash::Utils lock_keys

  • Thread starter Johannes Fürnkranz
  • Start date
J

Johannes Fürnkranz

Hi,

I don't have the 3rd edition of the Camel-book, so pardon me if this is
covered there:

Can anybody explain the purpose of Hash::Util::lock_keys to me?

I would have expected that this does something like internally convert
the hash to an array and/or provide faster access to the hash elements
by somehow avoiding to compute the hashing function for each element. I
tested this with Benchmark, but I could not find any noticable
difference between retrieving a key from a locked hash or a regular
hash. The only difference I note is, of course, the error message that I
get when I try to access an undefined slot of the locked hash. Is that all?

thx, Juffi
 
J

Jay Tilton

=?ISO-8859-1?Q?Johannes_F=FCrnkranz?=

: I don't have the 3rd edition of the Camel-book, so pardon me if this is
: covered there:

If you did have a camel, where would you look for documentation of
modules that it does not cover?

Most of the competently authored modules carry their own
documentation, accessible through perldoc.

perldoc Hash::Util

: Can anybody explain the purpose of Hash::Util::lock_keys to me?

It restricts the hash's set of keys.

: I would have expected that this does something like internally convert
: the hash to an array and/or provide faster access to the hash elements
: by somehow avoiding to compute the hashing function for each element.

A non-hashing hash? Far out!

Looking at the Hash::Util code reveals the more mundane truth.
lock_keys() just invokes a routine inside perl to mark the hash as
read-only.
 
J

Johannes Fürnkranz

Jay said:
=?ISO-8859-1?Q?Johannes_F=FCrnkranz?=

: I don't have the 3rd edition of the Camel-book, so pardon me if this is
: covered there:

If you did have a camel, where would you look for documentation of
modules that it does not cover?

I looked through Hash::Util, of course. Sorry if this was not obvious
from my posting. But I find the assumption that sbdy posts a question on
a module w/o looking at perldoc a bit presumptious.
I think this was clearly not a newbie question.
Most of the competently authored modules carry their own
documentation, accessible through perldoc.

perldoc Hash::Util

: Can anybody explain the purpose of Hash::Util::lock_keys to me?

It restricts the hash's set of keys.

: I would have expected that this does something like internally convert
: the hash to an array and/or provide faster access to the hash elements
: by somehow avoiding to compute the hashing function for each element.

A non-hashing hash? Far out!

Looking at the Hash::Util code reveals the more mundane truth.
lock_keys() just invokes a routine inside perl to mark the hash as
read-only.

Yes, but Hash::Util also tells me that the hash-lock "is intended to
largely replace the deprecated pseudo-hashes."

Now, if I remember correctly, these pseudo-hashes were meant to combine
the better of arrays (smaller and faster) and hashes (can take any
keys). In some sense they were "non-hashing hashes", because they were
actually arrays.

So if a locked hash is only a regular hash with the entries marked
read-only, I don't really see in what way that gives you more mileage.
So I was wondering how they are implemented and whether they buy me
anything beyond an error message for new keys.

And Hash::Util doesn't really answer these questions (at least not for me).

Juffi
 
J

Johannes Fürnkranz

Jay said:
: Now, if I remember correctly, these pseudo-hashes were meant to combine
: the better of arrays (smaller and faster) and hashes (can take any
: keys).

I never was able to reconcile the "smaller and faster" claim with
expectation. Seems they would be neither smaller, since there's a
hash taking up space alongside the array elements, nor faster, since
using their hash nature requires Perl to look up the array index in a
hash then look up the array element.

I never quite got that either. I think it only worked with 'use fields'
to get some compile-time optimization, but it was awkward in any case.
No matter. Even if pseudohashes do provide speed/storage benefits,
Hash::Util doesn't.

I see. Too bad.

Thank you!

Juffi
 
B

Bob Walton

Jay Tilton wrote:

....
lock_keys() just invokes a routine inside perl to mark the hash as
read-only.


Well, almost -- the values assigned to existing keys may be modified.
Perhaps the correct statement would that the keys themselves are read-only.
 
T

Tassilo v. Parseval

Also sprach Bob Walton:
Jay Tilton wrote:

...


Well, almost -- the values assigned to existing keys may be modified.
Perhaps the correct statement would that the keys themselves are read-only.

That happens automatically. When setting the SVf_READONLY flag on a hash
you get exactly this behaviour. To mark the values as read-only you'd
have to walk through the hash and do a SvREADONLY_on on each of them. I
remember once wanting to make a nested hash fully read-only (that is:
including all the values recursively) and I was slightly taken aback
that it required so much work.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top