加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl的常见陷阱?

发布时间:2020-12-15 21:29:09 所属栏目:大数据 来源:网络整理
导读:关于 Hidden features of Perl的问题产生至少 one response,其可以被认为是特征或错误特征。跟进这个问题似乎是合乎逻辑的:在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读