CINXE.COM

perltrap - Perl traps for the unwary - 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>perltrap - Perl traps for the unwary - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/perltrap"> <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.41.1</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/perltrap">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/perltrap">5.40.1</a> <a class="dropdown-item" href="/5.40.0/perltrap">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/perltrap">5.38.3</a> <a class="dropdown-item" href="/5.38.2/perltrap">5.38.2</a> <a class="dropdown-item" href="/5.38.1/perltrap">5.38.1</a> <a class="dropdown-item" href="/5.38.0/perltrap">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/perltrap">5.36.3</a> <a class="dropdown-item" href="/5.36.2/perltrap">5.36.2</a> <a class="dropdown-item" href="/5.36.1/perltrap">5.36.1</a> <a class="dropdown-item" href="/5.36.0/perltrap">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/perltrap">5.34.3</a> <a class="dropdown-item" href="/5.34.2/perltrap">5.34.2</a> <a class="dropdown-item" href="/5.34.1/perltrap">5.34.1</a> <a class="dropdown-item" href="/5.34.0/perltrap">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/perltrap">5.32.1</a> <a class="dropdown-item" href="/5.32.0/perltrap">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/perltrap">5.30.3</a> <a class="dropdown-item" href="/5.30.2/perltrap">5.30.2</a> <a class="dropdown-item" href="/5.30.1/perltrap">5.30.1</a> <a class="dropdown-item" href="/5.30.0/perltrap">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perltrap">5.28.3</a> <a class="dropdown-item" href="/5.28.2/perltrap">5.28.2</a> <a class="dropdown-item" href="/5.28.1/perltrap">5.28.1</a> <a class="dropdown-item" href="/5.28.0/perltrap">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/perltrap">5.26.3</a> <a class="dropdown-item" href="/5.26.2/perltrap">5.26.2</a> <a class="dropdown-item" href="/5.26.1/perltrap">5.26.1</a> <a class="dropdown-item" href="/5.26.0/perltrap">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/perltrap">5.24.4</a> <a class="dropdown-item" href="/5.24.3/perltrap">5.24.3</a> <a class="dropdown-item" href="/5.24.2/perltrap">5.24.2</a> <a class="dropdown-item" href="/5.24.1/perltrap">5.24.1</a> <a class="dropdown-item" href="/5.24.0/perltrap">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/perltrap">5.22.4</a> <a class="dropdown-item" href="/5.22.3/perltrap">5.22.3</a> <a class="dropdown-item" href="/5.22.2/perltrap">5.22.2</a> <a class="dropdown-item" href="/5.22.1/perltrap">5.22.1</a> <a class="dropdown-item" href="/5.22.0/perltrap">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perltrap">5.20.3</a> <a class="dropdown-item" href="/5.20.2/perltrap">5.20.2</a> <a class="dropdown-item" href="/5.20.1/perltrap">5.20.1</a> <a class="dropdown-item" href="/5.20.0/perltrap">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/perltrap">5.18.4</a> <a class="dropdown-item" href="/5.18.3/perltrap">5.18.3</a> <a class="dropdown-item" href="/5.18.2/perltrap">5.18.2</a> <a class="dropdown-item" href="/5.18.1/perltrap">5.18.1</a> <a class="dropdown-item" href="/5.18.0/perltrap">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/perltrap">5.16.3</a> <a class="dropdown-item" href="/5.16.2/perltrap">5.16.2</a> <a class="dropdown-item" href="/5.16.1/perltrap">5.16.1</a> <a class="dropdown-item" href="/5.16.0/perltrap">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perltrap">5.14.4</a> <a class="dropdown-item" href="/5.14.3/perltrap">5.14.3</a> <a class="dropdown-item" href="/5.14.2/perltrap">5.14.2</a> <a class="dropdown-item" href="/5.14.1/perltrap">5.14.1</a> <a class="dropdown-item" href="/5.14.0/perltrap">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/perltrap">5.12.5</a> <a class="dropdown-item" href="/5.12.4/perltrap">5.12.4</a> <a class="dropdown-item" href="/5.12.3/perltrap">5.12.3</a> <a class="dropdown-item" href="/5.12.2/perltrap">5.12.2</a> <a class="dropdown-item" href="/5.12.1/perltrap">5.12.1</a> <a class="dropdown-item" href="/5.12.0/perltrap">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/perltrap">5.10.1</a> <a class="dropdown-item" href="/5.10.0/perltrap">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perltrap">5.8.9</a> <a class="dropdown-item" href="/5.8.8/perltrap">5.8.8</a> <a class="dropdown-item" href="/5.8.7/perltrap">5.8.7</a> <a class="dropdown-item" href="/5.8.6/perltrap">5.8.6</a> <a class="dropdown-item" href="/5.8.5/perltrap">5.8.5</a> <a class="dropdown-item" href="/5.8.4/perltrap">5.8.4</a> <a class="dropdown-item" href="/5.8.3/perltrap">5.8.3</a> <a class="dropdown-item" href="/5.8.2/perltrap">5.8.2</a> <a class="dropdown-item" href="/5.8.1/perltrap">5.8.1</a> <a class="dropdown-item" href="/5.8.0/perltrap">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perltrap">5.6.2</a> <a class="dropdown-item" href="/5.6.1/perltrap">5.6.1</a> <a class="dropdown-item" href="/5.6.0/perltrap">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/perltrap">5.005_04</a> <a class="dropdown-item" href="/5.005_03/perltrap">5.005_03</a> <a class="dropdown-item" href="/5.005_02/perltrap">5.005_02</a> <a class="dropdown-item" href="/5.005_01/perltrap">5.005_01</a> <a class="dropdown-item" href="/5.005/perltrap">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/perltrap">blead</a> <a class="dropdown-item" href="/5.41.10/perltrap">5.41.10</a> <a class="dropdown-item" href="/5.41.9/perltrap">5.41.9</a> <a class="dropdown-item" href="/5.41.8/perltrap">5.41.8</a> <a class="dropdown-item" href="/5.41.7/perltrap">5.41.7</a> <a class="dropdown-item" href="/5.41.6/perltrap">5.41.6</a> <a class="dropdown-item" href="/5.41.5/perltrap">5.41.5</a> <a class="dropdown-item" href="/5.41.4/perltrap">5.41.4</a> <a class="dropdown-item" href="/5.41.3/perltrap">5.41.3</a> <a class="dropdown-item" href="/5.41.2/perltrap">5.41.2</a> <a class="dropdown-item active" href="/5.41.1/perltrap">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/perltrap">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/perltrap">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/perltrap">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perltrap">5.39.10</a> <a class="dropdown-item" href="/5.39.9/perltrap">5.39.9</a> <a class="dropdown-item" href="/5.39.8/perltrap">5.39.8</a> <a class="dropdown-item" href="/5.39.7/perltrap">5.39.7</a> <a class="dropdown-item" href="/5.39.6/perltrap">5.39.6</a> <a class="dropdown-item" href="/5.39.5/perltrap">5.39.5</a> <a class="dropdown-item" href="/5.39.4/perltrap">5.39.4</a> <a class="dropdown-item" href="/5.39.3/perltrap">5.39.3</a> <a class="dropdown-item" href="/5.39.2/perltrap">5.39.2</a> <a class="dropdown-item" href="/5.39.1/perltrap">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/perltrap">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.41.1/perl">Perl</a> <a class="dropdown-item" href="/5.41.1/perlintro">Intro</a> <a class="dropdown-item" href="/5.41.1/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.41.1/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.41.1/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.41.1/perlop">Operators</a> <a class="dropdown-item" href="/5.41.1/functions">Functions</a> <a class="dropdown-item" href="/5.41.1/variables">Variables</a> <a class="dropdown-item" href="/5.41.1/modules">Modules</a> <a class="dropdown-item" href="/5.41.1/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.41.1/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.41.1/perlcommunity">Community</a> <a class="dropdown-item" href="/5.41.1/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.41.1/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.41.1/perltrap">perltrap</a> <div id="more"> (<a href="/5.41.1/perltrap.txt">source</a>, <a href="https://metacpan.org/pod/perltrap">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.41.1. 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="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#Awk-Traps">Awk Traps</a> </li> <li> <a class="text-decoration-none" href="#C/C++-Traps">C/C++ Traps</a> </li> <li> <a class="text-decoration-none" href="#JavaScript-Traps">JavaScript Traps</a> </li> <li> <a class="text-decoration-none" href="#Sed-Traps">Sed Traps</a> </li> <li> <a class="text-decoration-none" href="#Shell-Traps">Shell Traps</a> </li> <li> <a class="text-decoration-none" href="#Perl-Traps">Perl Traps</a> </li> </ul> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>perltrap - Perl traps for the unwary</p> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>The biggest trap of all is forgetting to <code>use warnings</code> or use the <b>-w</b> switch; see <a href="/5.41.1/warnings">warnings</a> and <a href="/5.41.1/perlrun#-w">&quot;-w&quot; in perlrun</a>. The second biggest trap is not making your entire program runnable under <code>use strict</code>. The third biggest trap is not reading the list of changes in this version of Perl; see <a href="/5.41.1/perldelta">perldelta</a>.</p> <h2 id="Awk-Traps"><a class="permalink" href="#Awk-Traps">#</a><a id="Awk"></a>Awk Traps</h2> <p>Accustomed <b>awk</b> users should take special note of the following:</p> <ul> <li><p>A Perl program executes only once, not once for each input line. You can do an implicit loop with <code>-n</code> or <code>-p</code>.</p> </li> <li><p>The English module, loaded via</p> <pre><code>use English;</code></pre> <p>allows you to refer to special variables (like <code>$/</code>) with names (like $RS), as though they were in <b>awk</b>; see <a href="/5.41.1/perlvar">perlvar</a> for details.</p> </li> <li><p>Semicolons are required after all simple statements in Perl (except at the end of a block). Newline is not a statement delimiter.</p> </li> <li><p>Curly brackets are required on <code>if</code>s and <code>while</code>s.</p> </li> <li><p>Variables begin with &quot;$&quot;, &quot;@&quot; or &quot;%&quot; in Perl.</p> </li> <li><p>Arrays index from 0. Likewise string positions in substr() and index().</p> </li> <li><p>You have to decide whether your array has numeric or string indices.</p> </li> <li><p>Hash values do not spring into existence upon mere reference.</p> </li> <li><p>You have to decide whether you want to use string or numeric comparisons.</p> </li> <li><p>Reading an input line does not split it for you. You get to split it to an array yourself. And the split() operator has different arguments than <b>awk</b>&#39;s.</p> </li> <li><p>The current input line is normally in $_, not $0. It generally does not have the newline stripped. ($0 is the name of the program executed.) See <a href="/5.41.1/perlvar">perlvar</a>.</p> </li> <li><p>$&lt;<i>digit</i>&gt; does not refer to fields--it refers to substrings matched by the last match pattern.</p> </li> <li><p>The print() statement does not add field and record separators unless you set <code>$,</code> and <code>$\</code>. You can set $OFS and $ORS if you&#39;re using the English module.</p> </li> <li><p>You must open your files before you print to them.</p> </li> <li><p>The range operator is &quot;..&quot;, not comma. The comma operator works as in C.</p> </li> <li><p>The match operator is &quot;=~&quot;, not &quot;~&quot;. (&quot;~&quot; is the one&#39;s complement operator, as in C.)</p> </li> <li><p>The exponentiation operator is &quot;**&quot;, not &quot;^&quot;. &quot;^&quot; is the XOR operator, as in C. (You know, one could get the feeling that <b>awk</b> is basically incompatible with C.)</p> </li> <li><p>The concatenation operator is &quot;.&quot;, not the null string. (Using the null string would render <code>/pat/ /pat/</code> unparsable, because the third slash would be interpreted as a division operator--the tokenizer is in fact slightly context sensitive for operators like &quot;/&quot;, &quot;?&quot;, and &quot;&gt;&quot;. And in fact, &quot;.&quot; itself can be the beginning of a number.)</p> </li> <li><p>The <code>next</code>, <code>exit</code>, and <code>continue</code> keywords work differently.</p> </li> <li><p>The following variables work differently:</p> <pre><code>Awk Perl ARGC scalar @ARGV (compare with $#ARGV) ARGV[0] $0 FILENAME $ARGV FNR $. - something FS (whatever you like) NF $#Fld, or some such NR $. OFMT $# OFS $, ORS $\ RLENGTH length($&amp;) RS $/ RSTART length($`) SUBSEP $;</code></pre> </li> <li><p>You cannot set $RS to a pattern, only a string.</p> </li> <li><p>When in doubt, run the <b>awk</b> construct through <b>a2p</b> and see what it gives you.</p> </li> </ul> <h2 id="C/C++-Traps"><a class="permalink" href="#C/C++-Traps">#</a><a id="C"></a><a id="C-C-Traps"></a>C/C++ Traps</h2> <p>Cerebral C and C++ programmers should take note of the following:</p> <ul> <li><p>Curly brackets are required on <code>if</code>&#39;s and <code>while</code>&#39;s.</p> </li> <li><p>You must use <code>elsif</code> rather than <code>else if</code>.</p> </li> <li><p>The <code>break</code> and <code>continue</code> keywords from C become in Perl <code>last</code> and <code>next</code>, respectively. Unlike in C, these do <i>not</i> work within a <code>do { } while</code> construct. See <a href="/5.41.1/perlsyn#Loop-Control">&quot;Loop Control&quot; in perlsyn</a>.</p> </li> <li><p>The switch statement is called <code>given</code>/<code>when</code> and only available in perl 5.10 or newer. See <a href="/5.41.1/perlsyn#Switch-Statements">&quot;Switch Statements&quot; in perlsyn</a>.</p> </li> <li><p>Variables begin with &quot;$&quot;, &quot;@&quot; or &quot;%&quot; in Perl.</p> </li> <li><p>Comments begin with &quot;#&quot;, not &quot;/*&quot; or &quot;//&quot;. Perl may interpret C/C++ comments as division operators, unterminated regular expressions or the defined-or operator.</p> </li> <li><p>You can&#39;t take the address of anything, although a similar operator in Perl is the backslash, which creates a reference.</p> </li> <li><p><code>ARGV</code> must be capitalized. <code>$ARGV[0]</code> is C&#39;s <code>argv[1]</code>, and <code>argv[0]</code> ends up in <code>$0</code>.</p> </li> <li><p>System calls such as link(), unlink(), rename(), etc. return nonzero for success, not 0. (system(), however, returns zero for success.)</p> </li> <li><p>Signal handlers deal with signal names, not numbers. Use <code>kill -l</code> to find their names on your system.</p> </li> </ul> <h2 id="JavaScript-Traps"><a class="permalink" href="#JavaScript-Traps">#</a><a id="JavaScript"></a>JavaScript Traps</h2> <p>Judicious JavaScript programmers should take note of the following:</p> <ul> <li><p>In Perl, binary <code>+</code> is always addition. <code>$string1 + $string2</code> converts both strings to numbers and then adds them. To concatenate two strings, use the <code>.</code> operator.</p> </li> <li><p>The <code>+</code> unary operator doesn&#39;t do anything in Perl. It exists to avoid syntactic ambiguities.</p> </li> <li><p>Unlike <code>for...in</code>, Perl&#39;s <code>for</code> (also spelled <code>foreach</code>) does not allow the left-hand side to be an arbitrary expression. It must be a variable:</p> <pre><code> for my $variable (keys %hash) { ... }</code></pre> <p>Furthermore, don&#39;t forget the <code>keys</code> in there, as <code>foreach my $kv (%hash) {}</code> iterates over the keys and values, and is generally not useful ($kv would be a key, then a value, and so on).</p> </li> <li><p>To iterate over the indices of an array, use <code>foreach my $i (0 .. $#array) {}</code>. <code>foreach my $v (@array) {}</code> iterates over the values.</p> </li> <li><p>Perl requires braces following <code>if</code>, <code>while</code>, <code>foreach</code>, etc.</p> </li> <li><p>In Perl, <code>else if</code> is spelled <code>elsif</code>.</p> </li> <li><p><code>? :</code> has higher precedence than assignment. In JavaScript, one can write:</p> <pre><code class="plaintext">condition ? do_something() : variable = 3</code></pre> <p>and the variable is only assigned if the condition is false. In Perl, you need parentheses:</p> <pre><code>$condition ? do_something() : ($variable = 3);</code></pre> <p>Or just use <code>if</code>.</p> </li> <li><p>Perl requires semicolons to separate statements.</p> </li> <li><p>Variables declared with <code>my</code> only affect code <i>after</i> the declaration. You cannot write <code>$x = 1; my $x;</code> and expect the first assignment to affect the same variable. It will instead assign to an <code>$x</code> declared previously in an outer scope, or to a global variable.</p> <p>Note also that the variable is not visible until the following <i>statement</i>. This means that in <code>my $x = 1 + $x</code> the second $x refers to one declared previously.</p> </li> <li><p><code>my</code> variables are scoped to the current block, not to the current function. If you write <code>{my $x;} $x;</code>, the second <code>$x</code> does not refer to the one declared inside the block.</p> </li> <li><p>An object&#39;s members cannot be made accessible as variables. The closest Perl equivalent to <code>with(object) { method() }</code> is <code>for</code>, which can alias <code>$_</code> to the object:</p> <pre><code> for ($object) { $_-&gt;method; }</code></pre> </li> <li><p>The object or class on which a method is called is passed as one of the method&#39;s arguments, not as a separate <code>this</code> value.</p> </li> </ul> <h2 id="Sed-Traps"><a class="permalink" href="#Sed-Traps">#</a><a id="Sed"></a>Sed Traps</h2> <p>Seasoned <b>sed</b> programmers should take note of the following:</p> <ul> <li><p>A Perl program executes only once, not once for each input line. You can do an implicit loop with <code>-n</code> or <code>-p</code>.</p> </li> <li><p>Backreferences in substitutions use &quot;$&quot; rather than &quot;\&quot;.</p> </li> <li><p>The pattern matching metacharacters &quot;(&quot;, &quot;)&quot;, and &quot;|&quot; do not have backslashes in front.</p> </li> <li><p>The range operator is <code>...</code>, rather than comma.</p> </li> </ul> <h2 id="Shell-Traps"><a class="permalink" href="#Shell-Traps">#</a><a id="Shell"></a>Shell Traps</h2> <p>Sharp shell programmers should take note of the following:</p> <ul> <li><p>The backtick operator does variable interpolation without regard to the presence of single quotes in the command.</p> </li> <li><p>The backtick operator does no translation of the return value, unlike <b>csh</b>.</p> </li> <li><p>Shells (especially <b>csh</b>) do several levels of substitution on each command line. Perl does substitution in only certain constructs such as double quotes, backticks, angle brackets, and search patterns.</p> </li> <li><p>Shells interpret scripts a little bit at a time. Perl compiles the entire program before executing it (except for <code>BEGIN</code> blocks, which execute at compile time).</p> </li> <li><p>The arguments are available via @ARGV, not $1, $2, etc.</p> </li> <li><p>The environment is not automatically made available as separate scalar variables.</p> </li> <li><p>The shell&#39;s <code>test</code> uses &quot;=&quot;, &quot;!=&quot;, &quot;&lt;&quot; etc for string comparisons and &quot;-eq&quot;, &quot;-ne&quot;, &quot;-lt&quot; etc for numeric comparisons. This is the reverse of Perl, which uses <code>eq</code>, <code>ne</code>, <code>lt</code> for string comparisons, and <code>==</code>, <code>!=</code> <code>&lt;</code> etc for numeric comparisons.</p> </li> </ul> <h2 id="Perl-Traps"><a class="permalink" href="#Perl-Traps">#</a><a id="Perl"></a>Perl Traps</h2> <p>Practicing Perl Programmers should take note of the following:</p> <ul> <li><p>Remember that many operations behave differently in a list context than they do in a scalar one. See <a href="/5.41.1/perldata">perldata</a> for details.</p> </li> <li><p>Avoid barewords if you can, especially all lowercase ones. You can&#39;t tell by just looking at it whether a bareword is a function or a string. By using quotes on strings and parentheses on function calls, you won&#39;t ever get them confused.</p> </li> <li><p>You cannot discern from mere inspection which builtins are unary operators (like chop() and chdir()) and which are list operators (like print() and unlink()). (Unless prototyped, user-defined subroutines can <b>only</b> be list operators, never unary ones.) See <a href="/5.41.1/perlop">perlop</a> and <a href="/5.41.1/perlsub">perlsub</a>.</p> </li> <li><p>People have a hard time remembering that some functions default to $_, or @ARGV, or whatever, but that others which you might expect to do not.</p> </li> <li><p>The &lt;FH&gt; construct is not the name of the filehandle, it is a readline operation on that handle. The data read is assigned to $_ only if the file read is the sole condition in a while loop:</p> <pre><code>while (&lt;FH&gt;) { } while (defined($_ = &lt;FH&gt;)) { }.. &lt;FH&gt;; # data discarded!</code></pre> </li> <li><p>Remember not to use <code>=</code> when you need <code>=~</code>; these two constructs are quite different:</p> <pre><code>$x = /foo/; $x =~ /foo/;</code></pre> </li> <li><p>The <code>do {}</code> construct isn&#39;t a real loop that you can use loop control on.</p> </li> <li><p>Use <code>my()</code> for local variables whenever you can get away with it (but see <a href="/5.41.1/perlform">perlform</a> for where you can&#39;t). Using <code>local()</code> actually gives a local value to a global variable, which leaves you open to unforeseen side-effects of dynamic scoping.</p> </li> <li><p>If you localize an exported variable in a module, its exported value will not change. The local name becomes an alias to a new value but the external name is still an alias for the original.</p> </li> </ul> <p>As always, if any of these are ever officially declared as bugs, they&#39;ll be fixed and removed.</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>

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