{
    "mode": "perldoc",
    "parameter": "DBM_Filter",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/DBM_Filter/json",
    "generated": "2026-06-14T07:34:34Z",
    "synopsis": "use DBMFilter ;\nuse SDBMFile; # or DBFile, GDBMFile, NDBMFile, or ODBMFile\n$db = tie %hash, ...\n$db->FilterPush(Fetch => sub {...},\nStore => sub {...});\n$db->FilterPush('myfilter1');\n$db->FilterPush('myfilter2', params...);\n$db->FilterKeyPush(...) ;\n$db->FilterValuePush(...) ;\n$db->FilterPop();\n$db->Filtered();\npackage DBMFilter::myfilter1;\nsub Store { ... }\nsub Fetch { ... }\n1;\npackage DBMFilter::myfilter2;\nsub Filter\n{\nmy @opts = @;\n...\nreturn (\nsub Store { ... },\nsub Fetch { ... } );\n}\n1;",
    "sections": {
        "NAME": {
            "content": "DBMFilter -- Filter DBM keys/values\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use DBMFilter ;\nuse SDBMFile; # or DBFile, GDBMFile, NDBMFile, or ODBMFile\n\n$db = tie %hash, ...\n\n$db->FilterPush(Fetch => sub {...},\nStore => sub {...});\n\n$db->FilterPush('myfilter1');\n$db->FilterPush('myfilter2', params...);\n\n$db->FilterKeyPush(...) ;\n$db->FilterValuePush(...) ;\n\n$db->FilterPop();\n$db->Filtered();\n\npackage DBMFilter::myfilter1;\n\nsub Store { ... }\nsub Fetch { ... }\n\n1;\n\npackage DBMFilter::myfilter2;\n\nsub Filter\n{\nmy @opts = @;\n...\nreturn (\nsub Store { ... },\nsub Fetch { ... } );\n}\n\n1;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides an interface that allows filters to be applied to tied Hashes associated\nwith DBM files. It builds on the DBM Filter hooks that are present in all the *DB*File modules\nincluded with the standard Perl source distribution from version 5.6.1 onwards. In addition to\nthe *DB*File modules distributed with Perl, the BerkeleyDB module, available on CPAN, supports\nthe DBM Filter hooks. See perldbmfilter for more details on the DBM Filter hooks.\n\nWhat is a DBM Filter?\nA DBM Filter allows the keys and/or values in a tied hash to be modified by some user-defined\ncode just before it is written to the DBM file and just after it is read back from the DBM file.\nFor example, this snippet of code\n\n$somehash{\"abc\"} = 42;\n\ncould potentially trigger two filters, one for the writing of the key \"abc\" and another for\nwriting the value 42. Similarly, this snippet\n\nmy ($key, $value) = each %somehash\n\nwill trigger two filters, one for the reading of the key and one for the reading of the value.\n\nLike the existing DBM Filter functionality, this module arranges for the $ variable to be\npopulated with the key or value that a filter will check. This usually means that most DBM\nfilters tend to be very short.\n\nSo what's new?\nThe main enhancements over the standard DBM Filter hooks are:\n\n*   A cleaner interface.\n\n*   The ability to easily apply multiple filters to a single DBM file.\n\n*   The ability to create \"canned\" filters. These allow commonly used filters to be packaged\ninto a stand-alone module.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "This module will arrange for the following methods to be available via the object returned from\nthe \"tie\" call.\n\n$db->FilterPush() / $db->FilterKeyPush() / $db->FilterValuePush()\nAdd a filter to filter stack for the database, $db. The three formats vary only in whether they\napply to the DBM key, the DBM value or both.\n\nFilterPush\nThe filter is applied to *both* keys and values.\n\nFilterKeyPush\nThe filter is applied to the key *only*.\n\nFilterValuePush\nThe filter is applied to the value *only*.\n\n$db->FilterPop()\nRemoves the last filter that was applied to the DBM file associated with $db, if present.\n\n$db->Filtered()\nReturns TRUE if there are any filters applied to the DBM associated with $db. Otherwise returns\nFALSE.\n",
            "subsections": []
        },
        "Writing a Filter": {
            "content": "Filters can be created in two main ways\n",
            "subsections": [
                {
                    "name": "Immediate Filters",
                    "content": "An immediate filter allows you to specify the filter code to be used at the point where the\nfilter is applied to a dbm. In this mode the Filter*Push methods expects to receive exactly\ntwo parameters.\n\nmy $db = tie %hash, 'SDBMFile', ...\n$db->FilterPush( Store => sub { },\nFetch => sub { });\n\nThe code reference associated with \"Store\" will be called before any key/value is written to the\ndatabase and the code reference associated with \"Fetch\" will be called after any key/value is\nread from the database.\n\nFor example, here is a sample filter that adds a trailing NULL character to all strings before\nthey are written to the DBM file, and removes the trailing NULL when they are read from the DBM\nfile\n\nmy $db = tie %hash, 'SDBMFile', ...\n$db->FilterPush( Store => sub { $ .= \"\\x00\" ; },\nFetch => sub { s/\\x00$// ;    });\n\nPoints to note:\n\n1.   Both the Store and Fetch filters manipulate $.\n"
                },
                {
                    "name": "Canned Filters",
                    "content": "Immediate filters are useful for one-off situations. For more generic problems it can be useful\nto package the filter up in its own module.\n\nThe usage is for a canned filter is:\n\n$db->FilterPush(\"name\", params)\n\nwhere\n\n\"name\"\nis the name of the module to load. If the string specified does not contain the package\nseparator characters \"::\", it is assumed to refer to the full module name\n\"DBMFilter::name\". This means that the full names for canned filters, \"null\" and \"utf8\",\nincluded with this module are:\n\nDBMFilter::null\nDBMFilter::utf8\n\nparams\nany optional parameters that need to be sent to the filter. See the encode filter for an\nexample of a module that uses parameters.\n\nThe module that implements the canned filter can take one of two forms. Here is a template for\nthe first\n\npackage DBMFilter::null ;\n\nuse strict;\nuse warnings;\n\nsub Store\n{\n# store code here\n}\n\nsub Fetch\n{\n# fetch code here\n}\n\n1;\n\nNotes:\n\n1.   The package name uses the \"DBMFilter::\" prefix.\n\n2.   The module *must* have both a Store and a Fetch method. If only one is present, or neither\nare present, a fatal error will be thrown.\n\nThe second form allows the filter to hold state information using a closure, thus:\n\npackage DBMFilter::encoding ;\n\nuse strict;\nuse warnings;\n\nsub Filter\n{\nmy @params = @ ;\n\n...\nreturn {\nStore   => sub { $ = $encoding->encode($) },\nFetch   => sub { $ = $encoding->decode($) }\n} ;\n}\n\n1;\n\nIn this instance the \"Store\" and \"Fetch\" methods are encapsulated inside a \"Filter\" method.\n"
                }
            ]
        },
        "Filters Included": {
            "content": "A number of canned filers are provided with this module. They cover a number of the main areas\nthat filters are needed when interfacing with DBM files. They also act as templates for your own\nfilters.\n\nThe filter included are:\n\n*    utf8\n\nThis module will ensure that all data written to the DBM will be encoded in UTF-8.\n\nThis module needs the Encode module.\n\n*    encode\n\nAllows you to choose the character encoding will be store in the DBM file.\n\n*    compress\n\nThis filter will compress all data before it is written to the database and uncompressed it\non reading.\n\nThis module needs Compress::Zlib.\n\n*    int32\n\nThis module is used when interoperating with a C/C++ application that uses a C int as\neither the key and/or value in the DBM file.\n\n*    null\n\nThis module ensures that all data written to the DBM file is null terminated. This is\nuseful when you have a perl script that needs to interoperate with a DBM file that a C\nprogram also uses. A fairly common issue is for the C application to include the\nterminating null in a string when it writes to the DBM file. This filter will ensure that\nall data written to the DBM file can be read by the C application.\n",
            "subsections": []
        },
        "NOTES": {
            "content": "",
            "subsections": [
                {
                    "name": "Maintain Round Trip Integrity",
                    "content": "When writing a DBM filter it is *very* important to ensure that it is possible to retrieve all\ndata that you have written when the DBM filter is in place. In practice, this means that\nwhatever transformation is applied to the data in the Store method, the *exact* inverse\noperation should be applied in the Fetch method.\n\nIf you don't provide an exact inverse transformation, you will find that code like this will not\nbehave as you expect.\n\nwhile (my ($k, $v) = each %hash)\n{\n...\n}\n\nDepending on the transformation, you will find that one or more of the following will happen\n\n1    The loop will never terminate.\n\n2    Too few records will be retrieved.\n\n3    Too many will be retrieved.\n\n4    The loop will do the right thing for a while, but it will unexpectedly fail.\n\nDon't mix filtered & non-filtered data in the same database file.\nThis is just a restatement of the previous section. Unless you are completely certain you know\nwhat you are doing, avoid mixing filtered & non-filtered data.\n"
                }
            ]
        },
        "EXAMPLE": {
            "content": "Say you need to interoperate with a legacy C application that stores keys as C ints and the\nvalues and null terminated UTF-8 strings. Here is how you would set that up\n\nmy $db = tie %hash, 'SDBMFile', ...\n\n$db->FilterKeyPush('int32') ;\n\n$db->FilterValuePush('utf8');\n$db->FilterValuePush('null');\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "<DBFile>, GDBMFile, NDBMFile, ODBMFile, SDBMFile, perldbmfilter\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Paul Marquess <pmqs@cpan.org>\n",
            "subsections": []
        }
    },
    "summary": "DBMFilter -- Filter DBM keys/values",
    "flags": [],
    "examples": [
        "Say you need to interoperate with a legacy C application that stores keys as C ints and the",
        "values and null terminated UTF-8 strings. Here is how you would set that up",
        "my $db = tie %hash, 'SDBMFile', ...",
        "$db->FilterKeyPush('int32') ;",
        "$db->FilterValuePush('utf8');",
        "$db->FilterValuePush('null');"
    ],
    "see_also": []
}