【发布时间】:2019-10-26 12:47:38
【问题描述】:
我正在学习 perl 脚本。我使用下面的示例代码来理解 perl 中的引用
#!/usr/bin/perl
use Data::Dumper;
my $example;
sub pushdata{
my ($ref,$value)=@_;
print 'Reference of array received in pushdata sub @{$example} ', $ref,"\n";
my @pusharray=@$ref;
print 'Reference of array passed to the push sub @pusharray',\@pusharray,"\n";
push(@pusharray,$value);
}
print 'Before creating array Reference of $example ', \$example,"\n";
$example->[0]=1;
$example->[1]=1;
$example->[2]=1;
$example->[3]=1;
print 'After creating array Reference of $example ', \$example,"\n";
pushdata(\@{$example},10);
pushdata(\@{$example},10);
pushdata(\@{$example},10);
pushdata(\@{$example},10);
pushdata(\@{$example},10);
print Dumper($example),"\n";
这段代码的输出是
1.Before creating array Reference of $example SCALAR(0x561e878b47f8)
2.After creating array Reference of $example REF(0x561e878b47f8)
3.Reference of array received in pushdata sub @{$example} ARRAY(0x561e87888470)
4.Reference of array passed to the push sub @pusharrayARRAY(0x561e878b4960)
5.Reference of array received in pushdata sub @{$example} ARRAY(0x561e87888470)
6.Reference of array passed to the push sub @pusharrayARRAY(0x561e878b4960)
7.Reference of array received in pushdata sub @{$example} ARRAY(0x561e87888470)
8.Reference of array passed to the push sub @pusharrayARRAY(0x561e878b4960)
9.Reference of array received in pushdata sub @{$example} ARRAY(0x561e87888470)
10.Reference of array passed to the push sub @pusharrayARRAY(0x561e878b4960)
11.Reference of array received in pushdata sub @{$example} ARRAY(0x561e87888470)
12.Reference of array passed to the push sub @pusharrayARRAY(0x561e878b4960)
13.$VAR1 = [
1,
1,
1,
1
];
Q1.这里 outputline1 标量值为 SCALAR0x561e878b47f8。将数组分配给标量值后,它从 SCALAR 变为 Ref 类型 outputline2。是不是因为我对这个标量值进行了操作并分配了数组?
第二季度。如果我们看到 outputline3 我尝试打印此处收到的 ref 值。它与 \$example 引用不同,为什么?
【问题讨论】:
标签: perl