{
    "mode": "perldoc",
    "parameter": "B",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/B/json",
    "generated": "2026-07-05T15:54:53Z",
    "synopsis": "use B;",
    "sections": {
        "NAME": {
            "content": "B - The Perl Compiler Backend\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use B;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"B\" module supplies classes which allow a Perl program to delve into its own innards. It is\nthe module used to implement the \"backends\" of the Perl compiler. Usage of the compiler does not\nrequire knowledge of this module: see the O module for the user-visible part. The \"B\" module is\nof use to those who want to write new compiler backends. This documentation assumes that the\nreader knows a fair amount about perl's internals including such things as SVs, OPs and the\ninternal symbol table and syntax tree of a program.\n",
            "subsections": []
        },
        "OVERVIEW": {
            "content": "The \"B\" module contains a set of utility functions for querying the current state of the Perl\ninterpreter; typically these functions return objects from the B::SV and B::OP classes, or their\nderived classes. These classes in turn define methods for querying the resulting objects about\ntheir own internal state.\n",
            "subsections": []
        },
        "Utility Functions": {
            "content": "The \"B\" module exports a variety of functions: some are simple utility functions, others provide\na Perl program with a way to get an initial \"handle\" on an internal object.\n\nFunctions Returning \"B::SV\", \"B::AV\", \"B::HV\", and \"B::CV\" objects\nFor descriptions of the class hierarchy of these objects and the methods that can be called on\nthem, see below, \"OVERVIEW OF CLASSES\" and \"SV-RELATED CLASSES\".\n\nsvundef\nReturns the SV object corresponding to the C variable \"svundef\".\n\nsvyes\nReturns the SV object corresponding to the C variable \"svyes\".\n\nsvno\nReturns the SV object corresponding to the C variable \"svno\".\n",
            "subsections": [
                {
                    "name": "svref_2object",
                    "content": "Takes a reference to any Perl value, and turns the referred-to value into an object in the\nappropriate B::OP-derived or B::SV-derived class. Apart from functions such as \"mainroot\",\nthis is the primary way to get an initial \"handle\" on an internal perl data structure which\ncan then be followed with the other access methods.\n\nThe returned object will only be valid as long as the underlying OPs and SVs continue to\nexist. Do not attempt to use the object after the underlying structures are freed.\n\namagicgeneration\nReturns the SV object corresponding to the C variable \"amagicgeneration\". As of Perl 5.18,\nthis is just an alias to \"PLna\", so its value is meaningless.\n\ninitav\nReturns the AV object (i.e. in class B::AV) representing INIT blocks.\n\ncheckav\nReturns the AV object (i.e. in class B::AV) representing CHECK blocks.\n\nunitcheckav\nReturns the AV object (i.e. in class B::AV) representing UNITCHECK blocks.\n\nbeginav\nReturns the AV object (i.e. in class B::AV) representing BEGIN blocks.\n\nendav\nReturns the AV object (i.e. in class B::AV) representing END blocks.\n\ncomppadlist\nReturns the PADLIST object (i.e. in class B::PADLIST) of the global comppadlist. In Perl\n5.16 and earlier it returns an AV object (class B::AV).\n\nregexpadav\nOnly when perl was compiled with ithreads.\n\nmaincv\nReturn the (faked) CV corresponding to the main part of the Perl program.\n"
                },
                {
                    "name": "Functions for Examining the Symbol Table",
                    "content": ""
                },
                {
                    "name": "walksymtable",
                    "content": "Walk the symbol table starting at SYMREF and call METHOD on each symbol (a B::GV object)\nvisited. When the walk reaches package symbols (such as \"Foo::\") it invokes RECURSE, passing\nin the symbol name, and only recurses into the package if that sub returns true.\n\nPREFIX is the name of the SYMREF you're walking.\n\nFor example:\n\n# Walk CGI's symbol table calling printsubs on each symbol.\n# Recurse only into CGI::Util::\nwalksymtable(\\%CGI::, 'printsubs',\nsub { $[0] eq 'CGI::Util::' }, 'CGI::');\n\nprintsubs() is a B::GV method you have declared. Also see \"B::GV Methods\", below.\n\nFunctions Returning \"B::OP\" objects or for walking op trees\nFor descriptions of the class hierarchy of these objects and the methods that can be called on\nthem, see below, \"OVERVIEW OF CLASSES\" and \"OP-RELATED CLASSES\".\n\nmainroot\nReturns the root op (i.e. an object in the appropriate B::OP-derived class) of the main part\nof the Perl program.\n\nmainstart\nReturns the starting op of the main part of the Perl program.\n"
                },
                {
                    "name": "walkoptree",
                    "content": "Does a tree-walk of the syntax tree based at OP and calls METHOD on each op it visits. Each\nnode is visited before its children. If \"walkoptreedebug\" (see below) has been called to\nturn debugging on then the method \"walkoptreedebug\" is called on each op before METHOD is\ncalled.\n"
                },
                {
                    "name": "walkoptree_debug",
                    "content": "Returns the current debugging flag for \"walkoptree\". If the optional DEBUG argument is\nnon-zero, it sets the debugging flag to that. See the description of \"walkoptree\" above for\nwhat the debugging flag does.\n"
                },
                {
                    "name": "Miscellaneous Utility Functions",
                    "content": ""
                },
                {
                    "name": "ppname",
                    "content": "Return the PP function name (e.g. \"ppadd\") of op number OPNUM.\n"
                },
                {
                    "name": "hash",
                    "content": "Returns a string in the form \"0x...\" representing the value of the internal hash function\nused by perl on string STR.\n"
                },
                {
                    "name": "cast_I32",
                    "content": "Casts I to the internal I32 type used by that perl.\n\nminusc\nDoes the equivalent of the \"-c\" command-line option. Obviously, this is only useful in a\nBEGIN block or else the flag is set too late.\n"
                },
                {
                    "name": "cstring",
                    "content": "Returns a double-quote-surrounded escaped version of STR which can be used as a string in C\nsource code.\n"
                },
                {
                    "name": "perlstring",
                    "content": "Returns a double-quote-surrounded escaped version of STR which can be used as a string in\nPerl source code.\n"
                },
                {
                    "name": "safename",
                    "content": "This function returns the string with the first character modified if it is a control\ncharacter. It converts it to ^X format first, so that \"\\cG\" becomes \"^G\". This is used\ninternally by B::GV::SAFENAME, but you can call it directly.\n\nclass(OBJ)\nReturns the class of an object without the part of the classname preceding the first \"::\".\nThis is used to turn \"B::UNOP\" into \"UNOP\" for example.\n\nthreadsvnames\nThis used to provide support for the old 5.005 threading module. It now does nothing.\n"
                },
                {
                    "name": "Exported utility variables",
                    "content": "@optype\nmy $optype = $optype[$optypenum];\n\nA simple mapping of the op type number to its type (like 'COP' or 'BINOP').\n\n@specialsvname\nmy $svname = $specialsvname[$svindex];\n\nCertain SV types are considered 'special'. They're represented by B::SPECIAL and are\nreferred to by a number from the specialsvlist. This array maps that number back to the\nname of the SV (like 'Nullsv' or '&PLsvundef').\n"
                }
            ]
        },
        "OVERVIEW OF CLASSES": {
            "content": "The C structures used by Perl's internals to hold SV and OP information (PVIV, AV, HV, ..., OP,\nSVOP, UNOP, ...) are modelled on a class hierarchy and the \"B\" module gives access to them via a\ntrue object hierarchy. Structure fields which point to other objects (whether types of SV or\ntypes of OP) are represented by the \"B\" module as Perl objects of the appropriate class.\n\nThe bulk of the \"B\" module is the methods for accessing fields of these structures.\n\nNote that all access is read-only. You cannot modify the internals by using this module. Also,\nnote that the B::OP and B::SV objects created by this module are only valid for as long as the\nunderlying objects exist; their creation doesn't increase the reference counts of the underlying\nobjects. Trying to access the fields of a freed object will give incomprehensible results, or\nworse.\n\nSV-RELATED CLASSES\nB::IV, B::NV, B::PV, B::PVIV, B::PVNV, B::PVMG, B::PVLV, B::AV, B::HV, B::CV, B::GV, B::FM,\nB::IO. These classes correspond in the obvious way to the underlying C structures of similar\nnames. The inheritance hierarchy mimics the underlying C \"inheritance\":\n\nB::SV\n|\n+------------+------------+\n|            |            |\nB::PV        B::IV        B::NV\n/  \\         /           /\n/    \\       /           /\nB::INVLIST  B::PVIV           /\n\\           /\n\\         /\n\\       /\nB::PVNV\n|\n|\nB::PVMG\n|\n+-------+-------+---+---+-------+-------+\n|       |       |       |       |       |\nB::AV   B::GV   B::HV   B::CV   B::IO B::REGEXP\n|               |\n|               |\nB::PVLV          B::FM\n\nAccess methods correspond to the underlying C macros for field access, usually with the leading\n\"class indication\" prefix removed (Sv, Av, Hv, ...). The leading prefix is only left in cases\nwhere its removal would cause a clash in method name. For example, \"GvREFCNT\" stays as-is since\nits abbreviation would clash with the \"superclass\" method \"REFCNT\" (corresponding to the C\nfunction \"SvREFCNT\").\n\nB::SV Methods\nREFCNT\nFLAGS\nobject2svref\nReturns a reference to the regular scalar corresponding to this B::SV object. In other\nwords, this method is the inverse operation to the svref2object() subroutine. This scalar\nand other data it points at should be considered read-only: modifying them is neither safe\nnor guaranteed to have a sensible effect.\n\nB::IV Methods\nIV  Returns the value of the IV, *interpreted as a signed integer*. This will be misleading if\n\"FLAGS & SVfIVisUV\". Perhaps you want the \"intvalue\" method instead?\n\nIVX\nUVX\nintvalue\nThis method returns the value of the IV as an integer. It differs from \"IV\" in that it\nreturns the correct value regardless of whether it's stored signed or unsigned.\n\nneeds64bits\npackiv\n\nB::NV Methods\nNV\nNVX\nCOPSEQRANGELOW\nCOPSEQRANGEHIGH\nThese last two are only valid for pad name SVs. They only existed in the B::NV class before\nPerl 5.22. In 5.22 they were moved to the B::PADNAME class.\n\nB::RV Methods\nRV\n\nB::PV Methods\nPV  This method is the one you usually want. It constructs a string using the length and offset\ninformation in the struct: for ordinary scalars it will return the string that you'd see\nfrom Perl, even if it contains null characters.\n\nRV  Same as B::RV::RV, except that it will die() if the PV isn't a reference.\n\nPVX This method is less often useful. It assumes that the string stored in the struct is\nnull-terminated, and disregards the length information.\n\nIt is the appropriate method to use if you need to get the name of a lexical variable from a\npadname array. Lexical variable names are always stored with a null terminator, and the\nlength field (CUR) is overloaded for other purposes and can't be relied on here.\n\nCUR This method returns the internal length field, which consists of the number of internal\nbytes, not necessarily the number of logical characters.\n\nLEN This method returns the number of bytes allocated (via malloc) for storing the string. This\nis 0 if the scalar does not \"own\" the string.\n\nB::PVMG Methods\nMAGIC\nSvSTASH\n\nB::MAGIC Methods\nMOREMAGIC\nprecomp\nOnly valid on r-magic, returns the string that generated the regexp.\n\nPRIVATE\nTYPE\nFLAGS\nOBJ Will die() if called on r-magic.\n\nPTR\nREGEX\nOnly valid on r-magic, returns the integer value of the REGEX stored in the MAGIC.\n\nB::INVLIST Methods\nprevindex\nReturns the cache result of previous invlistsearch() (internal usage)\n\nisoffset\nReturns a boolean value (0 or 1) to know if the invlist is using an offset. When false the\nlist begins with the code point U+0000. When true the list begins with the following\nelements.\n\narraylen\nReturns an integer with the size of the array used to define the invlist.\n\ngetinvlistarray\nThis method returns a list of integers representing the array used by the invlist. Note:\nthis cannot be used while in middle of iterating on an invlist and croaks.\n\nB::PVLV Methods\nTARGOFF\nTARGLEN\nTYPE\nTARG\n\nB::BM Methods\nUSEFUL\nPREVIOUS\nRARE\nTABLE\n\nB::REGEXP Methods\nREGEX\nprecomp\nqranoncv\ncompflags\nThe last two were added in Perl 5.22.\n\nB::GV Methods\nisempty\nThis method returns TRUE if the GP field of the GV is NULL.\n\nNAME\nSAFENAME\nThis method returns the name of the glob, but if the first character of the name is a\ncontrol character, then it converts it to ^X first, so that *^G would return \"^G\" rather\nthan \"\\cG\".\n\nIt's useful if you want to print out the name of a variable. If you restrict yourself to\nglobs which exist at compile-time then the result ought to be unambiguous, because code like\n\"${\"^G\"} = 1\" is compiled as two ops - a constant string and a dereference (rv2gv) - so that\nthe glob is created at runtime.\n\nIf you're working with globs at runtime, and need to disambiguate *^G from *{\"^G\"}, then you\nshould use the raw NAME method.\n\nSTASH\nSV\nIO\nFORM\nAV\nHV\nEGV\nCV\nCVGEN\nLINE\nFILE\nFILEGV\nGvREFCNT\nFLAGS\nGPFLAGS\nThis last one is present only in perl 5.22.0 and higher.\n\nB::IO Methods\nB::IO objects derive from IO objects and you will get more information from the IO object\nitself.\n\nFor example:\n\n$gvio = B::svref2object(\\*main::stdin)->IO;\n$IO = $gvio->object2svref();\n$fd = $IO->fileno();\n\nLINES\nPAGE\nPAGELEN\nLINESLEFT\nTOPNAME\nTOPGV\nFMTNAME\nFMTGV\nBOTTOMNAME\nBOTTOMGV\nSUBPROCESS\nIoTYPE\nA character symbolizing the type of IO Handle.\n\n-     STDIN/OUT\nI     STDIN/OUT/ERR\n<     read-only\n>     write-only\na     append\n+     read and write\ns     socket\n|     pipe\nI     IMPLICIT\n#     NUMERIC\nspace closed handle\n\\0    closed internal handle\n\nIoFLAGS\nIsSTD\nTakes one argument ( 'stdin' | 'stdout' | 'stderr' ) and returns true if the IoIFP of the\nobject is equal to the handle whose name was passed as argument; i.e., $io->IsSTD('stderr')\nis true if IoIFP($io) == PerlIOstderr().\n\nB::AV Methods\nFILL\nMAX\nARRAY\nARRAYelt\nLike \"ARRAY\", but takes an index as an argument to get only one element, rather than a list\nof all of them.\n\nB::CV Methods\nSTASH\nSTART\nROOT\nGV\nFILE\nDEPTH\nPADLIST\nReturns a B::PADLIST object.\n\nOUTSIDE\nOUTSIDESEQ\nXSUB\nXSUBANY\nFor constant subroutines, returns the constant SV returned by the subroutine.\n\nCvFLAGS\nconstsv\nNAMEHEK\nReturns the name of a lexical sub, otherwise \"undef\".\n\nB::HV Methods\nFILL\nMAX\nKEYS\nRITER\nNAME\nARRAY\n\nOP-RELATED CLASSES\n\"B::OP\", \"B::UNOP\", \"B::UNOPAUX\", \"B::BINOP\", \"B::LOGOP\", \"B::LISTOP\", \"B::PMOP\", \"B::SVOP\",\n\"B::PADOP\", \"B::PVOP\", \"B::LOOP\", \"B::COP\", \"B::METHOP\".\n\nThese classes correspond in the obvious way to the underlying C structures of similar names. The\ninheritance hierarchy mimics the underlying C \"inheritance\":\n\nB::OP\n|\n+----------+---------+--------+-------+---------+\n|          |         |        |       |         |\nB::UNOP    B::SVOP  B::PADOP  B::COP  B::PVOP  B::METHOP\n|\n+---+---+---------+\n|       |         |\nB::BINOP  B::LOGOP  B::UNOPAUX\n|\n|\nB::LISTOP\n|\n+---+---+\n|       |\nB::LOOP   B::PMOP\n\nAccess methods correspond to the underlying C structure field names, with the leading \"class\nindication\" prefix (\"op\") removed.\n\nB::OP Methods\nThese methods get the values of similarly named fields within the OP data structure. See top of\n\"op.h\" for more info.\n\nnext\nsibling\nparent\nReturns the OP's parent. If it has no parent, or if your perl wasn't built with\n\"-DPERLOPPARENT\", returns NULL.\n\nNote that the global variable $B::OP::doesparent is undefined on older perls that don't\nsupport the \"parent\" method, is defined but false on perls that support the method but were\nbuilt without \"-DPERLOPPARENT\", and is true otherwise.\n\nname\nThis returns the op name as a string (e.g. \"add\", \"rv2av\").\n\nppaddr\nThis returns the function name as a string (e.g. \"PLppaddr[OPADD]\",\n\"PLppaddr[OPRV2AV]\").\n\ndesc\nThis returns the op description from the global C PLopdesc array (e.g. \"addition\" \"array\nderef\").\n\ntarg\ntype\nopt\nflags\nprivate\nspare\n\nB::UNOP Method\nfirst\n\nB::UNOPAUX Methods (since 5.22)",
            "subsections": [
                {
                    "name": "aux_list",
                    "content": "This returns a list of the elements of the op's aux data structure, or a null list if there\nis no aux. What will be returned depends on the object's type, but will typically be a\ncollection of \"B::IV\", \"B::GV\", etc. objects. \"cv\" is the \"B::CV\" object representing the\nsub that the op is contained within.\n"
                },
                {
                    "name": "string",
                    "content": "This returns a textual representation of the object (likely to b useful for deparsing and\ndebugging), or an empty string if the op type doesn't support this. \"cv\" is the \"B::CV\"\nobject representing the sub that the op is contained within.\n\nB::BINOP Method\nlast\n\nB::LOGOP Method\nother\n\nB::LISTOP Method\nchildren\n\nB::PMOP Methods\npmreplroot\npmreplstart\npmflags\nprecomp\npmoffset\nOnly when perl was compiled with ithreads.\n\ncodelist\nSince perl 5.17.1\n\npmregexp\nAdded in perl 5.22, this method returns the B::REGEXP associated with the op. While PMOPs do\nnot actually have \"pmregexp\" fields under threaded builds, this method returns the regexp\nunder threads nonetheless, for convenience.\n\nB::SVOP Methods\nsv\ngv\n\nB::PADOP Method\npadix\n\nB::PVOP Method\npv\n\nB::LOOP Methods\nredoop\nnextop\nlastop\n\nB::COP Methods\nThe \"B::COP\" class is used for \"nextstate\" and \"dbstate\" ops. As of Perl 5.22, it is also used\nfor \"null\" ops that started out as COPs.\n\nlabel\nstash\nstashpv\nstashoff (threaded only)\nfile\ncopseq\nline\nwarnings\nio\nhints\nhintshash\n\nB::METHOP Methods (Since Perl 5.22)\nfirst\nmethsv\n\nPAD-RELATED CLASSES\nPerl 5.18 introduced a new class, B::PADLIST, returned by B::CV's \"PADLIST\" method.\n\nPerl 5.22 introduced the B::PADNAMELIST and B::PADNAME classes.\n\nB::PADLIST Methods\nMAX\nARRAY\nA list of pads. The first one is a B::PADNAMELIST containing the names. The rest are\ncurrently B::AV objects, but that could change in future versions.\n\nARRAYelt\nLike \"ARRAY\", but takes an index as an argument to get only one element, rather than a list\nof all of them.\n\nNAMES\nThis method, introduced in 5.22, returns the B::PADNAMELIST. It is equivalent to \"ARRAYelt\"\nwith a 0 argument.\n\nREFCNT\nid  This method, introduced in 5.22, returns an ID shared by clones of the same padlist.\n\noutid\nThis method, also added in 5.22, returns the ID of the outer padlist.\n\nB::PADNAMELIST Methods\nMAX\nARRAY\nARRAYelt\nThese two methods return the pad names, using B::SPECIAL objects for null pointers and\nB::PADNAME objects otherwise.\n\nREFCNT\n\nB::PADNAME Methods\nPV\nPVX\nLEN\nREFCNT\nFLAGS\nFor backward-compatibility, if the PADNAMEtOUTER flag is set, the FLAGS method adds the\nSVfFAKE flag, too.\n\nTYPE\nA B::HV object representing the stash for a typed lexical.\n\nSvSTASH\nA backward-compatibility alias for TYPE.\n\nOURSTASH\nA B::HV object representing the stash for 'our' variables.\n\nPROTOCV\nThe prototype CV for a 'my' sub.\n\nCOPSEQRANGELOW\nCOPSEQRANGEHIGH\nSequence numbers representing the scope within which a lexical is visible. Meaningless if\nPADNAMEtOUTER is set.\n\nPARENTPADINDEX\nOnly meaningful if PADNAMEtOUTER is set.\n\nPARENTFAKELEXFLAGS\nOnly meaningful if PADNAMEtOUTER is set.\n\n$B::overlay\nAlthough the optree is read-only, there is an overlay facility that allows you to override what\nvalues the various B::*OP methods return for a particular op. $B::overlay should be set to\nreference a two-deep hash: indexed by OP address, then method name. Whenever a an op method is\ncalled, the value in the hash is returned if it exists. This facility is used by B::Deparse to\n\"undo\" some optimisations. For example:\n\nlocal $B::overlay = {};\n...\nif ($op->name eq \"foo\") {\n$B::overlay->{$$op} = {\nname => 'bar',\nnext => $op->next->next,\n};\n}\n...\n$op->name # returns \"bar\"\n$op->next # returns the next op but one\n"
                }
            ]
        },
        "AUTHOR": {
            "content": "Malcolm Beattie, \"mbeattie@sable.ox.ac.uk\"\n",
            "subsections": []
        }
    },
    "summary": "B - The Perl Compiler Backend",
    "flags": [],
    "examples": [],
    "see_also": []
}