perl将单变量封装在双引号中 [英] perl encapsulate single variable in double quotes

查看:98
本文介绍了perl将单变量封装在双引号中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中,是否有任何理由将单个变量封装在双引号中(无串联)?



我经常在我的程序源中找到它正在工作(由10年前不再在这里工作的人所写):

  my $ sql_host = something; 
我的$ sql_user = somethingelse;

#向下几行
my $ db = sub_for_sql_conection( $ sql_host, $ sql_user, $ sql_pass, $ sql_db);

据我所知,没有理由这样做。当我使用旧脚本时,通常会删除引号,以便我的编辑器将引号着色为变量而不是字符串。为什么会这样。我错过了什么吗?



谢谢。

解决方案

所有这些这样做是明确地对变量进行字符串化。在99.9%的情况下,这是某种新手错误。



这种调用样式的副作用可能会发生一些事情:

  my $ foo = 1234; 
sub bar {$ _ [0] =〜s / 2 / two /}
print Foo is $ foo\n;
bar( $ foo);
print Foo is $ foo\n;
bar($ foo);
print Foo is $ foo\n;

在这里,字符串化创建了一个副本,并将其传递给子例程,从而避免了Perl的引用语义传递。通常认为这是对调用变量进行修改的一种不好的方式,因此您可能还可以。



您还可以在此处对对象或其他值进行字符串化。例如, undef 字符串化为空字符串。对象可以指定任意代码,以便在字符串化时运行。可能会有具有不同数值和字符串值的双值标量。



还有一种可能会发生的可怕现象。如果您正在使用XS代码,该代码查看在函数的标量参数上设置的标志,则对标量进行字符串化是对perl直截了当的表示方式,仅使用字符串标志,并为我提供一个很好的干净的新字符串值,没有数字标记。



我确信99.9%规则还有其他奇怪的例外。这些是一些。在删除引号之前,请花一秒钟时间检查是否有奇怪的废话。如果确实发生了合法使用情况,请添加一条注释,将引号标识为可行的合并,并给出其存在的理由。


In Perl, is there any reason to encapsulate a single variable in double quotes (no concatenation) ?

I often find this in the source of the program I am working on (writen 10 years ago by people that don't work here anymore):

my $sql_host = "something";
my $sql_user = "somethingelse";

# a few lines down
my $db = sub_for_sql_conection("$sql_host", "$sql_user", "$sql_pass", "$sql_db");

As far as I know there is no reason to do this. When I work in an old script I usualy remove the quotes so my editor colors them as variables not as strings.

I think they saw this somewhere and copied the style without understanding why it is so. Am I missing something ?

Thank you.

解决方案

All this does is explicitly stringify the variables. In 99.9% of cases, it is a newbie error of some sort.

There are things that may happen as a side effect of this calling style:

my $foo = "1234";
sub bar { $_[0] =~ s/2/two/ }
print "Foo is $foo\n";
bar( "$foo" );
print "Foo is $foo\n";
bar( $foo );
print "Foo is $foo\n";

Here, stringification created a copy and passed that to the subroutine, circumventing Perl's pass by reference semantics. It's generally considered to be bad manners to munge calling variables, so you are probably okay.

You can also stringify an object or other value here. For example, undef stringifies to the empty string. Objects may specify arbitrary code to run when stringified. It is possible to have dual valued scalars that have distinct numerical and string values. This is a way to specify that you want the string form.

There is also one deep spooky thing that could be going on. If you are working with XS code that looks at the flags that are set on scalar arguments to a function, stringifying the scalar is a straight forward way to say to perl, "Make me a nice clean new string value" with only stringy flags and no numeric flags.

I am sure there are other odd exceptions to the 99.9% rule. These are a few. Before removing the quotes, take a second to check for weird crap like this. If you do happen upon a legit usage, please add a comment that identifies the quotes as a workable kludge, and give their reason for existence.

这篇关于perl将单变量封装在双引号中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆