{
    "content": [
        {
            "type": "text",
            "text": "# constant (man)\n\n## NAME\n\nconstant - Perl pragma to declare constants\n\n## SYNOPSIS\n\nuse constant PI    => 4 * atan2(1, 1);\nuse constant DEBUG => 0;\nprint \"Pi equals \", PI, \"...\\n\" if DEBUG;\nuse constant {\nSEC   => 0,\nMIN   => 1,\nHOUR  => 2,\nMDAY  => 3,\nMON   => 4,\nYEAR  => 5,\nWDAY  => 6,\nYDAY  => 7,\nISDST => 8,\n};\nuse constant WEEKDAYS => qw(\nSunday Monday Tuesday Wednesday Thursday Friday Saturday\n);\nprint \"Today is \", (WEEKDAYS)[ (localtime)[WDAY] ], \".\\n\";\n\n## DESCRIPTION\n\nThis pragma allows you to declare constants at compile-time.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **NOTES** (3 subsections)\n- **TECHNICAL NOTES**\n- **CAVEATS**\n- **SEE ALSO**\n- **BUGS**\n- **AUTHORS** (1 subsections)\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "constant",
        "section": "",
        "mode": "man",
        "summary": "constant - Perl pragma to declare constants",
        "synopsis": "use constant PI    => 4 * atan2(1, 1);\nuse constant DEBUG => 0;\nprint \"Pi equals \", PI, \"...\\n\" if DEBUG;\nuse constant {\nSEC   => 0,\nMIN   => 1,\nHOUR  => 2,\nMDAY  => 3,\nMON   => 4,\nYEAR  => 5,\nWDAY  => 6,\nYDAY  => 7,\nISDST => 8,\n};\nuse constant WEEKDAYS => qw(\nSunday Monday Tuesday Wednesday Thursday Friday Saturday\n);\nprint \"Today is \", (WEEKDAYS)[ (localtime)[WDAY] ], \".\\n\";",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 23,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 12,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 37,
                "subsections": [
                    {
                        "name": "List constants",
                        "lines": 22
                    },
                    {
                        "name": "Defining multiple constants at once",
                        "lines": 23
                    },
                    {
                        "name": "Magic constants",
                        "lines": 10
                    }
                ]
            },
            {
                "name": "TECHNICAL NOTES",
                "lines": 19,
                "subsections": []
            },
            {
                "name": "CAVEATS",
                "lines": 19,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 9,
                "subsections": []
            },
            {
                "name": "BUGS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "AUTHORS",
                "lines": 9,
                "subsections": [
                    {
                        "name": "COPYRIGHT & LICENSE",
                        "lines": 8
                    }
                ]
            }
        ],
        "sections": {
            "NAME": {
                "content": "constant - Perl pragma to declare constants\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "use constant PI    => 4 * atan2(1, 1);\nuse constant DEBUG => 0;\n\nprint \"Pi equals \", PI, \"...\\n\" if DEBUG;\n\nuse constant {\nSEC   => 0,\nMIN   => 1,\nHOUR  => 2,\nMDAY  => 3,\nMON   => 4,\nYEAR  => 5,\nWDAY  => 6,\nYDAY  => 7,\nISDST => 8,\n};\n\nuse constant WEEKDAYS => qw(\nSunday Monday Tuesday Wednesday Thursday Friday Saturday\n);\n\nprint \"Today is \", (WEEKDAYS)[ (localtime)[WDAY] ], \".\\n\";\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This pragma allows you to declare constants at compile-time.\n\nWhen you declare a constant such as \"PI\" using the method shown above, each machine your\nscript runs upon can have as many digits of accuracy as it can use.  Also, your program will\nbe easier to read, more likely to be maintained (and maintained correctly), and far less\nlikely to send a space probe to the wrong planet because nobody noticed the one equation in\nwhich you wrote 3.14195.\n\nWhen a constant is used in an expression, Perl replaces it with its value at compile time,\nand may then optimize the expression further.  In particular, any code in an \"if (CONSTANT)\"\nblock will be optimized away if the constant is false.\n",
                "subsections": []
            },
            "NOTES": {
                "content": "As with all \"use\" directives, defining a constant happens at compile time.  Thus, it's\nprobably not correct to put a constant declaration inside of a conditional statement (like\n\"if ($foo) { use constant ... }\").\n\nConstants defined using this module cannot be interpolated into strings like variables.\nHowever, concatenation works just fine:\n\nprint \"Pi equals PI...\\n\";        # WRONG: does not expand \"PI\"\nprint \"Pi equals \".PI.\"...\\n\";    # right\n\nEven though a reference may be declared as a constant, the reference may point to data which\nmay be changed, as this code shows.\n\nuse constant ARRAY => [ 1,2,3,4 ];\nprint ARRAY->[1];\nARRAY->[1] = \" be changed\";\nprint ARRAY->[1];\n\nConstants belong to the package they are defined in.  To refer to a constant defined in\nanother package, specify the full package name, as in \"Some::Package::CONSTANT\".  Constants\nmay be exported by modules, and may also be called as either class or instance methods, that\nis, as \"Some::Package->CONSTANT\" or as \"$obj->CONSTANT\" where $obj is an instance of\n\"Some::Package\".  Subclasses may define their own constants to override those in their base\nclass.\n\nAs of version 1.32 of this module, constants can be defined in packages other than the\ncaller, by including the package name in the name of the constant:\n\nuse constant \"OtherPackage::FWIBBLE\" => 7865;\nconstant->import(\"Other::FWOBBLE\",$value); # dynamically at run time\n\nThe use of all caps for constant names is merely a convention, although it is recommended in\norder to make constants stand out and to help avoid collisions with other barewords,\nkeywords, and subroutine names.  Constant names must begin with a letter or underscore.\nNames beginning with a double underscore are reserved.  Some poor choices for names will\ngenerate warnings, if warnings are enabled at compile time.\n",
                "subsections": [
                    {
                        "name": "List constants",
                        "content": "Constants may be lists of more (or less) than one value.  A constant with no values evaluates\nto \"undef\" in scalar context.  Note that constants with more than one value do not return\ntheir last value in scalar context as one might expect.  They currently return the number of\nvalues, but this may change in the future.  Do not use constants with multiple values in\nscalar context.\n\nNOTE: This implies that the expression defining the value of a constant is evaluated in list\ncontext.  This may produce surprises:\n\nuse constant TIMESTAMP => localtime;                # WRONG!\nuse constant TIMESTAMP => scalar localtime;         # right\n\nThe first line above defines \"TIMESTAMP\" as a 9-element list, as returned by \"localtime()\" in\nlist context.  To set it to the string returned by \"localtime()\" in scalar context, an\nexplicit \"scalar\" keyword is required.\n\nList constants are lists, not arrays.  To index or slice them, they must be placed in\nparentheses.\n\nmy @workdays = WEEKDAYS[1 .. 5];            # WRONG!\nmy @workdays = (WEEKDAYS)[1 .. 5];          # right\n"
                    },
                    {
                        "name": "Defining multiple constants at once",
                        "content": "Instead of writing multiple \"use constant\" statements, you may define multiple constants in a\nsingle statement by giving, instead of the constant name, a reference to a hash where the\nkeys are the names of the constants to be defined.  Obviously, all constants defined using\nthis method must have a single value.\n\nuse constant {\nFOO => \"A single value\",\nBAR => \"This\", \"won't\", \"work!\",        # Error!\n};\n\nThis is a fundamental limitation of the way hashes are constructed in Perl.  The error\nmessages produced when this happens will often be quite cryptic -- in the worst case there\nmay be none at all, and you'll only later find that something is broken.\n\nWhen defining multiple constants, you cannot use the values of other constants defined in the\nsame declaration.  This is because the calling package doesn't know about any constant within\nthat group until after the \"use\" statement is finished.\n\nuse constant {\nBITMASK => 0xAFBAEBA8,\nNEGMASK => ~BITMASK,                    # Error!\n};\n"
                    },
                    {
                        "name": "Magic constants",
                        "content": "Magical values and references can be made into constants at compile time, allowing for way\ncool stuff like this.  (These error numbers aren't totally portable, alas.)\n\nuse constant E2BIG => ($! = 7);\nprint   E2BIG, \"\\n\";        # something like \"Arg list too long\"\nprint 0+E2BIG, \"\\n\";        # \"7\"\n\nYou can't produce a tied constant by giving a tied scalar as the value.  References to tied\nvariables, however, can be used as constants without any problems.\n"
                    }
                ]
            },
            "TECHNICAL NOTES": {
                "content": "In the current implementation, scalar constants are actually inlinable subroutines.  As of\nversion 5.004 of Perl, the appropriate scalar constant is inserted directly in place of some\nsubroutine calls, thereby saving the overhead of a subroutine call.  See \"Constant Functions\"\nin perlsub for details about how and when this happens.\n\nIn the rare case in which you need to discover at run time whether a particular constant has\nbeen declared via this module, you may use this function to examine the hash\n%constant::declared.  If the given constant name does not include a package name, the current\npackage is used.\n\nsub declared ($) {\nuse constant 1.01;              # don't omit this!\nmy $name = shift;\n$name =~ s/^::/main::/;\nmy $pkg = caller;\nmy $fullname = $name =~ /::/ ? $name : \"${pkg}::$name\";\n$constant::declared{$fullname};\n}\n",
                "subsections": []
            },
            "CAVEATS": {
                "content": "List constants are not inlined unless you are using Perl v5.20 or higher.  In v5.20 or\nhigher, they are still not read-only, but that may change in future versions.\n\nIt is not possible to have a subroutine or a keyword with the same name as a constant in the\nsame package.  This is probably a Good Thing.\n\nA constant with a name in the list \"STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG\" is not\nallowed anywhere but in package \"main::\", for technical reasons.\n\nUnlike constants in some languages, these cannot be overridden on the command line or via\nenvironment variables.\n\nYou can get into trouble if you use constants in a context which automatically quotes\nbarewords (as is true for any subroutine call).  For example, you can't say $hash{CONSTANT}\nbecause \"CONSTANT\" will be interpreted as a string.  Use $hash{CONSTANT()} or\n$hash{+CONSTANT} to prevent the bareword quoting mechanism from kicking in.  Similarly, since\nthe \"=>\" operator quotes a bareword immediately to its left, you have to say \"CONSTANT() =>\n'value'\" (or simply use a comma in place of the big arrow) instead of \"CONSTANT => 'value'\".\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "Readonly - Facility for creating read-only scalars, arrays, hashes.\n\nAttribute::Constant - Make read-only variables via attribute\n\nScalar::Readonly - Perl extension to the \"SvREADONLY\" scalar flag\n\nHash::Util - A selection of general-utility hash subroutines (mostly to lock/unlock keys and\nvalues)\n",
                "subsections": []
            },
            "BUGS": {
                "content": "Please report any bugs or feature requests via the perlbug(1) utility.\n",
                "subsections": []
            },
            "AUTHORS": {
                "content": "Tom Phoenix, <rootbeer@redcat.com>, with help from many other folks.\n\nMultiple constant declarations at once added by Casey West, <casey@geeknest.com>.\n\nDocumentation mostly rewritten by Ilmari Karonen, <perl@itz.pp.sci.fi>.\n\nThis program is maintained by the Perl 5 Porters.  The CPAN distribution is maintained by\nSébastien Aperghis-Tramoni <sebastien@aperghis.net>.\n",
                "subsections": [
                    {
                        "name": "COPYRIGHT & LICENSE",
                        "content": "Copyright (C) 1997, 1999 Tom Phoenix\n\nThis module is free software; you can redistribute it or modify it under the same terms as\nPerl itself.\n\n\n\nperl v5.34.0                                 2026-06-23                              constant(3perl)"
                    }
                ]
            }
        }
    }
}