在Perl中,点等于是什么意思? [英] What does dot-equals mean in Perl?

查看:242
本文介绍了在Perl中,点等于是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.="在Perl中是什么意思(点等于)?下面的示例代码(在while子句中):

What does ".=" mean in Perl (dot-equals)? Example code below (in the while clause):

if( my $file = shift @ARGV ) {
    $parser->parse( Source => {SystemId => $file} );
} else {
    my $input = "";
    while( <STDIN> ) { $input .= $_; }
    $parser->parse( Source => {String => $input} );
}
exit;

感谢您的见解.

推荐答案

期间.串联运算符.右边的等号表示它是赋值运算符,就像在C中一样.

The period . is the concatenation operator. The equal sign to the right means that this is an assignment operator, like in C.

例如:

$input .= $_;

$input = $input . $_;

但是,这还具有一些perl的魔力,例如,这消除了初始化变量以避免未初始化"警告的需要.尝试不同之处:

However, there's also some perl magic in this, for example this removes the need to initialize a variable to avoid "uninitialized" warnings. Try the difference:

perl -we 'my $x; $x = $x + 1'   # Use of uninitialized value in addition ...
perl -we 'my $x; $x += 1'       # no warning

这意味着您的代码行:

my $input = "";

非常多余.尽管有些人会觉得很舒服.

Is quite redundant. Albeit some people might find it comforting.

这篇关于在Perl中,点等于是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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