1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>strict - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Language" content="en-gb" />
<link href="css.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen" />
</head>
<script language="JavaScript" type="text/javascript" src="label.js"></script>
<script language="JavaScript">
pageDepth = 0;
setPath();
</script>
<body onLoad="showToolbars();loadLabels()">
<div id="pageHeader">
<div id="pageHeaderLogo">
<img src="onion.gif">
</div>
<div id="pageHeaderText">
<a href="http://perldoc.perl.org">perldoc.perl.org</a>
</div>
</div>
<div id="pageBody">
<div id="left">
<div id="leftContent">
<div id="leftClose">
<a href="#" onClick="closeLeft()" title="Hide navigation" onmouseover="leftCloseIcon.src='close_purple.gif';" onmouseout="leftCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="leftCloseIcon" id="leftCloseIcon" border=0></a>
</div>
<h1>Manual:</h1>
<ul>
<li><a href="index-overview.html">Overview</a></li>
<li><a href="index-tutorials.html">Tutorials</a></li>
<li><a href="index-faq.html">FAQs</a></li>
<li><a href="index-history.html">History / Changes</a></li>
<li><a href="index-licence.html">Licence</a></li>
</ul>
<h2>Reference:</h2>
<ul>
<li><a href="index-language.html">Language</a></li>
<li><a href="index-functions.html">Functions</a></li>
<li><a href="perlop.html">Operators</a></li>
<li><a href="perlvar.html">Special variables</a></li>
<li><a href="index-pragmas.html">Pragmas</a></li>
<li><a href="index-modules-A.html">Core modules</a></li>
<li><a href="index-utilities.html">Utilities</a></li>
<li><a href="index-internals.html">Internals</a></li>
<li><a href="index-platforms.html">Platform specific</a></li>
</ul>
<h2>Links:</h2>
<ul>
<li><a href="http://search.cpan.org">CPAN</a></li>
<li><a href="http://www.perl.org">Perl.org</a></li>
<li><a href="http://www.perl.com">Perl.com</a></li>
<li><a href="http://perlbuzz.com">Perl Buzz</a></li>
<li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
<li><a href="http://jobs.perl.org">Perl Jobs</a></li>
<li><a href="http://www.pm.org">Perl Mongers</a></li>
<li><a href="http://www.perlmonks.org">Perl Monks</a></li>
<li><a href="http://planet.perl.org">Planet Perl</a></li>
<li><a href="http://use.perl.org">Use Perl</a></li>
</ul>
<h2>Contact:</h2>
<ul>
<li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
(<a href="http://perl.jonallen.info">JJ</a>)</li>
<li class="spaced">Last updated on<br>23 December 2007</li>
<li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
more details</li>
</ul>
</div>
</div>
<div id="center">
<div id="centerContent">
<div id="contentHeader">
<div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
<div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
<div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
</div>
<div id="breadCrumbs"><a href="index.html">Home</a> > <a href="index-pragmas.html">Pragmas</a> > strict</div>
<script language="JavaScript">fromSearch();</script>
<div id="contentBody"><div class="title_container"><div class="page_title">strict</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#HISTORY">HISTORY</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>strict - Perl pragma to restrict unsafe constructs</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">"vars"</span><span class="sc">;</span>
<a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">"refs"</span><span class="sc">;</span>
<a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">"subs"</span><span class="sc">;</span></pre>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span>
<a class="l_k" href="functions/no.html">no</a> <span class="w">strict</span> <span class="q">"vars"</span><span class="sc">;</span></pre>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>If no import list is supplied, all possible restrictions are assumed.
(This is the safest mode to operate in, but is sometimes too strict for
casual programming.) Currently, there are three possible things to be
strict about: "subs", "vars", and "refs".</p>
<ul>
<li><a name="'strict-refs'"></a><b><code class="inline"><span class="w">strict</span> <span class="w">refs</span></code>
</b>
<p>This generates a runtime error if you
use symbolic references (see <a href="perlref.html">perlref</a>).</p>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">'refs'</span><span class="sc">;</span>
<span class="i">$ref</span> = \<span class="i">$foo</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$$ref</span><span class="sc">;</span> <span class="c"># ok</span>
<span class="i">$ref</span> = <span class="q">"foo"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$$ref</span><span class="sc">;</span> <span class="c"># runtime error; normally ok</span>
<span class="i">$file</span> = <span class="q">"STDOUT"</span><span class="sc">;</span>
<a class="l_k" href="functions/print.html">print</a> <span class="i">$file</span> <span class="q">"Hi!"</span><span class="sc">;</span> <span class="c"># error; note: no comma after $file</span></pre>
<p>There is one exception to this rule:</p>
<pre class="verbatim"> <span class="i">$bar</span> = \<span class="i">&</span>{<span class="q">'foo'</span>}<span class="sc">;</span>
<span class="i">&$bar</span><span class="sc">;</span></pre>
<p>is allowed so that <code class="inline"><a class="l_k" href="functions/goto.html">goto</a> <span class="i">&$AUTOLOAD</span></code>
would not break under stricture.</p>
</li>
<li><a name="'strict-vars'"></a><b><code class="inline"><span class="w">strict</span> <span class="w">vars</span></code>
</b>
<p>This generates a compile-time error if you access a variable that wasn't
declared via <code class="inline"><a class="l_k" href="functions/our.html">our</a></code> or <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">vars</span></code>
,
localized via <code class="inline"><a class="l_k" href="functions/my.html">my()</a></code>, or wasn't fully qualified. Because this is to avoid
variable suicide problems and subtle dynamic scoping issues, a merely
local() variable isn't good enough. See <a href="functions/my.html">my</a> and
<a href="functions/local.html">local</a>.</p>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">'vars'</span><span class="sc">;</span>
<span class="i">$X::foo</span> = <span class="n">1</span><span class="sc">;</span> <span class="c"># ok, fully qualified</span>
<a class="l_k" href="functions/my.html">my</a> <span class="i">$foo</span> = <span class="n">10</span><span class="sc">;</span> <span class="c"># ok, my() var</span>
<a class="l_k" href="functions/local.html">local</a> <span class="i">$foo</span> = <span class="n">9</span><span class="sc">;</span> <span class="c"># blows up</span></pre>
<pre class="verbatim"><a name="package-Cinna"></a> package <span class="i">Cinna</span><span class="sc">;</span>
<a class="l_k" href="functions/our.html">our</a> <span class="i">$bar</span><span class="sc">;</span> <span class="c"># Declares $bar in current package</span>
<span class="i">$bar</span> = <span class="q">'HgS'</span><span class="sc">;</span> <span class="c"># ok, global declared via pragma</span></pre>
<p>The local() generated a compile-time error because you just touched a global
name without fully qualifying it.</p>
<p>Because of their special use by sort(), the variables $a and $b are
exempted from this check.</p>
</li>
<li><a name="'strict-subs'"></a><b><code class="inline"><span class="w">strict</span> <span class="w">subs</span></code>
</b>
<p>This disables the poetry optimization, generating a compile-time error if
you try to use a bareword identifier that's not a subroutine, unless it
is a simple identifier (no colons) and that it appears in curly braces or
on the left hand side of the <code class="inline"><span class="cm">=></span></code>
symbol.</p>
<pre class="verbatim"> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span> <span class="q">'subs'</span><span class="sc">;</span>
<span class="i">$SIG</span>{<span class="w">PIPE</span>} = <span class="w">Plumber</span><span class="sc">;</span> <span class="c"># blows up</span>
<span class="i">$SIG</span>{<span class="w">PIPE</span>} = <span class="q">"Plumber"</span><span class="sc">;</span> <span class="c"># just fine: quoted string is always ok</span>
<span class="i">$SIG</span>{<span class="w">PIPE</span>} = \<span class="i">&Plumber</span><span class="sc">;</span> <span class="c"># preferred form</span></pre>
</li>
</ul>
<p>See <a href="perlmodlib.html#Pragmatic-Modules">"Pragmatic Modules" in perlmodlib</a>.</p>
<a name="HISTORY"></a><h1>HISTORY</h1>
<p><code class="inline"><span class="w">strict</span> <span class="q">'subs'</span></code>
, with Perl 5.6.1, erroneously permitted to use an unquoted
compound identifier (e.g. <code class="inline"><span class="w">Foo::Bar</span></code>
) as a hash key (before <code class="inline"><span class="cm">=></span></code>
or
inside curlies), but without forcing it always to a literal string.</p>
<p>Starting with Perl 5.8.1 strict is strict about its restrictions:
if unknown restrictions are used, the strict pragma will abort with</p>
<pre class="verbatim"> Unknown 'strict' tag(s) '...'</pre><p>As of version 1.04 (Perl 5.10), strict verifies that it is used as
"strict" to avoid the dreaded Strict trap on case insensitive file
systems.</p>
</div>
<div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>
</div>
</div>
<div id="right">
<div id="rightContent">
<div id="leftClose">
<a href="#" onClick="closeRight()" title="Hide toolbar" onmouseover="rightCloseIcon.src='close_purple.gif';" onmouseout="rightCloseIcon.src='close_blue.gif';"><img src="close_blue.gif" name="rightCloseIcon" id="rightCloseIcon" border=0></a>
</div>
<h1>Search:</h1>
<p>
<form action="search.html" name="perldoc_search">
<input type="text" name="q" size="10" class="grey"><br>
<!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
</form>
</p>
<script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
<h2>Labels:</h2>
<p>
<a href="#" onClick="addLabel('strict','strict.html')">Add this page</a>
</p>
<div class="labels" id="labels">
</div>
</div>
</div>
</div>
</body>
</html>
|