我基本上试图将字符串和哈希传递给perl中的子例程.
sub coru_excel {
my(%pushed_hash, $filename) = @_;
print Dumper(%pushed_hash);
}
Run Code Online (Sandbox Code Playgroud)
但似乎数据正在变得混乱.转储的数据还包括$filename.这是输出.
...................
$VAR7 = 'Address';
$VAR8 = [
'223 VIA DE
................
];
$VAR9 = 'data__a.xls' <----- $filename
$VAR10 = undef;
$VAR11 = 'DBA';
$VAR12 = [
'J & L iNC
..................
];
Run Code Online (Sandbox Code Playgroud)
以下是我调用子程序的方法.
coru_excel(%hash, "data_".$first."_".$last.".xls");
Run Code Online (Sandbox Code Playgroud)
FMc*_*FMc 10
参数作为一个无差别列表传递给子例程.
一种解决方案是颠倒参数的顺序,以便标量是第一个.
sub coru_excel {
my($filename, %pushed_hash) = @_;
}
coru_excel("FILE_NAME", %hash);
Run Code Online (Sandbox Code Playgroud)
另一种方法是通过引用传递哈希:
sub coru_excel {
my($pushed_hash_ref, $filename) = @_;
}
coru_excel(\%hash, "FILE_NAME");
Run Code Online (Sandbox Code Playgroud)
您可以将哈希作为参考传递:
sub coru_excel {
my($pushed_hashref, $filename) = @_;
print Dumper(%$pushed_hashref);
}
coru_excel(\%my_hash, $file);
Run Code Online (Sandbox Code Playgroud)
或者,在初始化哈希之前,您可以对最终参数进行特殊处理:
sub coru_excel {
my $filename = pop @_;
my(%pushed_hash) = @_;
print Dumper(%pushed_hash);
}
Run Code Online (Sandbox Code Playgroud)
您必须将散列作为参考传递:
coru_excel(\%hash, "data_".$first."_".$last.".xls");
Run Code Online (Sandbox Code Playgroud)
你这样使用它:
sub coru_excel {
my($pushed_hash_ref, $filename) = @_;
my %pushed_hash = %{$pushed_hash_ref};
print Dumper(%pushed_hash); # better: \%pushed_hash or $pushed_hash_ref
}
Run Code Online (Sandbox Code Playgroud)
有关参考和perlref的教程,请参阅perlreftut以获取更多信息.
Dumper 传递哈希(或数组)引用时,也会产生更好的可用信息.
| 归档时间: |
|
| 查看次数: |
1052 次 |
| 最近记录: |