CINXE.COM

perlmod - Perl modules (packages and symbol tables) - 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>perlmod - Perl modules (packages and symbol tables) - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/perlmod"> <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="/">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.6.0</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/perlmod">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.0/perlmod">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.2/perlmod">5.38.2</a> <a class="dropdown-item" href="/5.38.1/perlmod">5.38.1</a> <a class="dropdown-item" href="/5.38.0/perlmod">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/perlmod">5.36.3</a> <a class="dropdown-item" href="/5.36.2/perlmod">5.36.2</a> <a class="dropdown-item" href="/5.36.1/perlmod">5.36.1</a> <a class="dropdown-item" href="/5.36.0/perlmod">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/perlmod">5.34.3</a> <a class="dropdown-item" href="/5.34.2/perlmod">5.34.2</a> <a class="dropdown-item" href="/5.34.1/perlmod">5.34.1</a> <a class="dropdown-item" href="/5.34.0/perlmod">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/perlmod">5.32.1</a> <a class="dropdown-item" href="/5.32.0/perlmod">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/perlmod">5.30.3</a> <a class="dropdown-item" href="/5.30.2/perlmod">5.30.2</a> <a class="dropdown-item" href="/5.30.1/perlmod">5.30.1</a> <a class="dropdown-item" href="/5.30.0/perlmod">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perlmod">5.28.3</a> <a class="dropdown-item" href="/5.28.2/perlmod">5.28.2</a> <a class="dropdown-item" href="/5.28.1/perlmod">5.28.1</a> <a class="dropdown-item" href="/5.28.0/perlmod">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/perlmod">5.26.3</a> <a class="dropdown-item" href="/5.26.2/perlmod">5.26.2</a> <a class="dropdown-item" href="/5.26.1/perlmod">5.26.1</a> <a class="dropdown-item" href="/5.26.0/perlmod">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/perlmod">5.24.4</a> <a class="dropdown-item" href="/5.24.3/perlmod">5.24.3</a> <a class="dropdown-item" href="/5.24.2/perlmod">5.24.2</a> <a class="dropdown-item" href="/5.24.1/perlmod">5.24.1</a> <a class="dropdown-item" href="/5.24.0/perlmod">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/perlmod">5.22.4</a> <a class="dropdown-item" href="/5.22.3/perlmod">5.22.3</a> <a class="dropdown-item" href="/5.22.2/perlmod">5.22.2</a> <a class="dropdown-item" href="/5.22.1/perlmod">5.22.1</a> <a class="dropdown-item" href="/5.22.0/perlmod">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perlmod">5.20.3</a> <a class="dropdown-item" href="/5.20.2/perlmod">5.20.2</a> <a class="dropdown-item" href="/5.20.1/perlmod">5.20.1</a> <a class="dropdown-item" href="/5.20.0/perlmod">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/perlmod">5.18.4</a> <a class="dropdown-item" href="/5.18.3/perlmod">5.18.3</a> <a class="dropdown-item" href="/5.18.2/perlmod">5.18.2</a> <a class="dropdown-item" href="/5.18.1/perlmod">5.18.1</a> <a class="dropdown-item" href="/5.18.0/perlmod">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/perlmod">5.16.3</a> <a class="dropdown-item" href="/5.16.2/perlmod">5.16.2</a> <a class="dropdown-item" href="/5.16.1/perlmod">5.16.1</a> <a class="dropdown-item" href="/5.16.0/perlmod">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perlmod">5.14.4</a> <a class="dropdown-item" href="/5.14.3/perlmod">5.14.3</a> <a class="dropdown-item" href="/5.14.2/perlmod">5.14.2</a> <a class="dropdown-item" href="/5.14.1/perlmod">5.14.1</a> <a class="dropdown-item" href="/5.14.0/perlmod">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/perlmod">5.12.5</a> <a class="dropdown-item" href="/5.12.4/perlmod">5.12.4</a> <a class="dropdown-item" href="/5.12.3/perlmod">5.12.3</a> <a class="dropdown-item" href="/5.12.2/perlmod">5.12.2</a> <a class="dropdown-item" href="/5.12.1/perlmod">5.12.1</a> <a class="dropdown-item" href="/5.12.0/perlmod">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/perlmod">5.10.1</a> <a class="dropdown-item" href="/5.10.0/perlmod">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perlmod">5.8.9</a> <a class="dropdown-item" href="/5.8.8/perlmod">5.8.8</a> <a class="dropdown-item" href="/5.8.7/perlmod">5.8.7</a> <a class="dropdown-item" href="/5.8.6/perlmod">5.8.6</a> <a class="dropdown-item" href="/5.8.5/perlmod">5.8.5</a> <a class="dropdown-item" href="/5.8.4/perlmod">5.8.4</a> <a class="dropdown-item" href="/5.8.3/perlmod">5.8.3</a> <a class="dropdown-item" href="/5.8.2/perlmod">5.8.2</a> <a class="dropdown-item" href="/5.8.1/perlmod">5.8.1</a> <a class="dropdown-item" href="/5.8.0/perlmod">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perlmod">5.6.2</a> <a class="dropdown-item" href="/5.6.1/perlmod">5.6.1</a> <a class="dropdown-item active" href="/5.6.0/perlmod">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/perlmod">5.005_04</a> <a class="dropdown-item" href="/5.005_03/perlmod">5.005_03</a> <a class="dropdown-item" href="/5.005_02/perlmod">5.005_02</a> <a class="dropdown-item" href="/5.005_01/perlmod">5.005_01</a> <a class="dropdown-item" href="/5.005/perlmod">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/perlmod">blead</a> <a class="dropdown-item" href="/5.41.6/perlmod">5.41.6</a> <a class="dropdown-item" href="/5.41.5/perlmod">5.41.5</a> <a class="dropdown-item" href="/5.41.4/perlmod">5.41.4</a> <a class="dropdown-item" href="/5.41.3/perlmod">5.41.3</a> <a class="dropdown-item" href="/5.41.2/perlmod">5.41.2</a> <a class="dropdown-item" href="/5.41.1/perlmod">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.0-RC2/perlmod">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/perlmod">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perlmod">5.39.10</a> <a class="dropdown-item" href="/5.39.9/perlmod">5.39.9</a> <a class="dropdown-item" href="/5.39.8/perlmod">5.39.8</a> <a class="dropdown-item" href="/5.39.7/perlmod">5.39.7</a> <a class="dropdown-item" href="/5.39.6/perlmod">5.39.6</a> <a class="dropdown-item" href="/5.39.5/perlmod">5.39.5</a> <a class="dropdown-item" href="/5.39.4/perlmod">5.39.4</a> <a class="dropdown-item" href="/5.39.3/perlmod">5.39.3</a> <a class="dropdown-item" href="/5.39.2/perlmod">5.39.2</a> <a class="dropdown-item" href="/5.39.1/perlmod">5.39.1</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.6.0/perl">Perl</a> <a class="dropdown-item" href="/5.6.0/perlintro">Intro</a> <a class="dropdown-item" href="/5.6.0/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.6.0/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.6.0/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.0/perlop">Operators</a> <a class="dropdown-item" href="/5.6.0/functions">Functions</a> <a class="dropdown-item" href="/5.6.0/variables">Variables</a> <a class="dropdown-item" href="/5.6.0/modules">Modules</a> <a class="dropdown-item" href="/5.6.0/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.0/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.6.0/perlcommunity">Community</a> <a class="dropdown-item" href="/5.6.0/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.6.0/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.6.0/perlmod">perlmod</a> <div id="more"> (<a href="/5.6.0/perlmod.txt">source</a>, <a href="https://metacpan.org/pod/perlmod">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.6.0. <a href="/perlmod">View the latest version</a> </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="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#Packages">Packages</a> </li> <li> <a class="text-decoration-none" href="#Symbol-Tables">Symbol Tables</a> </li> <li> <a class="text-decoration-none" href="#Package-Constructors-and-Destructors">Package Constructors and Destructors</a> </li> <li> <a class="text-decoration-none" href="#Perl-Classes">Perl Classes</a> </li> <li> <a class="text-decoration-none" href="#Perl-Modules">Perl Modules</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>perlmod - Perl modules (packages and symbol tables)</p> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <h2 id="Packages"><a class="permalink" href="#Packages">#</a>Packages</h2> <p>Perl provides a mechanism for alternative namespaces to protect packages from stomping on each other&#39;s variables. In fact, there&#39;s really no such thing as a global variable in Perl . The package statement declares the compilation unit as being in the given namespace. The scope of the package declaration is from the declaration itself through the end of the enclosing block, <code>eval</code>, or file, whichever comes first (the same scope as the my() and local() operators). Unqualified dynamic identifiers will be in this namespace, except for those few identifiers that if unqualified, default to the main package instead of the current one as described below. A package statement affects only dynamic variables--including those you&#39;ve used local() on--but <i>not</i> lexical variables created with my(). Typically it would be the first declaration in a file included by the <code>do</code>, <code>require</code>, or <code>use</code> operators. You can switch into a package in more than one place; it merely influences which symbol table is used by the compiler for the rest of that block. You can refer to variables and filehandles in other packages by prefixing the identifier with the package name and a double colon: <code>$Package::Variable</code>. If the package name is null, the <code>main</code> package is assumed. That is, <code>$::sail</code> is equivalent to <code>$main::sail</code>.</p> <p>The old package delimiter was a single quote, but double colon is now the preferred delimiter, in part because it&#39;s more readable to humans, and in part because it&#39;s more readable to <b>emacs</b> macros. It also makes C++ programmers feel like they know what&#39;s going on--as opposed to using the single quote as separator, which was there to make Ada programmers feel like they knew what&#39;s going on. Because the old-fashioned syntax is still supported for backwards compatibility, if you try to use a string like <code>&quot;This is $owner&#39;s house&quot;</code>, you&#39;ll be accessing <code>$owner::s</code>; that is, the $s variable in package <code>owner</code>, which is probably not what you meant. Use braces to disambiguate, as in <code>&quot;This is ${owner}&#39;s house&quot;</code>.</p> <p>Packages may themselves contain package separators, as in <code>$OUTER::INNER::var</code>. This implies nothing about the order of name lookups, however. There are no relative packages: all symbols are either local to the current package, or must be fully qualified from the outer package name down. For instance, there is nowhere within package <code>OUTER</code> that <code>$INNER::var</code> refers to <code>$OUTER::INNER::var</code>. It would treat package <code>INNER</code> as a totally separate global package.</p> <p>Only identifiers starting with letters (or underscore) are stored in a package&#39;s symbol table. All other symbols are kept in package <code>main</code>, including all punctuation variables, like $_. In addition, when unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC, and SIG are forced to be in package <code>main</code>, even when used for other purposes than their built-in one. If you have a package called <code>m</code>, <code>s</code>, or <code>y</code>, then you can&#39;t use the qualified form of an identifier because it would be instead interpreted as a pattern match, a substitution, or a transliteration.</p> <p>Variables beginning with underscore used to be forced into package main, but we decided it was more useful for package writers to be able to use leading underscore to indicate private variables and method names. $_ is still global though. See also <a href="/5.6.0/perlvar#Technical-Note-on-the-Syntax-of-Variable-Names">&quot;Technical Note on the Syntax of Variable Names&quot; in perlvar</a>.</p> <p><code>eval</code>ed strings are compiled in the package in which the eval() was compiled. (Assignments to <code>$SIG{}</code>, however, assume the signal handler specified is in the <code>main</code> package. Qualify the signal handler name if you wish to have a signal handler in a package.) For an example, examine <i>perldb.pl</i> in the Perl library. It initially switches to the <code>DB</code> package so that the debugger doesn&#39;t interfere with variables in the program you are trying to debug. At various points, however, it temporarily switches back to the <code>main</code> package to evaluate various expressions in the context of the <code>main</code> package (or wherever you came from). See <a href="/5.6.0/perldebug">perldebug</a>.</p> <p>The special symbol <code>__PACKAGE__</code> contains the current package, but cannot (easily) be used to construct variables.</p> <p>See <a href="/5.6.0/perlsub">perlsub</a> for other scoping issues related to my() and local(), and <a href="/5.6.0/perlref">perlref</a> regarding closures.</p> <h2 id="Symbol-Tables"><a class="permalink" href="#Symbol-Tables">#</a><a id="Symbol"></a>Symbol Tables</h2> <p>The symbol table for a package happens to be stored in the hash of that name with two colons appended. The main symbol table&#39;s name is thus <code>%main::</code>, or <code>%::</code> for short. Likewise symbol table for the nested package mentioned earlier is named <code>%OUTER::INNER::</code>.</p> <p>The value in each entry of the hash is what you are referring to when you use the <code>*name</code> typeglob notation. In fact, the following have the same effect, though the first is more efficient because it does the symbol table lookups at compile time:</p> <pre><code>local *main::foo = *main::bar; local $main::{foo} = $main::{bar};</code></pre> <p>You can use this to print out all the variables in a package, for instance. The standard but antequated <i>dumpvar.pl</i> library and the CPAN module Devel::Symdump make use of this.</p> <p>Assignment to a typeglob performs an aliasing operation, i.e.,</p> <pre><code>*dick = *richard;</code></pre> <p>causes variables, subroutines, formats, and file and directory handles accessible via the identifier <code>richard</code> also to be accessible via the identifier <code>dick</code>. If you want to alias only a particular variable or subroutine, assign a reference instead:</p> <pre><code>*dick = \$richard;</code></pre> <p>Which makes $richard and $dick the same variable, but leaves @richard and @dick as separate arrays. Tricky, eh?</p> <p>This mechanism may be used to pass and return cheap references into or from subroutines if you won&#39;t want to copy the whole thing. It only works when assigning to dynamic variables, not lexicals.</p> <pre><code> %some_hash = (); # can&#39;t be my() *some_hash = fn( \%another_hash ); sub fn { local *hashsym = shift; # now use %hashsym normally, and you # will affect the caller&#39;s %another_hash my %nhash = (); # do what you want return \%nhash; }</code></pre> <p>On return, the reference will overwrite the hash slot in the symbol table specified by the *some_hash typeglob. This is a somewhat tricky way of passing around references cheaply when you won&#39;t want to have to remember to dereference variables explicitly.</p> <p>Another use of symbol tables is for making &quot;constant&quot; scalars.</p> <pre><code>*PI = \3.14159265358979;</code></pre> <p>Now you cannot alter $PI, which is probably a good thing all in all. This isn&#39;t the same as a constant subroutine, which is subject to optimization at compile-time. This isn&#39;t. A constant subroutine is one prototyped to take no arguments and to return a constant expression. See <a href="/5.6.0/perlsub">perlsub</a> for details on these. The <code>use constant</code> pragma is a convenient shorthand for these.</p> <p>You can say <code>*foo{PACKAGE}</code> and <code>*foo{NAME}</code> to find out what name and package the *foo symbol table entry comes from. This may be useful in a subroutine that gets passed typeglobs as arguments:</p> <pre><code>sub identify_typeglob { my $glob = shift; print &#39;You gave me &#39;, *{$glob}{PACKAGE}, &#39;::&#39;, *{$glob}{NAME}, &quot;\n&quot;; } identify_typeglob *foo; identify_typeglob *bar::baz;</code></pre> <p>This prints</p> <pre><code class="plaintext">You gave me main::foo You gave me bar::baz</code></pre> <p>The <code>*foo{THING}</code> notation can also be used to obtain references to the individual elements of *foo, see <a href="/5.6.0/perlref">perlref</a>.</p> <p>Subroutine definitions (and declarations, for that matter) need not necessarily be situated in the package whose symbol table they occupy. You can define a subroutine outside its package by explicitly qualifying the name of the subroutine:</p> <pre><code>package main; sub Some_package::foo { ... } # &amp;foo defined in Some_package</code></pre> <p>This is just a shorthand for a typeglob assignment at compile time:</p> <pre><code class="plaintext">BEGIN { *Some_package::foo = sub { ... } }</code></pre> <p>and is <i>not</i> the same as writing:</p> <pre><code> { package Some_package; sub foo { ... } }</code></pre> <p>In the first two versions, the body of the subroutine is lexically in the main package, <i>not</i> in Some_package. So something like this:</p> <pre><code> package main; $Some_package::name = &quot;fred&quot;; $main::name = &quot;barney&quot;; sub Some_package::foo { print &quot;in &quot;, __PACKAGE__, &quot;: \$name is &#39;$name&#39;\n&quot;; } Some_package::foo();</code></pre> <p>prints:</p> <pre><code>in main: $name is &#39;barney&#39;</code></pre> <p>rather than:</p> <pre><code>in Some_package: $name is &#39;fred&#39;</code></pre> <p>This also has implications for the use of the SUPER:: qualifier (see <a href="/5.6.0/perlobj">perlobj</a>).</p> <h2 id="Package-Constructors-and-Destructors"><a class="permalink" href="#Package-Constructors-and-Destructors">#</a><a id="Package"></a>Package Constructors and Destructors</h2> <p>Four special subroutines act as package constructors and destructors. These are the <code>BEGIN</code>, <code>CHECK</code>, <code>INIT</code>, and <code>END</code> routines. The <code>sub</code> is optional for these routines.</p> <p>A <code>BEGIN</code> subroutine is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file is parsed. You may have multiple <code>BEGIN</code> blocks within a file--they will execute in order of definition. Because a <code>BEGIN</code> block executes immediately, it can pull in definitions of subroutines and such from other files in time to be visible to the rest of the file. Once a <code>BEGIN</code> has run, it is immediately undefined and any code it used is returned to Perl&#39;s memory pool. This means you can&#39;t ever explicitly call a <code>BEGIN</code>.</p> <p>An <code>END</code> subroutine is executed as late as possible, that is, after perl has finished running the program and just before the interpreter is being exited, even if it is exiting as a result of a die() function. (But not if it&#39;s polymorphing into another program via <code>exec</code>, or being blown out of the water by a signal--you have to trap that yourself (if you can).) You may have multiple <code>END</code> blocks within a file--they will execute in reverse order of definition; that is: last in, first out (LIFO). <code>END</code> blocks are not executed when you run perl with the <code>-c</code> switch.</p> <p>Inside an <code>END</code> subroutine, <code>$?</code> contains the value that the program is going to pass to <code>exit()</code>. You can modify <code>$?</code> to change the exit value of the program. Beware of changing <code>$?</code> by accident (e.g. by running something via <code>system</code>).</p> <p>Similar to <code>BEGIN</code> blocks, <code>INIT</code> blocks are run just before the Perl runtime begins execution, in &quot;first in, first out&quot; (FIFO) order. For example, the code generators documented in <a href="/5.6.0/perlcc">perlcc</a> make use of <code>INIT</code> blocks to initialize and resolve pointers to XSUBs.</p> <p>Similar to <code>END</code> blocks, <code>CHECK</code> blocks are run just after the Perl compile phase ends and before the run time begins, in LIFO order. <code>CHECK</code> blocks are again useful in the Perl compiler suite to save the compiled state of the program.</p> <p>When you use the <b>-n</b> and <b>-p</b> switches to Perl, <code>BEGIN</code> and <code>END</code> work just as they do in <b>awk</b>, as a degenerate case. As currently implemented (and subject to change, since its inconvenient at best), both <code>BEGIN</code> and&lt;END&gt; blocks are run when you use the <b>-c</b> switch for a compile-only syntax check, although your main code is not.</p> <h2 id="Perl-Classes"><a class="permalink" href="#Perl-Classes">#</a><a id="Perl"></a>Perl Classes</h2> <p>There is no special class syntax in Perl, but a package may act as a class if it provides subroutines to act as methods. Such a package may also derive some of its methods from another class (package) by listing the other package name(s) in its global @ISA array (which must be a package global, not a lexical).</p> <p>For more on this, see <a href="/5.6.0/perltoot">perltoot</a> and <a href="/5.6.0/perlobj">perlobj</a>.</p> <h2 id="Perl-Modules"><a class="permalink" href="#Perl-Modules">#</a><a id="Perl1"></a>Perl Modules</h2> <p>A module is just a set of related function in a library file a Perl package with the same name as the file. It is specifically designed to be reusable by other modules or programs. It may do this by providing a mechanism for exporting some of its symbols into the symbol table of any package using it. Or it may function as a class definition and make its semantics available implicitly through method calls on the class and its objects, without explicitly exportating anything. Or it can do a little of both.</p> <p>For example, to start a traditional, non-OO module called Some::Module, create a file called <i>Some/Module.pm</i> and start with this template:</p> <pre><code>package Some::Module; # assumes Some/Module.pm use strict; use warnings; BEGIN { use Exporter (); our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); # set the version for version checking $VERSION = 1.00; # if using RCS/CVS, this may be preferred $VERSION = do { my @r = (q$Revision: 2.21 $ =~ /\d+/g); sprintf &quot;%d.&quot;.&quot;%02d&quot; x $#r, @r }; # must be all one line, for MakeMaker @ISA = qw(Exporter); @EXPORT = qw(&amp;func1 &amp;func2 &amp;func4); %EXPORT_TAGS = ( ); # eg: TAG =&gt; [ qw!name1 name2! ], # your exported package globals go here, # as well as any optionally exported functions @EXPORT_OK = qw($Var1 %Hashit &amp;func3); } our @EXPORT_OK; # non-exported package globals go here our @more; our $stuff; # initialize package globals, first exported ones $Var1 = &#39;&#39;; %Hashit = (); # then the others (which are still accessible as $Some::Module::stuff) $stuff = &#39;&#39;; @more = (); # all file-scoped lexicals must be created before # the functions below that use them. # file-private lexicals go here my $priv_var = &#39;&#39;; my %secret_hash = (); # here&#39;s a file-private function as a closure, # callable as &amp;$priv_func; it cannot be prototyped. my $priv_func = sub { # stuff goes here. }; # make all your functions, whether exported or not; # remember to put something interesting in the {} stubs sub func1 {} # no prototype sub func2() {} # proto&#39;d void sub func3($$) {} # proto&#39;d to 2 scalars # this one isn&#39;t exported, but could be called! sub func4(\%) {} # proto&#39;d to 1 hash ref END { } # module clean-up code here (global destructor) ## YOUR CODE GOES HERE 1; # don&#39;t forget to return a true value from the file</code></pre> <p>Then go on to declare and use your variables in functions without any qualifications. See <a href="/5.6.0/Exporter">Exporter</a> and the <a href="/5.6.0/perlmodlib">perlmodlib</a> for details on mechanics and style issues in module creation.</p> <p>Perl modules are included into your program by saying</p> <pre><code>use Module;</code></pre> <p>or</p> <pre><code>use Module LIST;</code></pre> <p>This is exactly equivalent to</p> <pre><code class="plaintext">BEGIN { require Module; import Module; }</code></pre> <p>or</p> <pre><code class="plaintext">BEGIN { require Module; import Module LIST; }</code></pre> <p>As a special case</p> <pre><code>use Module ();</code></pre> <p>is exactly equivalent to</p> <pre><code class="plaintext">BEGIN { require Module; }</code></pre> <p>All Perl module files have the extension <i>.pm</i>. The <code>use</code> operator assumes this so you don&#39;t have to spell out &quot;<i>Module.pm</i>&quot; in quotes. This also helps to differentiate new modules from old <i>.pl</i> and <i>.ph</i> files. Module names are also capitalized unless they&#39;re functioning as pragmas; pragmas are in effect compiler directives, and are sometimes called &quot;pragmatic modules&quot; (or even &quot;pragmata&quot; if you&#39;re a classicist).</p> <p>The two statements:</p> <pre><code>require SomeModule; require &quot;SomeModule.pm&quot;; </code></pre> <p>differ from each other in two ways. In the first case, any double colons in the module name, such as <code>Some::Module</code>, are translated into your system&#39;s directory separator, usually &quot;/&quot;. The second case does not, and would have to be specified literally. The other difference is that seeing the first <code>require</code> clues in the compiler that uses of indirect object notation involving &quot;SomeModule&quot;, as in <code>$ob = purge SomeModule</code>, are method calls, not function calls. (Yes, this really can make a difference.)</p> <p>Because the <code>use</code> statement implies a <code>BEGIN</code> block, the importing of semantics happens as soon as the <code>use</code> statement is compiled, before the rest of the file is compiled. This is how it is able to function as a pragma mechanism, and also how modules are able to declare subroutines that are then visible as list or unary operators for the rest of the current file. This will not work if you use <code>require</code> instead of <code>use</code>. With <code>require</code> you can get into this problem:</p> <pre><code>require Cwd; # make Cwd:: accessible $here = Cwd::getcwd(); use Cwd; # import names from Cwd:: $here = getcwd(); require Cwd; # make Cwd:: accessible $here = getcwd(); # oops! no main::getcwd()</code></pre> <p>In general, <code>use Module ()</code> is recommended over <code>require Module</code>, because it determines module availability at compile time, not in the middle of your program&#39;s execution. An exception would be if two modules each tried to <code>use</code> each other, and each also called a function from that other module. In that case, it&#39;s easy to use <code>require</code>s instead.</p> <p>Perl packages may be nested inside other package names, so we can have package names containing <code>::</code>. But if we used that package name directly as a filename it would makes for unwieldy or impossible filenames on some systems. Therefore, if a module&#39;s name is, say, <code>Text::Soundex</code>, then its definition is actually found in the library file <i>Text/Soundex.pm</i>.</p> <p>Perl modules always have a <i>.pm</i> file, but there may also be dynamically linked executables (often ending in <i>.so</i>) or autoloaded subroutine definitions (often ending in <i>.al</i> associated with the module. If so, these will be entirely transparent to the user of the module. It is the responsibility of the <i>.pm</i> file to load (or arrange to autoload) any additional functionality. For example, although the POSIX module happens to do both dynamic loading and autoloading, but the user can say just <code>use POSIX</code> to get it all.</p> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p>See <a href="/5.6.0/perlmodlib">perlmodlib</a> for general style issues related to building Perl modules and classes, as well as descriptions of the standard library and CPAN, <a href="/5.6.0/Exporter">Exporter</a> for how Perl&#39;s standard import/export mechanism works, <a href="/5.6.0/perltoot">perltoot</a> and <a href="/5.6.0/perltootc">perltootc</a> for an in-depth tutorial on creating classes, <a href="/5.6.0/perlobj">perlobj</a> for a hard-core reference document on objects, <a href="/5.6.0/perlsub">perlsub</a> for an explanation of functions and scoping, and <a href="/5.6.0/perlxstut">perlxstut</a> and <a href="/5.6.0/perlguts">perlguts</a> for more information on writing extension modules.</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="/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

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