#!/usr/bin/perl -w
use strict;
use warnings;
use Class::Struct;
struct System => {
Name => '$',
};
my $system = new System;
$system->Name("Server1");
my $strout1 = qq{Server is ${$system->Name}\n};
my $strout2 = "Server is \"".$system->Name."\"\n";
print $strout1;
print $strout2;
结果是:
Can't use string ("Server1") as a SCALAR ref while "strict refs" in use at test.pl line 14.
我希望能够正确使用 qq 和 deref
$system->Name
。谁能解释我哪里出错了?
最佳答案
方法调用不会插入到双引号字符串中,但会取消引用。如果要插入方法调用的结果,则必须取消引用对它的引用:
my $strout1 = qq{Server is ${\$system->Name}\n};
关于perl - 在使用 ("Server1"时不能使用字符串 "strict refs") 作为 SCALAR ref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32866281/