English 中文(简体)
是否在Perl发生污染?
原标题:Are there any drawbacks to interpolation in Perl?
最佳回答

举例来说,这只是一个简单的例子,即:$myVar,实际上更为复杂,例如,深层地疏漏了散射线,或采用某种方法。 某些东西在扼杀(大多数物体指指物),但方法没有吸引力。 另外,在直接打印成插图时,阵列也不同。

PS。 欢迎到Perl;请见旅程!

问题回答

www.un.org/Depts/DGACM/index_french.htm

$owner = "John";
$item = "motorcycle".
print "This is $owner s $item.
";  # Oops, parsed as $owner::s

但是,上述内容可以安全地写成。

print "This is ${owner} s $item.
";

有一些事情需要警惕,尽管你们知道这些事情,但你几乎从未犯过错误。

在有效识别文本旁边贴一个变称。 如果以前有定义,则每一方可找到最长久有效的变量名称,并且不予以照顾。 您可以将可变的名称部分从中删除,但可以明确:

  my $p =  p ;
  print "Mind your $ps and qs
";  # $ps, not $p

  print "Mind your ${p}s and qs";  # now its $p

现在,我就是这样说的。 如果我补充一下,我还有另一个问题,因为apos子是老时代遗留下来的包裹,它仍在发挥作用。 图书馆也在那里工作:

  my $p =  p ;
  print "Mind your $p s and q s
";  # $p::s, not $p

  print "Mind your ${p} s and q s";  # now its $p

Perl还可以干涉单项元素接触斜体和阵列,因此,在变称后面加列指数特性可能会使你不希望:

 print "The values are $string[$foo]
";  That s the element at index $foo
 print "The values are $string{$foo}
";  That s the value for the key $foo

当你想用电子邮件在座时,你可能会忘记 Perl 洲际阵列。 除非你逃脱@:

 print "Send me mail at joe@example.com
";  # interpolates @example

 print "Send me mail at joe@example.com
";

自2006年以来 如果你想一字不一的话,那么你就需要翻一番:

 print "C:
eal	oolsfor
ewwork";      # not what you might expect

 print "C:\real\tools\for\new\work"; # kinda ugly, but that s life
 print "C:/real/tools/for/new/work";      # Windows still understands this

尽管这些小小 go,但我确实不理解,如果我不得不使用另一种语言,我可以轻松地在Perl建造座椅。

方法A应当:

$myVar = "value";
print  Current value is  , $myVar, "
";

当你单独引用一个插图时,Perl确实是一纸 t,看着什么东西来争辩,这样,如果你有很长的插图,就不需要进行干涉,那么,像上文一样,使用单一引言和指动部分可能更快。

然而,这是一种微观刺激,实际上并没有带来巨大变化。

另一案件使用 方法 A. 如果扼杀含有你不希望穿梭的逃生特性:

$myVar = 12000;
print  Conversion from $US to $CND:  , $myVar,
";

在这种情况下,你不想请Perl查看$US$CND变量,你只是想在其中签字美元。

归根结底,它主要是一个风格问题。 我通常试图避免双重引用的扼杀,除非我需要。

既然我们谈论的是相互争执,值得一提的是,如果你利用单一引号施令来避免干涉,那么你仍然需要逃避一小 trail:

 c:files   #parse error
 c:files\  #correct

这是因为,第一种扼杀物的最终性质像是逃跑的单一引言,而不是扼杀装置。 逃跑的鞭lash也将转化为单一引诱中的一种lash。

如果你重新接触Perl,那么你会重新寻找最快的方式来做事。 此外,鼓励你以最类似于你的思维方式这样做(见Perl motto:TMTOWTDI, There s More Than One Way To Do It)。 因此,无论在你后来回到法典时,你最有可能理解的是哪一种方式,你都应这样做(其他所有事情,如给预期结果,都是平等的)。

如果你对“安全”表示担忧,那么就了解了tainting,这有助于保护你免受外部世界潜在的不实数据的影响。

(这很可能是社区维系。)

我所想的唯一明显问题是各阵列的干涉。 比较

print @arr, "
";

iii

print "@arr
";

Also, sometimes complicated dereferencing don t work well iii interpolation, but those are kind of rare.

我作为不熟悉任何具体编辑及其突出权力的Perl外部手,尽可能使用单一报价。 这使读者更清楚地看到,这种扼杀没有任何东西会以任何方式加以推断。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签