CINXE.COM
constant - Perl pragma to declare constants - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>constant - Perl pragma to declare constants - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/constant"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.39.2</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/constant">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/constant">5.40.1</a> <a class="dropdown-item" href="/5.40.0/constant">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/constant">5.38.3</a> <a class="dropdown-item" href="/5.38.2/constant">5.38.2</a> <a class="dropdown-item" href="/5.38.1/constant">5.38.1</a> <a class="dropdown-item" href="/5.38.0/constant">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/constant">5.36.3</a> <a class="dropdown-item" href="/5.36.2/constant">5.36.2</a> <a class="dropdown-item" href="/5.36.1/constant">5.36.1</a> <a class="dropdown-item" href="/5.36.0/constant">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/constant">5.34.3</a> <a class="dropdown-item" href="/5.34.2/constant">5.34.2</a> <a class="dropdown-item" href="/5.34.1/constant">5.34.1</a> <a class="dropdown-item" href="/5.34.0/constant">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/constant">5.32.1</a> <a class="dropdown-item" href="/5.32.0/constant">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/constant">5.30.3</a> <a class="dropdown-item" href="/5.30.2/constant">5.30.2</a> <a class="dropdown-item" href="/5.30.1/constant">5.30.1</a> <a class="dropdown-item" href="/5.30.0/constant">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/constant">5.28.3</a> <a class="dropdown-item" href="/5.28.2/constant">5.28.2</a> <a class="dropdown-item" href="/5.28.1/constant">5.28.1</a> <a class="dropdown-item" href="/5.28.0/constant">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/constant">5.26.3</a> <a class="dropdown-item" href="/5.26.2/constant">5.26.2</a> <a class="dropdown-item" href="/5.26.1/constant">5.26.1</a> <a class="dropdown-item" href="/5.26.0/constant">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/constant">5.24.4</a> <a class="dropdown-item" href="/5.24.3/constant">5.24.3</a> <a class="dropdown-item" href="/5.24.2/constant">5.24.2</a> <a class="dropdown-item" href="/5.24.1/constant">5.24.1</a> <a class="dropdown-item" href="/5.24.0/constant">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/constant">5.22.4</a> <a class="dropdown-item" href="/5.22.3/constant">5.22.3</a> <a class="dropdown-item" href="/5.22.2/constant">5.22.2</a> <a class="dropdown-item" href="/5.22.1/constant">5.22.1</a> <a class="dropdown-item" href="/5.22.0/constant">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/constant">5.20.3</a> <a class="dropdown-item" href="/5.20.2/constant">5.20.2</a> <a class="dropdown-item" href="/5.20.1/constant">5.20.1</a> <a class="dropdown-item" href="/5.20.0/constant">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/constant">5.18.4</a> <a class="dropdown-item" href="/5.18.3/constant">5.18.3</a> <a class="dropdown-item" href="/5.18.2/constant">5.18.2</a> <a class="dropdown-item" href="/5.18.1/constant">5.18.1</a> <a class="dropdown-item" href="/5.18.0/constant">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/constant">5.16.3</a> <a class="dropdown-item" href="/5.16.2/constant">5.16.2</a> <a class="dropdown-item" href="/5.16.1/constant">5.16.1</a> <a class="dropdown-item" href="/5.16.0/constant">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/constant">5.14.4</a> <a class="dropdown-item" href="/5.14.3/constant">5.14.3</a> <a class="dropdown-item" href="/5.14.2/constant">5.14.2</a> <a class="dropdown-item" href="/5.14.1/constant">5.14.1</a> <a class="dropdown-item" href="/5.14.0/constant">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/constant">5.12.5</a> <a class="dropdown-item" href="/5.12.4/constant">5.12.4</a> <a class="dropdown-item" href="/5.12.3/constant">5.12.3</a> <a class="dropdown-item" href="/5.12.2/constant">5.12.2</a> <a class="dropdown-item" href="/5.12.1/constant">5.12.1</a> <a class="dropdown-item" href="/5.12.0/constant">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/constant">5.10.1</a> <a class="dropdown-item" href="/5.10.0/constant">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/constant">5.8.9</a> <a class="dropdown-item" href="/5.8.8/constant">5.8.8</a> <a class="dropdown-item" href="/5.8.7/constant">5.8.7</a> <a class="dropdown-item" href="/5.8.6/constant">5.8.6</a> <a class="dropdown-item" href="/5.8.5/constant">5.8.5</a> <a class="dropdown-item" href="/5.8.4/constant">5.8.4</a> <a class="dropdown-item" href="/5.8.3/constant">5.8.3</a> <a class="dropdown-item" href="/5.8.2/constant">5.8.2</a> <a class="dropdown-item" href="/5.8.1/constant">5.8.1</a> <a class="dropdown-item" href="/5.8.0/constant">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/constant">5.6.2</a> <a class="dropdown-item" href="/5.6.1/constant">5.6.1</a> <a class="dropdown-item" href="/5.6.0/constant">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/constant">5.005_04</a> <a class="dropdown-item" href="/5.005_03/constant">5.005_03</a> <a class="dropdown-item" href="/5.005_02/constant">5.005_02</a> <a class="dropdown-item" href="/5.005_01/constant">5.005_01</a> <a class="dropdown-item" href="/5.005/constant">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/constant">blead</a> <a class="dropdown-item" href="/5.41.9/constant">5.41.9</a> <a class="dropdown-item" href="/5.41.8/constant">5.41.8</a> <a class="dropdown-item" href="/5.41.7/constant">5.41.7</a> <a class="dropdown-item" href="/5.41.6/constant">5.41.6</a> <a class="dropdown-item" href="/5.41.5/constant">5.41.5</a> <a class="dropdown-item" href="/5.41.4/constant">5.41.4</a> <a class="dropdown-item" href="/5.41.3/constant">5.41.3</a> <a class="dropdown-item" href="/5.41.2/constant">5.41.2</a> <a class="dropdown-item" href="/5.41.1/constant">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/constant">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/constant">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/constant">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/constant">5.39.10</a> <a class="dropdown-item" href="/5.39.9/constant">5.39.9</a> <a class="dropdown-item" href="/5.39.8/constant">5.39.8</a> <a class="dropdown-item" href="/5.39.7/constant">5.39.7</a> <a class="dropdown-item" href="/5.39.6/constant">5.39.6</a> <a class="dropdown-item" href="/5.39.5/constant">5.39.5</a> <a class="dropdown-item" href="/5.39.4/constant">5.39.4</a> <a class="dropdown-item" href="/5.39.3/constant">5.39.3</a> <a class="dropdown-item active" href="/5.39.2/constant">5.39.2</a> <a class="dropdown-item" href="/5.39.1/constant">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/constant">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.39.2/perl">Perl</a> <a class="dropdown-item" href="/5.39.2/perlintro">Intro</a> <a class="dropdown-item" href="/5.39.2/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.39.2/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.39.2/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.2/perlop">Operators</a> <a class="dropdown-item" href="/5.39.2/functions">Functions</a> <a class="dropdown-item" href="/5.39.2/variables">Variables</a> <a class="dropdown-item" href="/5.39.2/modules">Modules</a> <a class="dropdown-item" href="/5.39.2/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.2/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.39.2/perlcommunity">Community</a> <a class="dropdown-item" href="/5.39.2/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.39.2/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.39.2/constant">constant</a> <div id="more"> (<a href="/5.39.2/constant.txt">source</a>, <a href="https://metacpan.org/pod/constant">CPAN</a>) </div> <div id="moduleversion">version 1.33</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.39.2. This is a development version of Perl. </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#NOTES">NOTES</a> <ul> <li> <a class="text-decoration-none" href="#List-constants">List constants</a> </li> <li> <a class="text-decoration-none" href="#Defining-multiple-constants-at-once">Defining multiple constants at once</a> </li> <li> <a class="text-decoration-none" href="#Magic-constants">Magic constants</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#TECHNICAL-NOTES">TECHNICAL NOTES</a> </li> <li> <a class="text-decoration-none" href="#CAVEATS">CAVEATS</a> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> <li> <a class="text-decoration-none" href="#BUGS">BUGS</a> </li> <li> <a class="text-decoration-none" href="#AUTHORS">AUTHORS</a> </li> <li> <a class="text-decoration-none" href="#COPYRIGHT-&-LICENSE">COPYRIGHT & LICENSE</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>constant - Perl pragma to declare constants</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use constant PI => 4 * atan2(1, 1); use constant DEBUG => 0; print "Pi equals ", PI, "...\n" if DEBUG; use constant { SEC => 0, MIN => 1, HOUR => 2, MDAY => 3, MON => 4, YEAR => 5, WDAY => 6, YDAY => 7, ISDST => 8, }; use constant WEEKDAYS => qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); print "Today is ", (WEEKDAYS)[ (localtime)[WDAY] ], ".\n";</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>This pragma allows you to declare constants at compile-time.</p> <p>When you declare a constant such as <code>PI</code> using the method shown above, each machine your script runs upon can have as many digits of accuracy as it can use. Also, your program will be easier to read, more likely to be maintained (and maintained correctly), and far less likely to send a space probe to the wrong planet because nobody noticed the one equation in which you wrote <code>3.14195</code>.</p> <p>When a constant is used in an expression, Perl replaces it with its value at compile time, and may then optimize the expression further. In particular, any code in an <code>if (CONSTANT)</code> block will be optimized away if the constant is false.</p> <h1 id="NOTES"><a class="permalink" href="#NOTES">#</a>NOTES</h1> <p>As with all <code>use</code> directives, defining a constant happens at compile time. Thus, it's probably not correct to put a constant declaration inside of a conditional statement (like <code>if ($foo) { use constant ... }</code>).</p> <p>Constants defined using this module cannot be interpolated into strings like variables. However, concatenation works just fine:</p> <pre><code>print "Pi equals PI...\n"; # WRONG: does not expand "PI" print "Pi equals ".PI."...\n"; # right</code></pre> <p>Even though a reference may be declared as a constant, the reference may point to data which may be changed, as this code shows.</p> <pre><code>use constant ARRAY => [ 1,2,3,4 ]; print ARRAY->[1]; ARRAY->[1] = " be changed"; print ARRAY->[1];</code></pre> <p>Constants belong to the package they are defined in. To refer to a constant defined in another package, specify the full package name, as in <code>Some::Package::CONSTANT</code>. Constants may be exported by modules, and may also be called as either class or instance methods, that is, as <code>Some::Package->CONSTANT</code> or as <code>$obj->CONSTANT</code> where <code>$obj</code> is an instance of <code>Some::Package</code>. Subclasses may define their own constants to override those in their base class.</p> <p>As of version 1.32 of this module, constants can be defined in packages other than the caller, by including the package name in the name of the constant:</p> <pre><code>use constant "OtherPackage::FWIBBLE" => 7865; constant->import("Other::FWOBBLE",$value); # dynamically at run time</code></pre> <p>The use of all caps for constant names is merely a convention, although it is recommended in order to make constants stand out and to help avoid collisions with other barewords, keywords, and subroutine names. Constant names must begin with a letter or underscore. Names beginning with a double underscore are reserved. Some poor choices for names will generate warnings, if warnings are enabled at compile time.</p> <h2 id="List-constants"><a class="permalink" href="#List-constants">#</a><a id="List"></a>List constants</h2> <p>Constants may be lists of more (or less) than one value. A constant with no values evaluates to <code>undef</code> in scalar context. Note that constants with more than one value do <i>not</i> return their last value in scalar context as one might expect. They currently return the number of values, but <b>this may change in the future</b>. Do not use constants with multiple values in scalar context.</p> <p><b>NOTE:</b> This implies that the expression defining the value of a constant is evaluated in list context. This may produce surprises:</p> <pre><code>use constant TIMESTAMP => localtime; # WRONG! use constant TIMESTAMP => scalar localtime; # right</code></pre> <p>The first line above defines <code>TIMESTAMP</code> as a 9-element list, as returned by <code>localtime()</code> in list context. To set it to the string returned by <code>localtime()</code> in scalar context, an explicit <code>scalar</code> keyword is required.</p> <p>List constants are lists, not arrays. To index or slice them, they must be placed in parentheses.</p> <pre><code>my @workdays = WEEKDAYS[1 .. 5]; # WRONG! my @workdays = (WEEKDAYS)[1 .. 5]; # right</code></pre> <h2 id="Defining-multiple-constants-at-once"><a class="permalink" href="#Defining-multiple-constants-at-once">#</a><a id="Defining"></a>Defining multiple constants at once</h2> <p>Instead of writing multiple <code>use constant</code> statements, you may define multiple constants in a single statement by giving, instead of the constant name, a reference to a hash where the keys are the names of the constants to be defined. Obviously, all constants defined using this method must have a single value.</p> <pre><code>use constant { FOO => "A single value", BAR => "This", "won't", "work!", # Error! };</code></pre> <p>This is a fundamental limitation of the way hashes are constructed in Perl. The error messages produced when this happens will often be quite cryptic -- in the worst case there may be none at all, and you'll only later find that something is broken.</p> <p>When defining multiple constants, you cannot use the values of other constants defined in the same declaration. This is because the calling package doesn't know about any constant within that group until <i>after</i> the <code>use</code> statement is finished.</p> <pre><code>use constant { BITMASK => 0xAFBAEBA8, NEGMASK => ~BITMASK, # Error! };</code></pre> <h2 id="Magic-constants"><a class="permalink" href="#Magic-constants">#</a><a id="Magic"></a>Magic constants</h2> <p>Magical values and references can be made into constants at compile time, allowing for way cool stuff like this. (These error numbers aren't totally portable, alas.)</p> <pre><code>use constant E2BIG => ($! = 7); print E2BIG, "\n"; # something like "Arg list too long" print 0+E2BIG, "\n"; # "7"</code></pre> <p>You can't produce a tied constant by giving a tied scalar as the value. References to tied variables, however, can be used as constants without any problems.</p> <h1 id="TECHNICAL-NOTES"><a class="permalink" href="#TECHNICAL-NOTES">#</a><a id="TECHNICAL"></a>TECHNICAL NOTES</h1> <p>In the current implementation, scalar constants are actually inlinable subroutines. As of version 5.004 of Perl, the appropriate scalar constant is inserted directly in place of some subroutine calls, thereby saving the overhead of a subroutine call. See <a href="/5.39.2/perlsub#Constant-Functions">"Constant Functions" in perlsub</a> for details about how and when this happens.</p> <p>In the rare case in which you need to discover at run time whether a particular constant has been declared via this module, you may use this function to examine the hash <code>%constant::declared</code>. If the given constant name does not include a package name, the current package is used.</p> <pre><code>sub declared ($) { use constant 1.01; # don't omit this! my $name = shift; $name =~ s/^::/main::/; my $pkg = caller; my $full_name = $name =~ /::/ ? $name : "${pkg}::$name"; $constant::declared{$full_name}; }</code></pre> <h1 id="CAVEATS"><a class="permalink" href="#CAVEATS">#</a>CAVEATS</h1> <p>List constants are not inlined unless you are using Perl v5.20 or higher. In v5.20 or higher, they are still not read-only, but that may change in future versions.</p> <p>It is not possible to have a subroutine or a keyword with the same name as a constant in the same package. This is probably a Good Thing.</p> <p>A constant with a name in the list <code>STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG</code> is not allowed anywhere but in package <code>main::</code>, for technical reasons.</p> <p>Unlike constants in some languages, these cannot be overridden on the command line or via environment variables.</p> <p>You can get into trouble if you use constants in a context which automatically quotes barewords (as is true for any subroutine call). For example, you can't say <code>$hash{CONSTANT}</code> because <code>CONSTANT</code> will be interpreted as a string. Use <code>$hash{CONSTANT()}</code> or <code>$hash{+CONSTANT}</code> to prevent the bareword quoting mechanism from kicking in. Similarly, since the <code>=></code> operator quotes a bareword immediately to its left, you have to say <code>CONSTANT() => 'value'</code> (or simply use a comma in place of the big arrow) instead of <code>CONSTANT => 'value'</code>.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.39.2/Readonly">Readonly</a> - Facility for creating read-only scalars, arrays, hashes.</p> <p><a href="/5.39.2/Attribute::Constant">Attribute::Constant</a> - Make read-only variables via attribute</p> <p><a href="/5.39.2/Scalar::Readonly">Scalar::Readonly</a> - Perl extension to the <code>SvREADONLY</code> scalar flag</p> <p><a href="/5.39.2/Hash::Util">Hash::Util</a> - A selection of general-utility hash subroutines (mostly to lock/unlock keys and values)</p> <h1 id="BUGS"><a class="permalink" href="#BUGS">#</a>BUGS</h1> <p>Please report any bugs or feature requests via the perlbug(1) utility.</p> <h1 id="AUTHORS"><a class="permalink" href="#AUTHORS">#</a>AUTHORS</h1> <p>Tom Phoenix, <<i>rootbeer@redcat.com</i>>, with help from many other folks.</p> <p>Multiple constant declarations at once added by Casey West, <<i>casey@geeknest.com</i>>.</p> <p>Documentation mostly rewritten by Ilmari Karonen, <<i>perl@itz.pp.sci.fi</i>>.</p> <p>This program is maintained by the Perl 5 Porters. The CPAN distribution is maintained by S茅bastien Aperghis-Tramoni <<i>sebastien@aperghis.net</i>>.</p> <h1 id="COPYRIGHT-&-LICENSE"><a class="permalink" href="#COPYRIGHT-&-LICENSE">#</a><a id="COPYRIGHT"></a><a id="COPYRIGHT-LICENSE"></a>COPYRIGHT & LICENSE</h1> <p>Copyright (C) 1997, 1999 Tom Phoenix</p> <p>This module is free software; you can redistribute it or modify it under the same terms as Perl itself.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>