Note that there are some explanatory texts on larger screens.

plurals
  1. POCommon gotchas for Perl?
    text
    copied!<p>The question on <a href="https://stackoverflow.com/questions/161872/hidden-features-of-perl">Hidden features of Perl</a> yielded at least <a href="https://stackoverflow.com/questions/161872/hidden-features-of-perl#162249">one response</a> that could be regarded as either a feature or a mis-feature. It seemed logical to follow up with this question: what are common non-obvious mistakes in Perl? Things that seem like they ought to work, but don't.</p> <p>I won't give guidelines as to how to structure answers, or what's "too easy" to be considered a gotcha, since that's what the voting is for.</p> <h2>Table of Answers</h2> <p><strong>Syntax</strong></p> <ul> <li>General <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#166660">Single quotes instead of <code>::</code> in identifiers</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#167418">Indirect object syntax</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#166698">Confusing references with plain var types</a></li> </ul></li> <li>Filehandles <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#167342">Heredoc notation when using print with lexical filehandles</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#167122">Printing to a lexical filehandle contained in a hash</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#175231"><code>my</code> declarations should use parens around lists of variables</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas/549685#549685">Comparing strings with == and !=</a></li> </ul></li> </ul> <p><strong>Semantics/Language Features</strong></p> <ul> <li>General <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#167064"><code>do</code> is not a loop. You cannot <code>next</code>.</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#166804">Using the /o modifier with a regex</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas/1037460#1037460">Forgetting that <code>readdir</code>'s results are not relative to the CWD</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas/1373613#1373613">Unary minus's interaction with strings</a></li> </ul></li> <li>Context <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#166702">Assignment to scalar from arrays vs. lists</a></li> <li><a href="https://stackoverflow.com/questions/146329/what-is-the-worst-gotcha-youve-experienced#146394">The glob() iterator</a> (On a different question)</li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#175224">Implicit returns in list context</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#427595">Parenthesis changing the semantics of operators</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas/549324#549324">Calling context is propagated to return statements within functions</a></li> </ul></li> <li>Variables <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#166778">Can't localize exported variables without exporting the entire typeglob</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#167410">Using multiple variables (of different types) with the same name</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#167505"><code>while &lt;FH&gt;</code> does not localize <code>$_</code> automatically</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#167219">The Variable That's Validly Zero</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#169373">Constants can be redefined</a></li> </ul></li> </ul> <p><strong>Debugging</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#175285">Warning: Use of uninitialized value in concatenation</a></li> </ul> <p><strong>Best Practices</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#166682">Forgetting to <code>use strict</code> and <code>use warnings</code> (or <code>use diagnostics</code>)</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#169391">Misspelling variable names</a> (i.e., <code>use strict</code>, again)</li> </ul> <p><strong>Meta-Answers</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#168905">The perltrap manpage</a></li> <li><a href="https://stackoverflow.com/questions/166653/perl-common-gotchas#169443">Perl::Critic</a></li> </ul> <p>See Also: <a href="https://stackoverflow.com/questions/66117/aspnet-common-gotchas">ASP.NET - Common gotchas</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload