What is a tied hash?

Discussion in 'Perl Misc' started by Dan Anderson, Nov 21, 2003.

  1. Dan Anderson

    Dan Anderson Guest

    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
    Dan Anderson, Nov 21, 2003
    #1
    1. Advertising

  2. Dan Anderson <> wrote:
    >
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 21, 2003
    #2
    1. Advertising

  3. Dan Anderson

    Keith Keller Guest

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

    On 2003-11-21, Dan Anderson <> wrote:
    >
    > 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

    - --
    -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-----
    Keith Keller, Nov 21, 2003
    #3
  4. Dan Anderson

    Juha Laiho Guest

    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.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho, Nov 21, 2003
    #4
  5. On Sat, 22 Nov 2003 06:06:43 +1100,
    Edo <> wrote:
    > Dan Anderson wrote:
    >> 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

    >
    > 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
    --
    |
    Martien Verbruggen | I used to have a Heisenbergmobile. Every time
    | I looked at the speedometer, I got lost.
    |
    Martien Verbruggen, Nov 21, 2003
    #5
  6. Edo <> wrote:

    > perldoc -f tie
    >
    > has to do with keeping the hash sorted



    No it doesn't.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 21, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    499
    red floyd
    Nov 10, 2011
  2. Dan
    Replies:
    1
    Views:
    90
    Ben Morrow
    Nov 21, 2003
  3. Thomas Reat

    sharing a (tied) hash between processes

    Thomas Reat, Jan 1, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    118
    pkent
    Jan 3, 2004
  4. Replies:
    14
    Views:
    230
    Tomi Häsä
    Jan 10, 2005
  5. bernd
    Replies:
    0
    Views:
    618
    bernd
    Apr 24, 2012
Loading...

Share This Page