{
    "mode": "perldoc",
    "parameter": "Tie::Hash",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Tie%3A%3AHash/json",
    "generated": "2026-08-18T11:36:12Z",
    "synopsis": "package NewHash;\nrequire Tie::Hash;\n@ISA = qw(Tie::Hash);\nsub DELETE { ... }          # Provides needed method\nsub CLEAR { ... }           # Overrides inherited method\npackage NewStdHash;\nrequire Tie::Hash;\n@ISA = qw(Tie::StdHash);\n# All methods provided by default, define\n# only those needing overrides\n# Accessors access the storage in %{$[0]};\n# TIEHASH should return a reference to the actual storage\nsub DELETE { ... }\npackage NewExtraHash;\nrequire Tie::Hash;\n@ISA = qw(Tie::ExtraHash);\n# All methods provided by default, define\n# only those needing overrides\n# Accessors access the storage in %{$[0][0]};\n# TIEHASH should return an array reference with the first element\n# being the reference to the actual storage\nsub DELETE {\n$[0][1]->('del', $[0][0], $[1]); # Call the report writer\ndelete $[0][0]->{$[1]};           #  $[0]->SUPER::DELETE($[1])\n}\npackage main;\ntie %newhash, 'NewHash';\ntie %newstdhash, 'NewStdHash';\ntie %newextrahash, 'NewExtraHash',\nsub {warn \"Doing \\U$[1]\\E of $[2].\\n\"};",
    "sections": {
        "NAME": {
            "content": "Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package NewHash;\nrequire Tie::Hash;\n\n@ISA = qw(Tie::Hash);\n\nsub DELETE { ... }          # Provides needed method\nsub CLEAR { ... }           # Overrides inherited method\n\n\npackage NewStdHash;\nrequire Tie::Hash;\n\n@ISA = qw(Tie::StdHash);\n\n# All methods provided by default, define\n# only those needing overrides\n# Accessors access the storage in %{$[0]};\n# TIEHASH should return a reference to the actual storage\nsub DELETE { ... }\n\npackage NewExtraHash;\nrequire Tie::Hash;\n\n@ISA = qw(Tie::ExtraHash);\n\n# All methods provided by default, define\n# only those needing overrides\n# Accessors access the storage in %{$[0][0]};\n# TIEHASH should return an array reference with the first element\n# being the reference to the actual storage\nsub DELETE {\n$[0][1]->('del', $[0][0], $[1]); # Call the report writer\ndelete $[0][0]->{$[1]};           #  $[0]->SUPER::DELETE($[1])\n}\n\n\npackage main;\n\ntie %newhash, 'NewHash';\ntie %newstdhash, 'NewStdHash';\ntie %newextrahash, 'NewExtraHash',\nsub {warn \"Doing \\U$[1]\\E of $[2].\\n\"};\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides some skeletal methods for hash-tying classes. See perltie for a list of the\nfunctions required in order to tie a hash to a package. The basic Tie::Hash package provides a\n\"new\" method, as well as methods \"TIEHASH\", \"EXISTS\" and \"CLEAR\". The Tie::StdHash and\nTie::ExtraHash packages provide most methods for hashes described in perltie (the exceptions are\n\"UNTIE\" and \"DESTROY\"). They cause tied hashes to behave exactly like standard hashes, and allow\nfor selective overwriting of methods. Tie::Hash has legacy support for the \"new\" method: it is\nused if \"TIEHASH\" is not defined in the case a class forgets to include a \"TIEHASH\" method.\n\nFor developers wishing to write their own tied hashes, the required methods are briefly defined\nbelow. See the perltie section for more detailed descriptive, as well as example code:\n\nTIEHASH classname, LIST\nThe method invoked by the command \"tie %hash, classname\". Associates a new hash instance\nwith the specified class. \"LIST\" would represent additional arguments (along the lines of\nAnyDBMFile and compatriots) needed to complete the association.\n\nSTORE this, key, value\nStore datum *value* into *key* for the tied hash *this*.\n\nFETCH this, key\nRetrieve the datum in *key* for the tied hash *this*.\n\nFIRSTKEY this\nReturn the first key in the hash.\n\nNEXTKEY this, lastkey\nReturn the next key in the hash.\n\nEXISTS this, key\nVerify that *key* exists with the tied hash *this*.\n\nThe Tie::Hash implementation is a stub that simply croaks.\n\nDELETE this, key\nDelete the key *key* from the tied hash *this*.\n\nCLEAR this\nClear all values from the tied hash *this*.\n\nSCALAR this\nReturns what evaluating the hash in scalar context yields.\n\nTie::Hash does not implement this method (but Tie::StdHash and Tie::ExtraHash do).\n",
            "subsections": []
        },
        "Inheriting from Tie::StdHash": {
            "content": "The accessor methods assume that the actual storage for the data in the tied hash is in the hash\nreferenced by tied(%tiedhash). Thus overwritten \"TIEHASH\" method should return a hash reference,\nand the remaining methods should operate on the hash referenced by the first argument:\n\npackage ReportHash;\nour @ISA = 'Tie::StdHash';\n\nsub TIEHASH  {\nmy $storage = bless {}, shift;\nwarn \"New ReportHash created, stored in $storage.\\n\";\n$storage\n}\nsub STORE    {\nwarn \"Storing data with key $[1] at $[0].\\n\";\n$[0]{$[1]} = $[2]\n}\n",
            "subsections": []
        },
        "Inheriting from Tie::ExtraHash": {
            "content": "The accessor methods assume that the actual storage for the data in the tied hash is in the hash\nreferenced by \"(tied(%tiedhash))->[0]\". Thus overwritten \"TIEHASH\" method should return an array\nreference with the first element being a hash reference, and the remaining methods should\noperate on the hash \"%{ $[0]->[0] }\":\n\npackage ReportHash;\nour @ISA = 'Tie::ExtraHash';\n\nsub TIEHASH  {\nmy $class = shift;\nmy $storage = bless [{}, @], $class;\nwarn \"New ReportHash created, stored in $storage.\\n\";\n$storage;\n}\nsub STORE    {\nwarn \"Storing data with key $[1] at $[0].\\n\";\n$[0][0]{$[1]} = $[2]\n}\n\nThe default \"TIEHASH\" method stores \"extra\" arguments to tie() starting from offset 1 in the\narray referenced by tied(%tiedhash); this is the same storage algorithm as in TIEHASH subroutine\nabove. Hence, a typical package inheriting from Tie::ExtraHash does not need to overwrite this\nmethod.\n\n\"SCALAR\", \"UNTIE\" and \"DESTROY\"\nThe methods \"UNTIE\" and \"DESTROY\" are not defined in Tie::Hash, Tie::StdHash, or Tie::ExtraHash.\nTied hashes do not require presence of these methods, but if defined, the methods will be called\nin proper time, see perltie.\n\n\"SCALAR\" is only defined in Tie::StdHash and Tie::ExtraHash.\n\nIf needed, these methods should be defined by the package inheriting from Tie::Hash,\nTie::StdHash, or Tie::ExtraHash. See \"SCALAR\" in perltie to find out what happens when \"SCALAR\"\ndoes not exist.\n",
            "subsections": []
        },
        "MORE INFORMATION": {
            "content": "The packages relating to various DBM-related implementations (DBFile, NDBMFile, etc.) show\nexamples of general tied hashes, as does the Config module. While these do not utilize\nTie::Hash, they serve as good working examples.\n",
            "subsections": []
        }
    },
    "summary": "Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes",
    "flags": [],
    "examples": [],
    "see_also": []
}