为什么perl省略@字符串

时间:2018-04-19 12:36:22

标签: arrays string perl

#!/usr/bin/perl

$test ="@test=hello"; #(Actually am reading this text from File and assigning to a variable)

print "$test";

执行上面的代码后,perl输出以下

输出

=hello instead of @test=hello.

任何人都可以解释以上内容。我相信它正在考虑作为空数组,但是如何在阅读文件时避免这种情况,请清除我的误解。

谢谢,

1 个答案:

答案 0 :(得分:3)

Perl在由双引号分隔的字符串中插入变量。

@test被视为数组。

除非您明确创建了这个,否则当您尝试执行此操作时, 会出现错误。如果不这样做,那么您必须忘记use strict;use warnings;

如果您不想插入变量,请使用单引号字符串。

#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;

my $test = '@test=hello'; 
say $test;