简体   繁体   English

如何在Perl中将字符串作为一个元素数组引用返回?

[英]How do I return a string as a one element array reference in Perl?

I'm new to Perl, and this thing has got me stuck for far too long... 我是Perl的新手,而这件事让我陷入困境的时间太长了...

I want to dump a readable representation of the object itself from inside a function (I'm trying to debug something, and I'm doing this by returning an array reference which the caller expects, but containing an object dump rather than human readable text as per normal) so in my package I have: 我想从函数内部转储对象本身的可读表示形式(我正在尝试调试某些东西,我通过返回调用者期望的数组引用来做到这一点,但它包含对象转储而不是人类可读的文本按照常规),所以在我的包裹中,我有:

use Data::Dumper;

sub somefunctionName{

my $self = shift;
my $d = Dumper($self);
my @retval = ();
push(@retval, $d);
return \@retval;
}

This is giving me the error "Can't use string ("the literal object dump goes here") as a HASH ref while "strict refs" in use" 这给了我错误“在使用“严格引用”时,不能将字符串(“文字对象转储到这里”使用)作为哈希引用”

I can't for the life of me figure out a way to make the error go away, no matter how I mess with the backslashes, and what I've done above looks to me like exactly what every online tutorial does... But I'm obviously missing the point somewhere. 我不能为我的生活想出一个办法,使错误走了,不管我怎么惹反斜杠,以及我之前所做的那样看起来对我来说正是每一个在线教程呢......但我显然在某处错过了重点。

What am I doing wrong? 我究竟做错了什么?

According to the documentation 根据文档

  • Dumper(LIST) 自卸车(LIST)

    Returns the stringified form of the values in the list, subject to the configuration options below. 根据下面的配置选项,返回列表中值的字符串化形式。 The values will be named $VAR n in the output, where n is a numeric suffix. 这些值将在输出中命名为$ VAR n,其中n是数字后缀。 Will return a list of strings in a list context. 将在列表上下文中返回字符串列表。

You should be able to do 你应该能够做

@retval = Dumper($self);
return \@retval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM