在这里得到哈希键/值是重现该问题的代码:在同一行函数调用
sub hello { return (h => 1, n => 1); }
print join ", ", values hello();
我得到的错误:
Type of arg 1 to values must be hash (not subroutine entry) at - line 4, near ");" Execution of - aborted due to compilation errors.
我知道我可以打破通话并在两行上打印:
sub hello { return (h => 1, n => 1); }
my %hash = hello();
print join ", ", values %hash;
但我不想这样做。有没有办法在一行中做到这一点,以便我不必一直创建临时变量?
它在实际的程序中一直很有用。如果您从API获取哈希值,并且您只需要键或值。 – tster 2010-07-30 20:26:50
@tster:这是不同的。当然'%{sub_returning_hash()}'很有用。但是问题是关于一个返回plist的子集(即包含键和值的连续列表),这在Perl中并不常见。 – Gilles 2010-07-30 20:51:57
返回散列的函数与将数组返回给调用函数的函数没有区别。另外,返回哈希的函数可能在你的perl中不常见,但我处理了很多。 – tster 2010-07-31 12:03:57