What is a tied hash?

D

Dan Anderson

I'm just curious, what is the difference between a tied hash
and regular hash? I've seen the term come up quite often but never
quite knew what it meant. Does it have something to do with whether
you use a {} to create a reference to a hash or a list to create it,
and whether or not you need to use a ->?

Thanks in advance,

Dan
 
T

Tad McClellan

Dan Anderson said:
I'm just curious, what is the difference between a tied hash
and regular hash?


A regular hash does only regular things when it is accessed,
a tied hash allows you to do whatever you want each time
it is accessed (eg: read/write values in a disk file instead
of in RAM).

I've seen the term come up quite often but never
quite knew what it meant.


Then your first step should be to read the applicable docs:

perldoc -f tie

perldoc perltie

Does it have something to do with whether
you use a {} to create a reference to a hash or a list to create it,
and whether or not you need to use a ->?


No.
 
K

Keith Keller

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm just curious, what is the difference between a tied hash
and regular hash?

- From perldoc -f tie:
This function binds a variable to a package class
that will provide the implementation for the variable.

So instead of Perl handling the hash in the default manner, tie
tells Perl to handle it differently, where ''differently'' is
definied by the particular tie implementation.
Does it have something to do with whether
you use a {} to create a reference to a hash or a list to create it,
and whether or not you need to use a ->?

No.

Read

perldoc -f tie
perldoc Tie::Hash
perldoc perltie

for much more.

- --keith

- --
(e-mail address removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/vlIRhVcNCxZ5ID8RAptRAJ0Wc11ZavN1ibK8z6ayn50MnhFv7gCfdae/
0pxoYGtDt7OpmWcM3p8pJJQ=
=Ry7V
-----END PGP SIGNATURE-----
 
J

Juha Laiho

Dan Anderson said:
I'm just curious, what is the difference between a tied hash
and regular hash? I've seen the term come up quite often but never
quite knew what it meant. Does it have something to do with whether
you use a {} to create a reference to a hash or a list to create it,
and whether or not you need to use a ->?

"perldoc -f tie", "perldoc tie"

.... and to summarize. Tieing (tying?) lets you hide code behind something
that looks like a variable. And the code may then do "anything".

Commonly this would be used so that you have what you access as a hash,
but behind the scenes, all changes to the hash are stored into a file
(often into some kind of database format, where the db access is done
based on the hash keys).

It's not just hashes that can be tied, but also arrays, scalars and file
handles.
 
M

Martien Verbruggen

perldoc -f tie

has to do with keeping the hash sorted

Euhmm... Not really.

That could be one of its applications, but the tie mechanism in general
has nothing to do with sorting at all.

Martien
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top