{
    "content": [
        {
            "type": "text",
            "text": "# DBM_Filter (man)\n\n## NAME\n\nDBMFilter -- Filter DBM keys/values\n\n## SYNOPSIS\n\nuse 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;\n\n## DESCRIPTION\n\nThis 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\nmodules included with the standard Perl source distribution from version 5.6.1 onwards. In\naddition to the *DB*File modules distributed with Perl, the BerkeleyDB module, available on\nCPAN, supports the DBM Filter hooks. See perldbmfilter for more details on the DBM Filter\nhooks.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION** (2 subsections)\n- **METHODS** (5 subsections)\n- **NOTES** (2 subsections)\n- **EXAMPLE**\n- **SEE ALSO**\n- **AUTHOR**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "DBM_Filter",
        "section": "",
        "mode": "man",
        "summary": "DBMFilter -- Filter DBM keys/values",
        "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;",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "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": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 37,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 7,
                "subsections": [
                    {
                        "name": "What is a DBM Filter?",
                        "lines": 18
                    },
                    {
                        "name": "So what's new?",
                        "lines": 9
                    }
                ]
            },
            {
                "name": "METHODS",
                "lines": 19,
                "subsections": [
                    {
                        "name": "$db->Filtered()",
                        "lines": 3
                    },
                    {
                        "name": "Writing a Filter",
                        "lines": 2
                    },
                    {
                        "name": "Immediate Filters",
                        "lines": 24
                    },
                    {
                        "name": "Canned Filters",
                        "lines": 71
                    },
                    {
                        "name": "Filters Included",
                        "lines": 36
                    }
                ]
            },
            {
                "name": "NOTES",
                "lines": 1,
                "subsections": [
                    {
                        "name": "Maintain Round Trip Integrity",
                        "lines": 23
                    },
                    {
                        "name": "Don't mix filtered & non-filtered data in the same database file.",
                        "lines": 3
                    }
                ]
            },
            {
                "name": "EXAMPLE",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "AUTHOR",
                "lines": 5,
                "subsections": []
            }
        ],
        "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\nmodules included with the standard Perl source distribution from version 5.6.1 onwards. In\naddition to the *DB*File modules distributed with Perl, the BerkeleyDB module, available on\nCPAN, supports the DBM Filter hooks. See perldbmfilter for more details on the DBM Filter\nhooks.\n",
                "subsections": [
                    {
                        "name": "What is a DBM Filter?",
                        "content": "A 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\nfile. For 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\nvalue.\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"
                    },
                    {
                        "name": "So what's new?",
                        "content": "The 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"
                    }
                ]
            },
            "METHODS": {
                "content": "This module will arrange for the following methods to be available via the object returned\nfrom the \"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\nthey apply 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",
                "subsections": [
                    {
                        "name": "$db->Filtered()",
                        "content": "Returns TRUE if there are any filters applied to the DBM associated with $db.  Otherwise\nreturns FALSE.\n"
                    },
                    {
                        "name": "Writing a Filter",
                        "content": "Filters can be created in two main ways\n"
                    },
                    {
                        "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\nthe database and the code reference associated with \"Fetch\" will be called after any\nkey/value is read from the database.\n\nFor example, here is a sample filter that adds a trailing NULL character to all strings\nbefore they are written to the DBM file, and removes the trailing NULL when they are read\nfrom the DBM file\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\nuseful to 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\n\"utf8\", included 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\nfor the 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"
                    },
                    {
                        "name": "Filters Included",
                        "content": "A number of canned filers are provided with this module. They cover a number of the main\nareas that filters are needed when interfacing with DBM files. They also act as templates for\nyour own filters.\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\nit on 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\nthat all data written to the DBM file can be read by the C application.\n"
                    }
                ]
            },
            "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\nnot behave 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"
                    },
                    {
                        "name": "Don't mix filtered & non-filtered data in the same database file.",
                        "content": "This is just a restatement of the previous section. Unless you are completely certain you\nknow what 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\n\n\nperl v5.34.0                                 2026-06-23                            DBMFilter(3perl)",
                "subsections": []
            }
        }
    }
}