{
    "mode": "perldoc",
    "parameter": "SelfLoader",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/SelfLoader/json",
    "generated": "2026-06-13T21:19:58Z",
    "synopsis": "package FOOBAR;\nuse SelfLoader;\n... (initializing code)\nDATA\nsub {....",
    "sections": {
        "NAME": {
            "content": "SelfLoader - load functions only on demand\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package FOOBAR;\nuse SelfLoader;\n\n... (initializing code)\n\nDATA\nsub {....\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module tells its users that functions in the FOOBAR package are to be autoloaded from after\nthe \"DATA\" token. See also \"Autoloading\" in perlsub.\n",
            "subsections": [
                {
                    "name": "The DATA token",
                    "content": "The \"DATA\" token tells the perl compiler that the perl code for compilation is finished.\nEverything after the \"DATA\" token is available for reading via the filehandle FOOBAR::DATA,\nwhere FOOBAR is the name of the current package when the \"DATA\" token is reached. This works\njust the same as \"END\" does in package 'main', but for other modules data after \"END\" is\nnot automatically retrievable, whereas data after \"DATA\" is. The \"DATA\" token is not\nrecognized in versions of perl prior to 5.001m.\n\nNote that it is possible to have \"DATA\" tokens in the same package in multiple files, and\nthat the last \"DATA\" token in a given package that is encountered by the compiler is the one\naccessible by the filehandle. This also applies to \"END\" and main, i.e. if the 'main'\nprogram has an \"END\", but a module 'require'd (not 'use'd) by that program has a 'package\nmain;' declaration followed by an '\"DATA\"', then the \"DATA\" filehandle is set to access the\ndata after the \"DATA\" in the module, not the data after the \"END\" token in the 'main'\nprogram, since the compiler encounters the 'require'd file later.\n"
                },
                {
                    "name": "SelfLoader autoloading",
                    "content": "The SelfLoader works by the user placing the \"DATA\" token *after* perl code which needs to\nbe compiled and run at 'require' time, but *before* subroutine declarations that can be loaded\nin later - usually because they may never be called.\n\nThe SelfLoader will read from the FOOBAR::DATA filehandle to load in the data after \"DATA\",\nand load in any subroutine when it is called. The costs are the one-time parsing of the data\nafter \"DATA\", and a load delay for the first call of any autoloaded function. The benefits\n(hopefully) are a speeded up compilation phase, with no need to load functions which are never\nused.\n\nThe SelfLoader will stop reading from \"DATA\" if it encounters the \"END\" token - just as\nyou would expect. If the \"END\" token is present, and is followed by the token DATA, then the\nSelfLoader leaves the FOOBAR::DATA filehandle open on the line after that token.\n\nThe SelfLoader exports the \"AUTOLOAD\" subroutine to the package using the SelfLoader, and this\nloads the called subroutine when it is first called.\n\nThere is no advantage to putting subroutines which will always be called after the \"DATA\"\ntoken.\n"
                },
                {
                    "name": "Autoloading and package lexicals",
                    "content": "A 'my $packlexical' statement makes the variable $packlexical local only to the file up to\nthe \"DATA\" token. Subroutines declared elsewhere cannot see these types of variables, just\nas if you declared subroutines in the package but in another file, they cannot see these\nvariables.\n\nSo specifically, autoloaded functions cannot see package lexicals (this applies to both the\nSelfLoader and the Autoloader). The \"vars\" pragma provides an alternative to defining\npackage-level globals that will be visible to autoloaded routines. See the documentation on vars\nin the pragma section of perlmod.\n"
                },
                {
                    "name": "SelfLoader and AutoLoader",
                    "content": "The SelfLoader can replace the AutoLoader - just change 'use AutoLoader' to 'use SelfLoader'\n(though note that the SelfLoader exports the AUTOLOAD function - but if you have your own\nAUTOLOAD and are using the AutoLoader too, you probably know what you're doing), and the\n\"END\" token to \"DATA\". You will need perl version 5.001m or later to use this (version\n5.001 with all patches up to patch m).\n\nThere is no need to inherit from the SelfLoader.\n\nThe SelfLoader works similarly to the AutoLoader, but picks up the subs from after the\n\"DATA\" instead of in the 'lib/auto' directory. There is a maintenance gain in not needing to\nrun AutoSplit on the module at installation, and a runtime gain in not needing to keep opening\nand closing files to load subs. There is a runtime loss in needing to parse the code after the\n\"DATA\". Details of the AutoLoader and another view of these distinctions can be found in\nthat module's documentation.\n\nDATA, END, and the FOOBAR::DATA filehandle.\nThis section is only relevant if you want to use the \"FOOBAR::DATA\" together with the\nSelfLoader.\n\nData after the \"DATA\" token in a module is read using the FOOBAR::DATA filehandle. \"END\"\ncan still be used to denote the end of the \"DATA\" section if followed by the token DATA -\nthis is supported by the SelfLoader. The \"FOOBAR::DATA\" filehandle is left open if an \"END\"\nfollowed by a DATA is found, with the filehandle positioned at the start of the line after the\n\"END\" token. If no \"END\" token is present, or an \"END\" token with no DATA token on\nthe same line, then the filehandle is closed.\n\nThe SelfLoader reads from wherever the current position of the \"FOOBAR::DATA\" filehandle is,\nuntil the EOF or \"END\". This means that if you want to use that filehandle (and ONLY if you\nwant to), you should either\n\n1. Put all your subroutine declarations immediately after the \"DATA\" token and put your own\ndata after those declarations, using the \"END\" token to mark the end of subroutine\ndeclarations. You must also ensure that the SelfLoader reads first by calling\n'SelfLoader->loadstubs();', or by using a function which is selfloaded;\n\nor\n\n2. You should read the \"FOOBAR::DATA\" filehandle first, leaving the handle open and positioned\nat the first line of subroutine declarations.\n\nYou could conceivably do both.\n"
                },
                {
                    "name": "Classes and inherited methods.",
                    "content": "For modules which are not classes, this section is not relevant. This section is only relevant\nif you have methods which could be inherited.\n\nA subroutine stub (or forward declaration) looks like\n\nsub stub;\n\ni.e. it is a subroutine declaration without the body of the subroutine. For modules which are\nnot classes, there is no real need for stubs as far as autoloading is concerned.\n\nFor modules which ARE classes, and need to handle inherited methods, stubs are needed to ensure\nthat the method inheritance mechanism works properly. You can load the stubs into the module at\n'require' time, by adding the statement 'SelfLoader->loadstubs();' to the module to do this.\n\nThe alternative is to put the stubs in before the \"DATA\" token BEFORE releasing the module,\nand for this purpose the \"Devel::SelfStubber\" module is available. However this does require the\nextra step of ensuring that the stubs are in the module. If this is done I strongly recommend\nthat this is done BEFORE releasing the module - it should NOT be done at install time in\ngeneral.\n"
                }
            ]
        },
        "Multiple packages and fully qualified subroutine names": {
            "content": "Subroutines in multiple packages within the same file are supported - but you should note that\nthis requires exporting the \"SelfLoader::AUTOLOAD\" to every package which requires it. This is\ndone automatically by the SelfLoader when it first loads the subs into the cache, but you should\nreally specify it in the initialization before the \"DATA\" by putting a 'use SelfLoader'\nstatement in each package.\n\nFully qualified subroutine names are also supported. For example,\n\nDATA\nsub foo::bar {23}\npackage baz;\nsub dob {32}\n\nwill all be loaded correctly by the SelfLoader, and the SelfLoader will ensure that the packages\n'foo' and 'baz' correctly have the SelfLoader \"AUTOLOAD\" method when the data after \"DATA\"\nis first parsed.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "\"SelfLoader\" is maintained by the perl5-porters. Please direct any questions to the canonical\nmailing list. Anything that is applicable to the CPAN release can be sent to its maintainer,\nthough.\n\nAuthor and Maintainer: The Perl5-Porters <perl5-porters@perl.org>\n\nMaintainer of the CPAN release: Steffen Mueller <smueller@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This package has been part of the perl core since the first release of perl5. It has been\nreleased separately to CPAN so older installations can benefit from bug fixes.\n\nThis package has the same copyright and license as the perl core:\n\nCopyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006\nby Larry Wall and others\n\nAll rights reserved.\n\nThis program is free software; you can redistribute it and/or modify it under the terms of\neither:\n\na)  the GNU General Public License as published by the Free Software Foundation; either version\n1, or (at your option) any later version, or\n\nb)  the \"Artistic License\" which comes with this Kit.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;\nwithout even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See\neither the GNU General Public License or the Artistic License for more details.\n\nYou should have received a copy of the Artistic License with this Kit, in the file named\n\"Artistic\". If not, I'll be glad to provide one.\n\nYou should also have received a copy of the GNU General Public License along with this program\nin the file named \"Copying\". If not, write to the Free Software Foundation, Inc., 51 Franklin\nSt, Fifth Floor, Boston, MA 02110-1301, USA or visit their web page on the internet at\n<http://www.gnu.org/copyleft/gpl.html>.\n\nFor those of you that choose to use the GNU General Public License, my interpretation of the GNU\nGeneral Public License is that no Perl script falls under the terms of the GPL unless you\nexplicitly put said script under the terms of the GPL yourself. Furthermore, any object code\nlinked with perl does not automatically fall under the terms of the GPL, provided such object\ncode only adds definitions of subroutines and variables, and does not otherwise impair the\nresulting interpreter from executing any standard Perl script. I consider linking in C\nsubroutines in this manner to be the moral equivalent of defining subroutines in the Perl\nlanguage itself. You may sell such an object file as proprietary provided that you provide or\noffer to provide the Perl source, as specified by the GNU General Public License. (This is\nmerely an alternate way of specifying input to the program.) You may also sell a binary produced\nby the dumping of a running Perl script that belongs to you, provided that you provide or offer\nto provide the Perl source as specified by the GPL. (The fact that a Perl interpreter and your\ncode are in the same binary file is, in this case, a form of mere aggregation.) This is my\ninterpretation of the GPL. If you still have concerns or difficulties understanding my intent,\nfeel free to contact me. Of course, the Artistic License spells all this out for your\nprotection, so you may prefer to use that.\n",
            "subsections": []
        }
    },
    "summary": "SelfLoader - load functions only on demand",
    "flags": [],
    "examples": [],
    "see_also": []
}