{
    "mode": "info",
    "parameter": "namespace::clean",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/namespace%3A%3Aclean/json",
    "generated": "2026-07-05T16:06:10Z",
    "synopsis": "package Foo;\nuse warnings;\nuse strict;\nuse Carp qw(croak);   # 'croak' will be removed\nsub bar { 23 }        # 'bar' will be removed\n# remove all previously defined functions\nuse namespace::clean;\nsub baz { bar() }     # 'baz' still defined, 'bar' still bound\n# begin to collection function names from here again\nno namespace::clean;\nsub quux { baz() }    # 'quux' will be removed\n# remove all functions defined after the 'no' unimport\nuse namespace::clean;\n# Will print: 'No', 'No', 'Yes' and 'No'\nprint +(PACKAGE->can('croak') ? 'Yes' : 'No'), \"\\n\";\nprint +(PACKAGE->can('bar')   ? 'Yes' : 'No'), \"\\n\";\nprint +(PACKAGE->can('baz')   ? 'Yes' : 'No'), \"\\n\";\nprint +(PACKAGE->can('quux')  ? 'Yes' : 'No'), \"\\n\";\n1;",
    "sections": {
        "NAME": {
            "content": "namespace::clean - Keep imports and functions out of your namespace\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package Foo;\nuse warnings;\nuse strict;\n\nuse Carp qw(croak);   # 'croak' will be removed\n\nsub bar { 23 }        # 'bar' will be removed\n\n# remove all previously defined functions\nuse namespace::clean;\n\nsub baz { bar() }     # 'baz' still defined, 'bar' still bound\n\n# begin to collection function names from here again\nno namespace::clean;\n\nsub quux { baz() }    # 'quux' will be removed\n\n# remove all functions defined after the 'no' unimport\nuse namespace::clean;\n\n# Will print: 'No', 'No', 'Yes' and 'No'\nprint +(PACKAGE->can('croak') ? 'Yes' : 'No'), \"\\n\";\nprint +(PACKAGE->can('bar')   ? 'Yes' : 'No'), \"\\n\";\nprint +(PACKAGE->can('baz')   ? 'Yes' : 'No'), \"\\n\";\nprint +(PACKAGE->can('quux')  ? 'Yes' : 'No'), \"\\n\";\n\n1;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Keeping packages clean\nWhen you define a function, or import one, into a Perl package, it will\nnaturally also be available as a method. This does not per se cause\nproblems, but it can complicate subclassing and, for example, plugin\nclasses that are included via multiple inheritance by loading them as\nbase classes.\n\nThe \"namespace::clean\" pragma will remove all previously declared or\nimported symbols at the end of the current package's compile cycle.\nFunctions called in the package itself will still be bound by their\nname, but they won't show up as methods on your class or instances.\n\nBy unimporting via \"no\" you can tell \"namespace::clean\" to start\ncollecting functions for the next \"use namespace::clean;\"\nspecification.\n\nYou can use the \"-except\" flag to tell \"namespace::clean\" that you\ndon't want it to remove a certain function or method. A common use\nwould be a module exporting an \"import\" method along with some\nfunctions:\n\nuse ModuleExportingImport;\nuse namespace::clean -except => [qw( import )];\n\nIf you just want to \"-except\" a single sub, you can pass it directly.\nFor more than one value you have to use an array reference.\n\nLate binding caveat\n\nNote that the technique used by this module relies on perl having\nresolved all names to actual code references during the compilation of\na scope. While this is almost always what the interpreter does, there\nare some exceptions, notably the sort SUBNAME style of the \"sort\"\nbuilt-in invocation. The following example will not work, because\n\"sort\" does not try to resolve the function name to an actual code\nreference until runtime.\n\nuse MyApp::Utils 'mysorter';\nuse namespace::clean;\n\nmy @sorted = sort mysorter @list;\n\nYou need to work around this by forcing a compile-time resolution like\nso:\n\nuse MyApp::Utils 'mysorter';\nuse namespace::clean;\n\nmy $mysortercref = \\&mysorter;\n\nmy @sorted = sort $mysortercref @list;\n\nExplicitly removing functions when your scope is compiled\nIt is also possible to explicitly tell \"namespace::clean\" what packages\nto remove when the surrounding scope has finished compiling. Here is an\nexample:\n\npackage Foo;\nuse strict;\n\n# blessed NOT available\n\nsub myclass {\nuse Scalar::Util qw( blessed );\nuse namespace::clean qw( blessed );\n\n# blessed available\nreturn blessed shift;\n}\n\n# blessed NOT available\n\nMoose\nWhen using \"namespace::clean\" together with Moose you want to keep the\ninstalled \"meta\" method. So your classes should look like:\n\npackage Foo;\nuse Moose;\nuse namespace::clean -except => 'meta';\n...\n\nSame goes for Moose::Role.\n\nCleaning other packages\nYou can tell \"namespace::clean\" that you want to clean up another\npackage instead of the one importing. To do this you have to pass in\nthe \"-cleanee\" option like this:\n\npackage My::MooseX::namespace::clean;\nuse strict;\n\nuse namespace::clean (); # no cleanup, just load\n\nsub import {\nnamespace::clean->import(\n-cleanee => scalar(caller),\n-except  => 'meta',\n);\n}\n\nIf you don't care about \"namespace::clean\"s discover-and-\"-except\"\nlogic, and just want to remove subroutines, try \"cleansubroutines\".\n",
            "subsections": []
        },
        "METHODS": {
            "content": "cleansubroutines\nThis exposes the actual subroutine-removal logic.\n\nnamespace::clean->cleansubroutines($cleanee, qw( subA subB ));\n\nwill remove \"subA\" and \"subB\" from $cleanee. Note that this will remove\nthe subroutines immediately and not wait for scope end. If you want to\nhave this effect at a specific time (e.g. \"namespace::clean\" acts on\nscope compile end) it is your responsibility to make sure it runs at\nthat time.\n\nimport\nMakes a snapshot of the current defined functions and installs a\nB::Hooks::EndOfScope hook in the current scope to invoke the cleanups.\n\nunimport\nThis method will be called when you do a\n\nno namespace::clean;\n\nIt will start a new section of code that defines functions to clean up.\n\ngetclassstore\nThis returns a reference to a hash in a passed package containing\ninformation about function names included and excluded from removal.\n\ngetfunctions\nTakes a class as argument and returns all currently defined functions\nin it as a hash reference with the function name as key and a typeglob\nreference to the symbol as value.\n",
            "subsections": []
        },
        "IMPLEMENTATION DETAILS": {
            "content": "This module works through the effect that a\n\ndelete $SomePackage::{foo};\n\nwill remove the \"foo\" symbol from $SomePackage for run time lookups\n(e.g., method calls) but will leave the entry alive to be called by\nalready resolved names in the package itself. \"namespace::clean\" will\nrestore and therefor in effect keep all glob slots that aren't \"CODE\".\n\nA test file has been added to the perl core to ensure that this\nbehaviour will be stable in future releases.\n\nJust for completeness sake, if you want to remove the symbol\ncompletely, use \"undef\" instead.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "B::Hooks::EndOfScope\n",
            "subsections": []
        },
        "THANKS": {
            "content": "Many thanks to Matt S Trout for the inspiration on the whole idea.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "o   Robert 'phaylon' Sedlacek <rs@474.at>\n\no   Florian Ragwitz <rafl@debian.org>\n\no   Jesse Luehrs <doy@tozt.net>\n\no   Peter Rabbitson <ribasushi@cpan.org>\n\no   Father Chrysostomos <sprout@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2011 by \"AUTHORS\"\n\nThis is free software; you can redistribute it and/or modify it under\nthe same terms as the Perl 5 programming language system itself.\n\nperl v5.22.2                      2016-06-03             namespace::clean(3pm)",
            "subsections": []
        }
    },
    "summary": "namespace::clean - Keep imports and functions out of your namespace",
    "flags": [],
    "examples": [],
    "see_also": []
}