如何解引用散列引用 [英] How to dereference hash references

查看:113
本文介绍了如何解引用散列引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我所知道的关于引用/解引用的所有内容都来自这里: http://www.thegeekstuff.com/2010/06/perl-array-reference-examples/



我正在使用一个库(从库文档):

返回对一个哈希引用数组的引用



这在概念上对我来说很有意义(我并不陌生于编程),但在功能上没有意义(我显然对Perl很新颖)。 b

以下是一些代码:

  my $ Obj = QA :: STK :: ModuleImUsing->新的(参数,新的); 
$ Obj-> createClient();
$ obj-> sync(['/ tmp / files / ...']);
my $ result = $ Obj-> method_in_question(['/ tmp / files / ff-latest.xml']);

到目前为止这么好。 $ result 现在拥有对数组的引用。



所以当我这样做时:

  print结果:@ {$ result} \\\
;

我得到:

 结果:HASH(0x20d95b0)

可爱!但我仍然需要取消引用散列。然而,这里的东西变得奇怪(或者他们已经变得奇怪了?)。

  my $ hash_ref = @ {$结果}; 
print Dump($ hash_ref));

我得到这个:

  $ VAR1 = 1; 

其中...不是我期待的。



我的期望是错误的还是我以错误的方式解引用事物?

解决方案

c $ c> @ $ result 是一个数组,那么你的LHS必须是一个列表。否则,将为 $ hashref 分配数组大小。

  my($ hash_ref)= @ {$ result}; 
print Dump($ hash_ref));


UPDATE: Everything I know about referencing/dereferencing came from here: http://www.thegeekstuff.com/2010/06/perl-array-reference-examples/

I'm working with a library that (from the library documentation):

Returns a reference to an array of hash references

This conceptually makes sense to me (i'm not new to programming) but doesn't make sense functionally (i'm, apparently, very new to perl).

Here's some code:

my $Obj  = QA::STK::ModuleImUsing->new(arguments, to, new);
$Obj->createClient();
$Obj->sync( ['/tmp/files/...']);
my $result = $Obj->method_in_question(['/tmp/files/ff-latest.xml']);

So far so good. $result now holds the reference to an array.

So when I do this:

print "Result: @{ $result} \n";

I get:

Result: HASH(0x20d95b0)

Lovely! But I still need to dereference the hash. However, here's where things get weird (or maybe they've already gotten weird?).

my $hash_ref = @{ $result};
print Dump($hash_ref));

I get this:

$VAR1 = 1;

Which...isn't what I was expecting at all.

Are my expectations wrong or am I dereferencing things in the wrong way?

解决方案

If @$result is an array then your LHS must be a list. Otherwise $hashref will be assigned the array size.

my ($hash_ref) = @{ $result};
print Dump($hash_ref));

这篇关于如何解引用散列引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆