无法将散列和字符串传递给函数,在perl中一起!

前端之家收集整理的这篇文章主要介绍了无法将散列和字符串传递给函数,在perl中一起!前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我基本上试图将字符串和哈希传递给perl中的子例程.

sub coru_excel {
    my(%pushed_hash,$filename) = @_;
    print Dumper(%pushed_hash);
}

但似乎数据正在变得混乱.转储的数据还包括$filename.这是输出.

...................
$VAR7 = 'Address';
$VAR8 = [
          '223 VIA DE
................
        ];
$VAR9 = 'data__a.xls'     <----- $filename
$VAR10 = undef;
$VAR11 = 'DBA';
$VAR12 = [
           'J & L iNC
..................
         ];

以下是我调用子程序的方法.

coru_excel(%hash,"data_".$first."_".$last.".xls");

解决方法

参数作为一个无差别列表传递给子例程.

一种解决方案是颠倒参数的顺序,以便标量是第一个.

sub coru_excel {
    my($filename,%pushed_hash) = @_;
}

coru_excel("FILE_NAME",%hash);

另一种方法是通过引用传递哈希:

sub coru_excel {
    my($pushed_hash_ref,$filename) = @_;
}

coru_excel(\%hash,"FILE_NAME");

猜你在找的Perl相关文章