【问题标题】:Perl reference in array why deference create a new reference value数组中的 Perl 引用为什么要创建一个新的引用值
【发布时间】: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


    【解决方案1】:

    您在pushdata 中得到不同的值,因为您打印的内容不同。 print \$example; 打印对标量 $example 的引用,而不是对其包含的数组的引用。要像在pushdata 中那样打印对数组的引用,您应该使用print $example;

    (打印对标量的引用时,如果标量包含引用,则使用 REF,如果包含 glob,则使用 GLOB,否则使用 SCALAR。)

    虽然您没有问这个问题,但您的代码不起作用的原因是因为您要添加到 @pusharray,而不是 $example$ref 引用的数组。 @pusharray = @$ref; 复制数组的内容。

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dumper;
    
    sub pushdata{
       my ($ref, $value) = @_;
    
       print("\$ref:       $ref\n");
       push(@$ref, $value*2);                    # Adds to @$ref aka @$example
    
       my @local = @$ref;
       print("\\\@local:    ", \@local, "\n");
       push(@local, $value*3);                   # Adds to @local.
    }
    
    my $example = [ 1, 1, 1, 1 ];
    print("\$example:   $example\n");
    print("\\\@\$example: ", \@$example, "\n");
    
    pushdata($example, 10) for 1..4;
    print(Dumper($example));
    

    输出:

    $example:   ARRAY(0x1ea5e0)
    \@$example: ARRAY(0x1ea5e0)
    $ref:       ARRAY(0x1ea5e0)
    \@local:    ARRAY(0x6d0470)
    $ref:       ARRAY(0x1ea5e0)
    \@local:    ARRAY(0x6d0470)
    $ref:       ARRAY(0x1ea5e0)
    \@local:    ARRAY(0x6d0470)
    $ref:       ARRAY(0x1ea5e0)
    \@local:    ARRAY(0x6d0470)
    $VAR1 = [
              1,
              1,
              1,
              1,
              20,
              20,
              20,
              20
            ];
    

    【讨论】:

    • 我试图理解为什么它没有打印我的完整数组。谢谢,我现在明白了.....
    【解决方案2】:

    这一行

    my @pusharray=@$ref;
    

    创建一个副本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2018-10-15
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多