{
    "mode": "perldoc",
    "parameter": "NEXT",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/NEXT/json",
    "generated": "2026-06-14T12:34:24Z",
    "synopsis": "use NEXT;\npackage P;\nsub P::method   { print \"$[0]: P method\\n\";   $[0]->NEXT::method() }\nsub P::DESTROY  { print \"$[0]: P dtor\\n\";     $[0]->NEXT::DESTROY() }\npackage Q;\nuse base qw( P );\nsub Q::AUTOLOAD { print \"$[0]: Q AUTOLOAD\\n\"; $[0]->NEXT::AUTOLOAD() }\nsub Q::DESTROY  { print \"$[0]: Q dtor\\n\";     $[0]->NEXT::DESTROY() }\npackage R;\nsub R::method   { print \"$[0]: R method\\n\";   $[0]->NEXT::method() }\nsub R::AUTOLOAD { print \"$[0]: R AUTOLOAD\\n\"; $[0]->NEXT::AUTOLOAD() }\nsub R::DESTROY  { print \"$[0]: R dtor\\n\";     $[0]->NEXT::DESTROY() }\npackage S;\nuse base qw( Q R );\nsub S::method   { print \"$[0]: S method\\n\";   $[0]->NEXT::method() }\nsub S::AUTOLOAD { print \"$[0]: S AUTOLOAD\\n\"; $[0]->NEXT::AUTOLOAD() }\nsub S::DESTROY  { print \"$[0]: S dtor\\n\";     $[0]->NEXT::DESTROY() }\npackage main;\nmy $obj = bless {}, \"S\";\n$obj->method();             # Calls S::method, P::method, R::method\n$obj->missingmethod(); # Calls S::AUTOLOAD, Q::AUTOLOAD, R::AUTOLOAD\n# Clean-up calls S::DESTROY, Q::DESTROY, P::DESTROY, R::DESTROY",
    "sections": {
        "NAME": {
            "content": "NEXT - Provide a pseudo-class NEXT (et al) that allows method redispatch\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use NEXT;\n\npackage P;\nsub P::method   { print \"$[0]: P method\\n\";   $[0]->NEXT::method() }\nsub P::DESTROY  { print \"$[0]: P dtor\\n\";     $[0]->NEXT::DESTROY() }\n\npackage Q;\nuse base qw( P );\nsub Q::AUTOLOAD { print \"$[0]: Q AUTOLOAD\\n\"; $[0]->NEXT::AUTOLOAD() }\nsub Q::DESTROY  { print \"$[0]: Q dtor\\n\";     $[0]->NEXT::DESTROY() }\n\npackage R;\nsub R::method   { print \"$[0]: R method\\n\";   $[0]->NEXT::method() }\nsub R::AUTOLOAD { print \"$[0]: R AUTOLOAD\\n\"; $[0]->NEXT::AUTOLOAD() }\nsub R::DESTROY  { print \"$[0]: R dtor\\n\";     $[0]->NEXT::DESTROY() }\n\npackage S;\nuse base qw( Q R );\nsub S::method   { print \"$[0]: S method\\n\";   $[0]->NEXT::method() }\nsub S::AUTOLOAD { print \"$[0]: S AUTOLOAD\\n\"; $[0]->NEXT::AUTOLOAD() }\nsub S::DESTROY  { print \"$[0]: S dtor\\n\";     $[0]->NEXT::DESTROY() }\n\npackage main;\n\nmy $obj = bless {}, \"S\";\n\n$obj->method();             # Calls S::method, P::method, R::method\n$obj->missingmethod(); # Calls S::AUTOLOAD, Q::AUTOLOAD, R::AUTOLOAD\n\n# Clean-up calls S::DESTROY, Q::DESTROY, P::DESTROY, R::DESTROY\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"NEXT\" module adds a pseudoclass named \"NEXT\" to any program that uses it. If a method \"m\"\ncalls \"$self->NEXT::m()\", the call to \"m\" is redispatched as if the calling method had not\noriginally been found.\n\nNote: before using this module, you should look at next::method\n<https://metacpan.org/pod/mro#next::method> in the core mro module. \"mro\" has been a core module\nsince Perl 5.9.5.\n\nIn other words, a call to \"$self->NEXT::m()\" resumes the depth-first, left-to-right search of\n$self's class hierarchy that resulted in the original call to \"m\".\n\nNote that this is not the same thing as \"$self->SUPER::m()\", which begins a new dispatch that is\nrestricted to searching the ancestors of the current class. \"$self->NEXT::m()\" can backtrack\npast the current class -- to look for a suitable method in other ancestors of $self -- whereas\n\"$self->SUPER::m()\" cannot.\n\nA typical use would be in the destructors of a class hierarchy, as illustrated in the SYNOPSIS\nabove. Each class in the hierarchy has a DESTROY method that performs some class-specific action\nand then redispatches the call up the hierarchy. As a result, when an object of class S is\ndestroyed, the destructors of *all* its parent classes are called (in depth-first, left-to-right\norder).\n\nAnother typical use of redispatch would be in \"AUTOLOAD\"'ed methods. If such a method determined\nthat it was not able to handle a particular call, it might choose to redispatch that call, in\nthe hope that some other \"AUTOLOAD\" (above it, or to its left) might do better.\n\nBy default, if a redispatch attempt fails to find another method elsewhere in the objects class\nhierarchy, it quietly gives up and does nothing (but see \"Enforcing redispatch\"). This gracious\nacquiescence is also unlike the (generally annoying) behaviour of \"SUPER\", which throws an\nexception if it cannot redispatch.\n\nNote that it is a fatal error for any method (including \"AUTOLOAD\") to attempt to redispatch any\nmethod that does not have the same name. For example:\n\nsub S::oops { print \"oops!\\n\"; $[0]->NEXT::othermethod() }\n",
            "subsections": [
                {
                    "name": "Enforcing redispatch",
                    "content": "It is possible to make \"NEXT\" redispatch more demandingly (i.e. like \"SUPER\" does), so that the\nredispatch throws an exception if it cannot find a \"next\" method to call.\n\nTo do this, simple invoke the redispatch as:\n\n$self->NEXT::ACTUAL::method();\n\nrather than:\n\n$self->NEXT::method();\n\nThe \"ACTUAL\" tells \"NEXT\" that there must actually be a next method to call, or it should throw\nan exception.\n\n\"NEXT::ACTUAL\" is most commonly used in \"AUTOLOAD\" methods, as a means to decline an \"AUTOLOAD\"\nrequest, but preserve the normal exception-on-failure semantics:\n\nsub AUTOLOAD {\nif ($AUTOLOAD =~ /foo|bar/) {\n# handle here\n}\nelse {  # try elsewhere\nshift()->NEXT::ACTUAL::AUTOLOAD(@);\n}\n}\n\nBy using \"NEXT::ACTUAL\", if there is no other \"AUTOLOAD\" to handle the method call, an exception\nwill be thrown (as usually happens in the absence of a suitable \"AUTOLOAD\").\n"
                },
                {
                    "name": "Avoiding repetitions",
                    "content": "If \"NEXT\" redispatching is used in the methods of a \"diamond\" class hierarchy:\n\n#     A   B\n#    / \\ /\n#   C   D\n#    \\ /\n#     E\n\nuse NEXT;\n\npackage A;\nsub foo { print \"called A::foo\\n\"; shift->NEXT::foo() }\n\npackage B;\nsub foo { print \"called B::foo\\n\"; shift->NEXT::foo() }\n\npackage C; @ISA = qw( A );\nsub foo { print \"called C::foo\\n\"; shift->NEXT::foo() }\n\npackage D; @ISA = qw(A B);\nsub foo { print \"called D::foo\\n\"; shift->NEXT::foo() }\n\npackage E; @ISA = qw(C D);\nsub foo { print \"called E::foo\\n\"; shift->NEXT::foo() }\n\nE->foo();\n\nthen derived classes may (re-)inherit base-class methods through two or more distinct paths\n(e.g. in the way \"E\" inherits \"A::foo\" twice -- through \"C\" and \"D\"). In such cases, a sequence\nof \"NEXT\" redispatches will invoke the multiply inherited method as many times as it is\ninherited. For example, the above code prints:\n\ncalled E::foo\ncalled C::foo\ncalled A::foo\ncalled D::foo\ncalled A::foo\ncalled B::foo\n\n(i.e. \"A::foo\" is called twice).\n\nIn some cases this *may* be the desired effect within a diamond hierarchy, but in others (e.g.\nfor destructors) it may be more appropriate to call each method only once during a sequence of\nredispatches.\n\nTo cover such cases, you can redispatch methods via:\n\n$self->NEXT::DISTINCT::method();\n\nrather than:\n\n$self->NEXT::method();\n\nThis causes the redispatcher to only visit each distinct \"method\" method once. That is, to skip\nany classes in the hierarchy that it has already visited during redispatch. So, for example, if\nthe previous example were rewritten:\n\npackage A;\nsub foo { print \"called A::foo\\n\"; shift->NEXT::DISTINCT::foo() }\n\npackage B;\nsub foo { print \"called B::foo\\n\"; shift->NEXT::DISTINCT::foo() }\n\npackage C; @ISA = qw( A );\nsub foo { print \"called C::foo\\n\"; shift->NEXT::DISTINCT::foo() }\n\npackage D; @ISA = qw(A B);\nsub foo { print \"called D::foo\\n\"; shift->NEXT::DISTINCT::foo() }\n\npackage E; @ISA = qw(C D);\nsub foo { print \"called E::foo\\n\"; shift->NEXT::DISTINCT::foo() }\n\nE->foo();\n\nthen it would print:\n\ncalled E::foo\ncalled C::foo\ncalled A::foo\ncalled D::foo\ncalled B::foo\n\nand omit the second call to \"A::foo\" (since it would not be distinct from the first call to\n\"A::foo\").\n\nNote that you can also use:\n\n$self->NEXT::DISTINCT::ACTUAL::method();\n\nor:\n\n$self->NEXT::ACTUAL::DISTINCT::method();\n\nto get both unique invocation *and* exception-on-failure.\n\nNote that, for historical compatibility, you can also use \"NEXT::UNSEEN\" instead of\n\"NEXT::DISTINCT\".\n"
                },
                {
                    "name": "Invoking all versions of a method with a single call",
                    "content": "Yet another pseudo-class that \"NEXT\" provides is \"EVERY\". Its behaviour is considerably simpler\nthan that of the \"NEXT\" family. A call to:\n\n$obj->EVERY::foo();\n\ncalls *every* method named \"foo\" that the object in $obj has inherited. That is:\n\nuse NEXT;\n\npackage A; @ISA = qw(B D X);\nsub foo { print \"A::foo \" }\n\npackage B; @ISA = qw(D X);\nsub foo { print \"B::foo \" }\n\npackage X; @ISA = qw(D);\nsub foo { print \"X::foo \" }\n\npackage D;\nsub foo { print \"D::foo \" }\n\npackage main;\n\nmy $obj = bless {}, 'A';\n$obj->EVERY::foo();        # prints\" A::foo B::foo X::foo D::foo\n\nPrefixing a method call with \"EVERY::\" causes every method in the object's hierarchy with that\nname to be invoked. As the above example illustrates, they are not called in Perl's usual\n\"left-most-depth-first\" order. Instead, they are called \"breadth-first-dependency-wise\".\n\nThat means that the inheritance tree of the object is traversed breadth-first and the resulting\norder of classes is used as the sequence in which methods are called. However, that sequence is\nmodified by imposing a rule that the appropriate method of a derived class must be called before\nthe same method of any ancestral class. That's why, in the above example, \"X::foo\" is called\nbefore \"D::foo\", even though \"D\" comes before \"X\" in @B::ISA.\n\nIn general, there's no need to worry about the order of calls. They will be left-to-right,\nbreadth-first, most-derived-first. This works perfectly for most inherited methods (including\ndestructors), but is inappropriate for some kinds of methods (such as constructors, cloners,\ndebuggers, and initializers) where it's more appropriate that the least-derived methods be\ncalled first (as more-derived methods may rely on the behaviour of their \"ancestors\"). In that\ncase, instead of using the \"EVERY\" pseudo-class:\n\n$obj->EVERY::foo();        # prints\" A::foo B::foo X::foo D::foo\n\nyou can use the \"EVERY::LAST\" pseudo-class:\n\n$obj->EVERY::LAST::foo();  # prints\" D::foo X::foo B::foo A::foo\n\nwhich reverses the order of method call.\n\nWhichever version is used, the actual methods are called in the same context (list, scalar, or\nvoid) as the original call via \"EVERY\", and return:\n\n*   A hash of array references in list context. Each entry of the hash has the fully qualified\nmethod name as its key and a reference to an array containing the method's list-context\nreturn values as its value.\n\n*   A reference to a hash of scalar values in scalar context. Each entry of the hash has the\nfully qualified method name as its key and the method's scalar-context return values as its\nvalue.\n\n*   Nothing in void context (obviously).\n\nUsing \"EVERY\" methods\nThe typical way to use an \"EVERY\" call is to wrap it in another base method, that all classes\ninherit. For example, to ensure that every destructor an object inherits is actually called (as\nopposed to just the left-most-depth-first-est one):\n\npackage Base;\nsub DESTROY { $[0]->EVERY::Destroy }\n\npackage Derived1;\nuse base 'Base';\nsub Destroy {...}\n\npackage Derived2;\nuse base 'Base', 'Derived1';\nsub Destroy {...}\n\net cetera. Every derived class than needs its own clean-up behaviour simply adds its own\n\"Destroy\" method (*not* a \"DESTROY\" method), which the call to \"EVERY::LAST::Destroy\" in the\ninherited destructor then correctly picks up.\n\nLikewise, to create a class hierarchy in which every initializer inherited by a new object is\ninvoked:\n\npackage Base;\nsub new {\nmy ($class, %args) = @;\nmy $obj = bless {}, $class;\n$obj->EVERY::LAST::Init(\\%args);\n}\n\npackage Derived1;\nuse base 'Base';\nsub Init {\nmy ($argsref) = @;\n...\n}\n\npackage Derived2;\nuse base 'Base', 'Derived1';\nsub Init {\nmy ($argsref) = @;\n...\n}\n\net cetera. Every derived class than needs some additional initialization behaviour simply adds\nits own \"Init\" method (*not* a \"new\" method), which the call to \"EVERY::LAST::Init\" in the\ninherited constructor then correctly picks up.\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "mro (in particular next::method <https://metacpan.org/pod/mro#next::method>), which has been a\ncore module since Perl 5.9.5.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Damian Conway (damian@conway.org)\n",
            "subsections": []
        },
        "BUGS AND IRRITATIONS": {
            "content": "Because it's a module, not an integral part of the interpreter, \"NEXT\" has to guess where the\nsurrounding call was found in the method look-up sequence. In the presence of diamond\ninheritance patterns it occasionally guesses wrong.\n\nIt's also too slow (despite caching).\n\nComment, suggestions, and patches welcome.\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.\nThis module is free software. It may be used, redistributed\nand/or modified under the same terms as Perl itself.\n",
            "subsections": []
        }
    },
    "summary": "NEXT - Provide a pseudo-class NEXT (et al) that allows method redispatch",
    "flags": [],
    "examples": [],
    "see_also": []
}