Perl的常见陷阱?

前端之家收集整理的这篇文章主要介绍了Perl的常见陷阱?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
关于 Hidden features of Perl的问题产生至少 one response,其可以被认为是特征或错误特征。跟进这个问题似乎是合乎逻辑的:在Perl中常见的非显而易见的错误是什么?似乎他们应该工作的东西,但不是。

我不会给出关于如何构造答案的指导方针,或者什么是“太容易”被认为是骗子,因为这是投票。

答案表

句法

>一般

> Single quotes instead of :: in identifiers
> Indirect object syntax
> Confusing references with plain var types

>文件句柄

> 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 !=

语义/语言特性

>一般

> 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

>上下文

> 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

>变量

> 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

调试

> Warning: Use of uninitialized value in concatenation

最佳实践

> Forgetting to use strict and use warnings (or use diagnostics)
> Misspelling variable names(即,再次使用strict)

元答案

> The perltrap manpage
> Perl::Critic

参见:ASP.NET – Common gotchas

解决方法

事实上,单引号可以用于替换::在标识符中。

考虑:

use strict;
print "$foo";        #-- Won't compile under use strict
print "$foo's fun!"; #-- Compiles just fine,refers to $foo::s

导致以下问题:

use strict;
my $name = "John";
print "$name's name is '$name'";
# prints:
#  name is 'John'

避免这种情况的推荐方法是在变量名称周围使用大括号:

print "${name}'s name is '$name'";
# John's name is 'John'

还要使用警告,因为它会告诉你使用未定义的变量$ name :: s

原文链接:https://www.f2er.com/Perl/173554.html

猜你在找的Perl相关文章