可以在不首先声明变量的情况下进行引用吗?

时间:2011-04-08 09:56:50

标签: arrays perl hash reference perl-data-structures

我有这个有效的代码

my @new = keys %h1;
my @old = keys %h2;

function(\@new, \@old);

但是可以在不必先声明变量的情况下完成吗?

function必须将其参数作为引用

2 个答案:

答案 0 :(得分:7)

use strict;
use Data::Dumper;

my %test = (key1 => "value",key2 => "value2");
my %test2 = (key3 => "value3",key4 => "value4");

test_sub([keys %test], [keys %test2]);

sub test_sub{
 my $ref_arr = shift;
 my $ref_arr2 = shift;
 print Dumper($ref_arr);
 print Dumper($ref_arr2);
}

输出:

$VAR1 = [
          'key2',
          'key1'
        ];
$VAR1 = [
          'key4',
          'key3'
        ];

答案 1 :(得分:6)

function([ keys %h1 ], [ keys %h2 ]);

来自perldoc perlref

  

对匿名数组的引用可以   使用方括号创建:

     

$ arrayref = [1,2,['a','b','c']];

相关问题