【问题标题】:Why perl omitting the @ strings为什么 perl 省略了 @ 字符串
【发布时间】:2018-09-29 23:17:48
【问题描述】:
#!/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.

谁能解释一下上面的内容。我相信它被认为是空数组,但是在读取文件时如何避免这种情况,请消除我的误解。

谢谢,

【问题讨论】:

  • 你可以在@前面加一个反斜杠或者用单引号代替双引号

标签: arrays string perl


【解决方案1】:

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

@test 被视为一个数组。

除非您明确地创建了它,否则当您尝试这样做时,您应该收到错误消息。如果你不这样做,那么你一定忘记了use strict;use warnings;

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

#!/usr/bin/env perl

use strict;
use warnings;
use v5.10;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2015-09-02
    相关资源
    最近更新 更多