perl - 在使用 ("Server1"时不能使用字符串 "strict refs") 作为 SCALAR ref

标签 perl class struct scalar

#!/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/

相关文章:

linux - 计算散列中的条目以及它们更改的频率

c++ - 手动对象构造函数调用

c - 使用数据结构来处理内存的个人 malloc 函数

c - 初始化结构时出现段错误

perl - 如何在 Perl 文件的顶部和底部添加行?

php - 使用 Perl 或 PHP 的 HTTP 多部分响应

python - Scipy最小化错误: 'numpy.float64' object is not callable

戈朗 : loop through fields of a struct modify them and and return the struct?

perl - 格式化日期时间输出

c++ - 在 for 循环中调用一个函数来删除 vector (或列表)中的元素