关于
Hidden features of Perl的问题产生至少
one response,其可以被认为是特征或错误特征。跟进这个问题似乎是合乎逻辑的:在Perl中常见的非显而易见的错误是什么?似乎他们应该工作的东西,但不是。
@H_301_2@我不会给出关于如何构造答案的指导方针,或者什么是“太容易”被认为是骗子,因为这是投票。
@H_301_2@答案表
@H_301_2@句法
@H_301_2@>一般
@H_301_2@> Single quotes instead of
> Indirect object syntax
> Confusing references with plain var types @H_301_2@>文件句柄 @H_301_2@> Heredoc notation when using print with lexical filehandles
> Printing to a lexical filehandle contained in a hash
>
> Comparing strings with == and != @H_301_2@语义/语言特性 @H_301_2@>一般 @H_301_2@>
> Using the /o modifier with a regex
> Forgetting that
> Unary minus’s interaction with strings @H_301_2@>上下文 @H_301_2@> Assignment to scalar from arrays vs. lists
> The glob() iterator(另一个问题)
> Implicit returns in list context
> Parenthesis changing the semantics of operators
> Calling context is propagated to return statements within functions @H_301_2@>变量 @H_301_2@> Can’t localize exported variables without exporting the entire typeglob
> Using multiple variables (of different types) with the same name
>
> The Variable That’s Validly Zero
> Constants can be redefined @H_301_2@调试 @H_301_2@> Warning: Use of uninitialized value in concatenation @H_301_2@最佳实践 @H_301_2@> Forgetting to
> Misspelling variable names(即,再次使用strict) @H_301_2@元答案 @H_301_2@> The perltrap manpage
> Perl::Critic @H_301_2@参见:ASP.NET – Common gotchas
::
in identifiers> Indirect object syntax
> Confusing references with plain var types @H_301_2@>文件句柄 @H_301_2@> Heredoc notation when using print with lexical filehandles
> Printing to a lexical filehandle contained in a hash
>
my
declarations should use parens around lists of variables> Comparing strings with == and != @H_301_2@语义/语言特性 @H_301_2@>一般 @H_301_2@>
do
is not a loop. You cannot next
.> Using the /o modifier with a regex
> Forgetting that
readdir
‘s results are not relative to the CWD> Unary minus’s interaction with strings @H_301_2@>上下文 @H_301_2@> Assignment to scalar from arrays vs. lists
> The glob() iterator(另一个问题)
> Implicit returns in list context
> Parenthesis changing the semantics of operators
> Calling context is propagated to return statements within functions @H_301_2@>变量 @H_301_2@> Can’t localize exported variables without exporting the entire typeglob
> Using multiple variables (of different types) with the same name
>
while <FH>
does not localize $_
automatically> The Variable That’s Validly Zero
> Constants can be redefined @H_301_2@调试 @H_301_2@> Warning: Use of uninitialized value in concatenation @H_301_2@最佳实践 @H_301_2@> Forgetting to
use strict
and use warnings
(or use diagnostics
)> Misspelling variable names(即,再次使用strict) @H_301_2@元答案 @H_301_2@> The perltrap manpage
> Perl::Critic @H_301_2@参见:ASP.NET – Common gotchas
解决方法
事实上,单引号可以用于替换::在标识符中。
@H_301_2@考虑:
use strict; print "$foo"; #-- Won't compile under use strict print "$foo's fun!"; #-- Compiles just fine,refers to $foo::s@H_301_2@导致以下问题:
use strict; my $name = "John"; print "$name's name is '$name'"; # prints: # name is 'John'@H_301_2@避免这种情况的推荐方法是在变量名称周围使用大括号:
print "${name}'s name is '$name'"; # John's name is 'John'@H_301_2@还要使用警告,因为它会告诉你使用未定义的变量$ name :: s