CINXE.COM

constant - Perl pragma to declare constants - metacpan.org

<!DOCTYPE html> <html lang="en-US"> <head> <title>constant - Perl pragma to declare constants - metacpan.org</title> <link rel="preload" as="fetch" href="/account/login_status" crossorigin="anonymous" /> <link href="/assets/style-XFEQ536G.css" rel="stylesheet" type="text/css"> <script src="/assets/main-WXBSEDQJ.js" type="module"></script> <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of constant - MetaCPAN" href="/dist/constant/releases.rss" /> <link rel="canonical" href="https://metacpan.org/pod/constant" /> <meta name="description" content="Perl pragma to declare constants" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5"> <link rel="shortcut icon" href="/static/icons/favicon.ico"> <link rel="apple-touch-icon" sizes="152x152" href="/static/icons/apple-touch-icon.png"> <link rel="search" href="/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN"> <script async src="https://perl-ads.perlhacks.com/perl-ads.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-E82Q2V8LVD"></script> <meta property="og:site_name" content="MetaCPAN" /> <meta name="twitter:site" content="@metacpan" /> <meta property="og:type" content="article" /> <meta name="twitter:card" content="summary" /> <meta property="og:url" content="https://metacpan.org/pod/constant" /> <meta property="og:title" content="constant" /> <meta property="og:description" content="Perl pragma to declare constants" /> <meta property="og:image" content="https://metacpan.org/static/images/dots.png" /> </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="header-logo-large hidden-xs"> <a href="/" tabindex="0"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#logo" /> </svg> </a> </div> <div class="header-logo-icon visible-xs"> <a href="/"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#dots" /> </svg> </a> </div> <ul class="nav navbar-nav menu-items hidden-xs hidden-sm"> <li><a href="/about">About</a></li> <li><a href="/about/sponsors">Sponsor</a></li> <li><a href="https://grep.metacpan.org/">grep::cpan</a></li> <li><a href="/recent">Recent</a></li> <li><a href="/about/faq">FAQ</a></li> <li><a href="/tools">Tools</a></li> <li><a href="https://fastapi.metacpan.org/">API</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <button type="button" class="searchbar-btn visible-xs visible-sm"> <i class="fa fa-search button-fa-icon"></i> </button> <form action="/search" class="searchbar-form visible-md visible-lg search-form form-horizontal"> <input type="hidden" name="size" id="metacpan_search-size" value="20"> <div class="form-group"> <div class="search-group"> <i class="fa fa-search"></i> <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value=""> </div> </div> </form> <li class="icon-slidepanel visible-xs visible-sm"> <button data-toggle="slidepanel" data-target=".slidepanel"> <span class="button-fa-icon"> <i class="fa fa-bars slidepanel-open"></i> <i class="fa fa-times slidepanel-close"></i> </span> </button> </li> <li class="dropdown login-dropdown show-logged-in"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li><a href="/account/identities">Identities</a></li> <li><a href="/account/profile">Profile</a></li> <li><a href="/account/favorite/list">Favorites</a></li> <li> <a href="#" type="button" class="logout-button"> Logout </a> </li> </ul> </li> <li class="dropdown login-dropdown show-logged-out"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon avatar-placeholder" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li> <a href="/login/github"> <i class="fab fa-github fa-fw"></i> GitHub </a> </li> <li> <a href="/login/twitter"> <i class="fab fa-twitter fa-fw"></i> Twitter </a> </li> <li> <a href="/login/google"> <i class="fab fa-google fa-fw"></i> Google </a> </li> </ul> </li> <li class="dropdown login-dropdown hide-logged-in hide-logged-out"> <button> <i class="fa fa-user button-fa-icon" aria-hidden="true"></i> </button> </li> </ul> </nav> <div class="page-content "> <div id="perl-ad-target" class="top-notify-banner perl-ad-target"> </div> <nav class="sidebar"> <div class="slidepanel"> <ul class="nav-list "> <li class="nav-header no-margin-top"> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The date that this version of constant was released."> <span class="relatize">08 Jun 2015 21:47:40 UTC</span> </div> </li> <li> Distribution: <a href="/dist/constant">constant</a> </li> <li> Module version: 1.33 </li> <li> <a data-keyboard-shortcut="g s" href="/dist/constant/source/lib/constant.pm">Source</a> (<a href="/dist/constant/source/lib/constant.pm?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g b" href="/dist/constant/source/lib">Browse</a> (<a href="/dist/constant/source/lib?raw=1">raw</a>) </li> <li> <a data-keyboard-shortcut="g c" href="/dist/constant/changes">Changes</a> </li> <li> <a rel="noopener nofollow" class="nopopup" href="https://metacpan.org/module/constant">Homepage</a> </li> <li> <a class="nopopup" href="/dist/constant/contribute">How to Contribute</a> </li> <li> <a rel="noopener nofollow" href="git://perl5.git.perl.org/perl.git">Clone git repository</a> </li> <li> <a rel="noopener nofollow" data-keyboard-shortcut="g i" href="https://rt.perl.org/rt3/Search/Results.html?Query=Queue=&#39;perl5&#39; AND Content LIKE &#39;module=constant&#39; AND (Status=&#39;open&#39; OR Status=&#39;new&#39; OR Status=&#39;stalled&#39;)">Issues</a> </li> <li> <a rel="noopener nofollow" href="http://matrix.cpantesters.org/?dist=constant+1.33" title="Matrix">Testers</a> <span title="(pass / fail / na)">(<a rel="noopener nofollow" href="https://www.cpantesters.org/distro/c/constant.html?oncpan=1&amp;distmat=1&amp;version=1.33&amp;grade=2" style="color: #090">5781</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/c/constant.html?oncpan=1&amp;distmat=1&amp;version=1.33&amp;grade=3" style="color: #900">0</a> / <a rel="noopener nofollow" href="https://www.cpantesters.org/distro/c/constant.html?oncpan=1&amp;distmat=1&amp;version=1.33&amp;grade=4">16</a>)</span> </li> <li> <a rel="noopener nofollow" href="http://cpants.cpanauthors.org/release/RJBS/constant-1.33">Kwalitee</a> </li> <li> <div class="ttip" data-toggle="tooltip" data-placement="bottom" title="The # people with an indexing permission on constant who have released something to CPAN in the last 2 years (i.e. the # people likely able to release critical fixes in a timely manner)"> Bus factor: 5 </div> </li> <li> <a rel="noopener nofollow" href="http://cpancover.com/latest/constant-1.33/index.html">0.00% Coverage </a> </li> <li> License: perl_5 </li> <li class="nav-header">Activity</li> <li> <div class="activity-graph"> <img src="/dist/constant/activity.svg?res=month" /> <div class="comment">24 month</div> </div> </li> <li class="nav-header">Tools</li> <li> <a itemprop="downloadUrl" href="https://cpan.metacpan.org/authors/id/R/RJ/RJBS/constant-1.33.tar.gz"> Download (<span itemprop="fileSize">13.19KB</span>)</a> </li> <li> <a href="https://explorer.metacpan.org/?url=%2Fmodule%2FRJBS%2Fconstant-1.33%2Flib%2Fconstant.pm"> MetaCPAN Explorer </a> </li> <li> <a href="/dist/constant/permissions"> Permissions </a> </li> <li> <a href="/dist/constant/releases.rss"> Subscribe to distribution </a> </li> <li> <button class="btn btn-link" data-toggle="modal" data-target="#metacpan_install-instructions-dialog"> Install Instructions </button> </li> <li> <form action="/search"> <input type="hidden" name="q" value="dist:constant"> <input type="search" name="q" placeholder="Search distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li> <form action="https://grep.metacpan.org/search"> <input type="hidden" name="qd" value="constant"> <input type="hidden" name="source" value="metacpan"> <input type="search" name="q" placeholder="grep distribution" class="form-control tool-bar-form"> <input type="submit" style="display: none"> </form> </li> <li class="version-jump"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Jump to version</option> <option disabled value="/release/RJBS/constant-1.33/view/lib/constant.pm" >1.33 (RJBS on 2015-06-08)</option> <option value="/release/SAPER/constant-1.27/view/lib/constant.pm" >1.27 (SAPER on 2013-03-21)</option> <option value="/release/SAPER/constant-1.25/view/lib/constant.pm" >1.25 (SAPER on 2013-01-19)</option> <option value="/release/SAPER/constant-1.24/view/lib/constant.pm" >1.24 (SAPER on 2013-01-07)</option> <option value="/release/SAPER/constant-1.21/view/lib/constant.pm" >1.21 (SAPER on 2011-04-18)</option> <optgroup label="BackPAN">' <option value="/release/SAPER/constant-1.20/view/lib/constant.pm" >1.20 (SAPER on 2011-04-16)</option> <option value="/release/SAPER/constant-1.19/view/lib/constant.pm" >1.19 (SAPER on 2009-09-12)</option> <option value="/release/SAPER/constant-1.18/view/lib/constant.pm" >1.18 (SAPER on 2009-08-18)</option> <option value="/release/SAPER/constant-1.17/view/lib/constant.pm" >1.17 (SAPER on 2008-10-29)</option> <option value="/release/SAPER/constant-1.16/view/lib/constant.pm" >1.16 (SAPER on 2008-09-21)</option> <option value="/release/SAPER/constant-1.15/view/lib/constant.pm" >1.15 (SAPER on 2007-12-31)</option> <option value="/release/SAPER/constant-1.14/view/lib/constant.pm" >1.14 (SAPER on 2007-12-27)</option> <option value="/release/SAPER/constant-1.13/view/lib/constant.pm" >1.13 (SAPER on 2007-12-27)</option> <option value="/release/SAPER/constant-1.12/view/lib/constant.pm" >1.12 (SAPER on 2007-11-06)</option> <option value="/release/SAPER/constant-1.11/view/lib/constant.pm" >1.11 (SAPER on 2007-09-25)</option> <option value="/release/PHOENIX/constant-0.01/view/lib/constant.pm" >0.01 (PHOENIX on 1997-03-18)</option> </optgroup> </select> </li> <li class="version-diff"> <select class="select-navigator form-control tool-bar-form"> <option disabled selected>Diff with version</option> <option disabled value="/release/RJBS/constant-1.33/diff/RJBS/constant-1.33/lib/constant.pm" >1.33 (RJBS on 2015-06-08)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.27/lib/constant.pm" >1.27 (SAPER on 2013-03-21)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.25/lib/constant.pm" >1.25 (SAPER on 2013-01-19)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.24/lib/constant.pm" >1.24 (SAPER on 2013-01-07)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.21/lib/constant.pm" >1.21 (SAPER on 2011-04-18)</option> <optgroup label="BackPAN">' <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.20/lib/constant.pm" >1.20 (SAPER on 2011-04-16)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.19/lib/constant.pm" >1.19 (SAPER on 2009-09-12)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.18/lib/constant.pm" >1.18 (SAPER on 2009-08-18)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.17/lib/constant.pm" >1.17 (SAPER on 2008-10-29)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.16/lib/constant.pm" >1.16 (SAPER on 2008-09-21)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.15/lib/constant.pm" >1.15 (SAPER on 2007-12-31)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.14/lib/constant.pm" >1.14 (SAPER on 2007-12-27)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.13/lib/constant.pm" >1.13 (SAPER on 2007-12-27)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.12/lib/constant.pm" >1.12 (SAPER on 2007-11-06)</option> <option value="/release/RJBS/constant-1.33/diff/SAPER/constant-1.11/lib/constant.pm" >1.11 (SAPER on 2007-09-25)</option> <option value="/release/RJBS/constant-1.33/diff/PHOENIX/constant-0.01/lib/constant.pm" >0.01 (PHOENIX on 1997-03-18)</option> </optgroup> </select> </li> <li> <ul class="dependencies"> <li class="nav-header">Dependencies</li> <li><a href="/pod/Test::More" title="Test::More" class="ellipsis">Test::More</a></li> <li><i class="ttip" title="dynamic_config enabled">and possibly others</i></li> <li> <hr> </li> <li> <a href="/module/constant/requires">Reverse dependencies</a> </li> <li> <a href="http://deps.cpantesters.org/?module=constant">CPAN Testers List</a> </li> <li> <a href="https://cpandeps.grinnz.com/?dist=constant">Dependency graph</a> </li> </ul> </li> <li class="nav-header">Permalinks</li> <li> <a href="/release/RJBS/constant-1.33/view/lib/constant.pm">This version</a> </li> <li> <a href="/pod/constant">Latest version</a> </li> <li> <div class="plussers"> <div class="nav-header">++ed by:</div> <div> <a class="display-all" href="/author/BRUNOV"><img src="https://www.gravatar.com/avatar/ff900b4f3eb6504c21691432e187923e?d=identicon&amp;s=20" title="BRUNOV" alt="BRUNOV"></a> <a class="display-all" href="/author/MMUSGROVE"><img src="https://www.gravatar.com/avatar/c8d0367f59978d30016ef08c7a60b961?d=identicon&amp;s=20" title="MMUSGROVE" alt="MMUSGROVE"></a> <a class="display-all" href="/author/RWP"><img src="https://www.gravatar.com/avatar/7110eb2389e69a066d9d7cad7a1cc2bf?d=identicon&amp;s=20" title="RWP" alt="RWP"></a> <a class="display-all" href="/author/DOHERTY"><img src="https://www.gravatar.com/avatar/9533c36b6385c8321bad636c98e9c0a8?d=identicon&amp;s=20" title="DOHERTY" alt="DOHERTY"></a> <a class="display-all" href="/author/KRAUTCAT"><img src="https://www.gravatar.com/avatar/efd137d1577aa9df7fc833f6306958d5?d=identicon&amp;s=20" title="KRAUTCAT" alt="KRAUTCAT"></a> </div> <!-- Display counts of plussers--> <div> <a href="/dist/constant/plussers">9 PAUSE users</a> </div> <div> 5 non-PAUSE users </div> </div> </li> <li> <div> <button class="contributors-show-button btn-link">and 1 contributors</button> <div id="metacpan_contributors" class="slide-out slide-out-hidden"> <div> <ul> <li class="contributor" data-contrib-email="sebastien@aperghis.net" > <img class="gravatar" width="20" height="20" src="https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?d=identicon&amp;s=20" /> Sebastien Aperghis-Tramoni </li> </ul> </div> </div> </div> </li> </ul> </div> </nav> <div class="content-navigation"> <div class="breadcrumbs"> <span> <a data-keyboard-shortcut="g a" rel="author" href="/author/RJBS" class="author-name">Ricardo SIGNES 🙂</a> </span> <span>&nbsp;/&nbsp;</span> <div class="release dist-release status-latest maturity-released"> <span class="dropdown"><b class="caret"></b></span> <select class="select-navigator "> <option selected value="/release/RJBS/constant-1.33/view/lib/constant.pm" >1.33 (RJBS on 2015-06-08)</option> <option value="/release/SAPER/constant-1.27/view/lib/constant.pm" >1.27 (SAPER on 2013-03-21)</option> <option value="/release/SAPER/constant-1.25/view/lib/constant.pm" >1.25 (SAPER on 2013-01-19)</option> <option value="/release/SAPER/constant-1.24/view/lib/constant.pm" >1.24 (SAPER on 2013-01-07)</option> <option value="/release/SAPER/constant-1.21/view/lib/constant.pm" >1.21 (SAPER on 2011-04-18)</option> <optgroup label="BackPAN">' <option value="/release/SAPER/constant-1.20/view/lib/constant.pm" >1.20 (SAPER on 2011-04-16)</option> <option value="/release/SAPER/constant-1.19/view/lib/constant.pm" >1.19 (SAPER on 2009-09-12)</option> <option value="/release/SAPER/constant-1.18/view/lib/constant.pm" >1.18 (SAPER on 2009-08-18)</option> <option value="/release/SAPER/constant-1.17/view/lib/constant.pm" >1.17 (SAPER on 2008-10-29)</option> <option value="/release/SAPER/constant-1.16/view/lib/constant.pm" >1.16 (SAPER on 2008-09-21)</option> <option value="/release/SAPER/constant-1.15/view/lib/constant.pm" >1.15 (SAPER on 2007-12-31)</option> <option value="/release/SAPER/constant-1.14/view/lib/constant.pm" >1.14 (SAPER on 2007-12-27)</option> <option value="/release/SAPER/constant-1.13/view/lib/constant.pm" >1.13 (SAPER on 2007-12-27)</option> <option value="/release/SAPER/constant-1.12/view/lib/constant.pm" >1.12 (SAPER on 2007-11-06)</option> <option value="/release/SAPER/constant-1.11/view/lib/constant.pm" >1.11 (SAPER on 2007-09-25)</option> <option value="/release/PHOENIX/constant-0.01/view/lib/constant.pm" >0.01 (PHOENIX on 1997-03-18)</option> </optgroup> </select> <a data-keyboard-shortcut="g d" class="release-name" href="/dist/constant">constant-1.33</a> </div> <span class="river-gauge-gauge"> <svg width="24px" height="15px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g> <title> River stage five &#10; • 768 direct dependents &#10; • 27266 total dependents </title> <rect x="0" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="5" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="10" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="15" y="0" width="4" height="15" fill="#7ea3f2" /> <rect x="20" y="0" width="4" height="15" fill="#7ea3f2" /> </g> </svg> </span> <div id="constant-fav" class="show-logged-in"> <form action="/account/favorite/add" style="display: inline" method="POST"> <input type="hidden" name="remove" value="0"> <input type="hidden" name="release" value="constant-1.33"> <input type="hidden" name="author" value="RJBS"> <input type="hidden" name="distribution" value="constant"> <button type="submit" class="favorite highlight"><span>14</span> ++</button> </form> </div> <div class="show-logged-out"> <button class="fav-not-logged-in favorite highlight"><span>14</span> ++</button> </div> / <span>constant</span> </div> </div> <main class="content"> <nav class="toc"> <div class="toc-header"><strong>Contents</strong></div> <ul> <li><a href="#NAME">NAME</a></li> <li><a href="#SYNOPSIS">SYNOPSIS</a></li> <li><a href="#DESCRIPTION">DESCRIPTION</a></li> <li><a href="#NOTES">NOTES</a> <ul> <li><a href="#List-constants">List constants</a></li> <li><a href="#Defining-multiple-constants-at-once">Defining multiple constants at once</a></li> <li><a href="#Magic-constants">Magic constants</a></li> </ul> </li> <li><a href="#TECHNICAL-NOTES">TECHNICAL NOTES</a></li> <li><a href="#CAVEATS">CAVEATS</a></li> <li><a href="#SEE-ALSO">SEE ALSO</a></li> <li><a href="#BUGS">BUGS</a></li> <li><a href="#AUTHORS">AUTHORS</a></li> <li><a href="#COPYRIGHT-%26-LICENSE">COPYRIGHT &amp; LICENSE</a></li> </ul></nav> <div class="pod anchors"> <h1 id="NAME">NAME</h1> <p>constant - Perl pragma to declare constants</p> <h1 id="SYNOPSIS">SYNOPSIS</h1> <pre><code>use constant PI =&gt; 4 * atan2(1, 1); use constant DEBUG =&gt; 0; print &quot;Pi equals &quot;, PI, &quot;...\n&quot; if DEBUG; use constant { SEC =&gt; 0, MIN =&gt; 1, HOUR =&gt; 2, MDAY =&gt; 3, MON =&gt; 4, YEAR =&gt; 5, WDAY =&gt; 6, YDAY =&gt; 7, ISDST =&gt; 8, }; use constant WEEKDAYS =&gt; qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); print &quot;Today is &quot;, (WEEKDAYS)[ (localtime)[WDAY] ], &quot;.\n&quot;;</code></pre> <h1 id="DESCRIPTION">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">NOTES</h1> <p>As with all <code>use</code> directives, defining a constant happens at compile time. Thus, it&#39;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 &quot;Pi equals PI...\n&quot;; # WRONG: does not expand &quot;PI&quot; print &quot;Pi equals &quot;.PI.&quot;...\n&quot;; # 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 =&gt; [ 1,2,3,4 ]; print ARRAY-&gt;[1]; ARRAY-&gt;[1] = &quot; be changed&quot;; print ARRAY-&gt;[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-&gt;CONSTANT</code> or as <code>$obj-&gt;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 &quot;OtherPackage::FWIBBLE&quot; =&gt; 7865; constant-&gt;import(&quot;Other::FWOBBLE&quot;,$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 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 =&gt; localtime; # WRONG! use constant TIMESTAMP =&gt; 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 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 =&gt; &quot;A single value&quot;, BAR =&gt; &quot;This&quot;, &quot;won&#39;t&quot;, &quot;work!&quot;, # 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&#39;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&#39;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 =&gt; 0xAFBAEBA8, NEGMASK =&gt; ~BITMASK, # Error! };</code></pre> <h2 id="Magic-constants"><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&#39;t totally portable, alas.)</p> <pre><code>use constant E2BIG =&gt; ($! = 7); print E2BIG, &quot;\n&quot;; # something like &quot;Arg list too long&quot; print 0+E2BIG, &quot;\n&quot;; # &quot;7&quot;</code></pre> <p>You can&#39;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 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="/pod/perlsub#Constant-Functions">&quot;Constant Functions&quot; 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&#39;t omit this! my $name = shift; $name =~ s/^::/main::/; my $pkg = caller; my $full_name = $name =~ /::/ ? $name : &quot;${pkg}::$name&quot;; $constant::declared{$full_name}; }</code></pre> <h1 id="CAVEATS">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&#39;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>=&gt;</code> operator quotes a bareword immediately to its left, you have to say <code>CONSTANT() =&gt; &#39;value&#39;</code> (or simply use a comma in place of the big arrow) instead of <code>CONSTANT =&gt; &#39;value&#39;</code>.</p> <h1 id="SEE-ALSO"><a id="SEE"></a>SEE ALSO</h1> <p><a href="/pod/Readonly">Readonly</a> - Facility for creating read-only scalars, arrays, hashes.</p> <p><a href="/pod/Attribute::Constant">Attribute::Constant</a> - Make read-only variables via attribute</p> <p><a href="/pod/Scalar::Readonly">Scalar::Readonly</a> - Perl extension to the <code>SvREADONLY</code> scalar flag</p> <p><a href="/pod/Hash::Util">Hash::Util</a> - A selection of general-utility hash subroutines (mostly to lock/unlock keys and values)</p> <h1 id="BUGS">BUGS</h1> <p>Please report any bugs or feature requests via the perlbug(1) utility.</p> <h1 id="AUTHORS">AUTHORS</h1> <p>Tom Phoenix, &lt;<i>rootbeer@redcat.com</i>&gt;, with help from many other folks.</p> <p>Multiple constant declarations at once added by Casey West, &lt;<i>casey@geeknest.com</i>&gt;.</p> <p>Documentation mostly rewritten by Ilmari Karonen, &lt;<i>perl@itz.pp.sci.fi</i>&gt;.</p> <p>This program is maintained by the Perl 5 Porters. The CPAN distribution is maintained by S&eacute;bastien Aperghis-Tramoni &lt;<i>sebastien@aperghis.net</i>&gt;.</p> <h1 id="COPYRIGHT-&amp;-LICENSE"><a id="COPYRIGHT"></a><a id="COPYRIGHT-LICENSE"></a>COPYRIGHT &amp; 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="metacpan_install-instructions-dialog" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">Module Install Instructions</h4> </div> <div class="modal-body"> <p>To install constant, copy and paste the appropriate command in to your terminal.</p> <p><a href="/dist/App-cpanminus/view/bin/cpanm">cpanm</a></p> <pre><code>cpanm constant</code></pre> <p><a href="/pod/CPAN">CPAN shell</a></p> <pre><code>perl -MCPAN -e shell install constant</code></pre> <p>For more information on module installation, please visit <a href="https://www.cpan.org/modules/INSTALL.html">the detailed CPAN module installation guide</a>.</p> </div> <div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a> </div> </div> </div> </div> </main> <div class="content-pagination"> </div> </div> <footer class="footer"> <div class="footer-container"> <div class="footer-social"> <div class="footer-link footer-logo"> <a href="/"> <img src="/static/images/metacpan-logo.svg" alt="MetaCPAN" /> </a> </div> <a class="footer-social-link" href="https://github.com/metacpan"> <i class="fab fa-github-square"></i> </a> <a class="footer-social-link" href="https://fosstodon.org/@metacpan"> <i class="fab fa-mastodon"></i> </a> </div> <div class="footer-links"> <div class="footer-link"> <a href="/about">About</a> </div> <div class="footer-link"> <a href="/about/sponsors">Sponsor</a> </div> <div class="footer-link"> <a href="https://grep.metacpan.org">grep::cpan</a> </div> <div class="footer-link"> <a href="/recent">Recent</a> </div> <div class="footer-link"> <a href="/about/faq">FAQ</a> </div> <div class="footer-link"> <a href="/tools">Tools</a> </div> <div class="footer-link"> <a href="https://fastapi.metacpan.org/">API</a> </div> <div class="footer-link"> <a href="https://www.perl.org/">Perl.org</a> </div> </div> <div class="footer-sponsors"> <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener"> <img class="footer-sponsor-bytemark" src="/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener"> <img class="footer-sponsor-liquidweb" src="/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener"> <img class="footer-sponsor-deriv" src="/static/images/sponsors/deriv.svg" alt="Deriv logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener"> <img class="footer-sponsor-geocode" src="/static/images/sponsors/geocodelogo.svg" alt="Geocode logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener"> <img class="footer-sponsor-fastly" src="/static/images/sponsors/fastly_logo.svg" alt="Fastly logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener"> <img class="footer-sponsor-opencage" src="/static/images/sponsors/open-cage.svg" alt="OpenCage logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://www.elastic.co/" rel="noopener"> <img class="footer-sponsor-elastic" src="/static/images/sponsors/elastic.svg" alt="Elastic logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://route4me.com/" rel="noopener"> <img class="footer-sponsor-route4me" src="/static/images/sponsors/route4me.png" alt="Route4Me logo"> </a> </div> </div> </footer> <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Keyboard Shortcuts</h4> </div> <div class="modal-body row"> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Global</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>s</kbd> </td> <td>Focus search bar</td> </tr> <tr> <td class="keys"> <kbd>?</kbd> </td> <td>Bring up this help dialog</td> </tr> </tbody> </table> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>GitHub</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>p</kbd> </td> <td>Go to pull requests</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>go to github issues (only if github is preferred repository)</td> </tr> </tbody> </table> </div> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>POD</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>a</kbd> </td> <td>Go to author</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>c</kbd> </td> <td>Go to changes</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>Go to issues</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>d</kbd> </td> <td>Go to dist</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>r</kbd> </td> <td>Go to repository/SCM</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>s</kbd> </td> <td>Go to source</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>b</kbd> </td> <td>Go to file browse</td> </tr> </tbody> </table> </div> <div class="col-md-12"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Search terms</th> </tr> </thead> <tbody> <tr> <td><em>module:</em> (e.g. <a href="/search?q=module%3APlugin">module:Plugin</a>)</td> </tr> <tr> <td><em>distribution:</em> (e.g. <a href="/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td> </tr> <tr> <td><em>author:</em> (e.g. <a href="/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td> </tr> <tr> <td><em>version:</em> (e.g. <a href="/search?q=version%3A1.00">version:1.00</a>)</td> </tr> </tbody> </table> </div> </div> <div class="modal-footer"></div> </div> </div> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10