简体   繁体   中英

Perl reference in array why deference create a new reference value

I am learning perl script. I was using below example code to understand referencing in 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";

And the output of this code is

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.Here outputline1 the scalar value is SCALAR0x561e878b47f8. After assigning array to scalar value it change from SCALAR to Ref type outputline2 . Is it due i did an operation on this scalar value and assigned array?

Q2. If we see outputline3 i tried to print the ref value which is received here.It is not same as the \$example reference why?

You get a different value in pushdata because you print something different. print \$example; prints a reference to the scalar $example , not the reference to the array it contains. To print the reference to the array like you did inside of pushdata , you should have used print $example; .

(When printing a reference to a scalar, it uses REF if the scalar contains a reference, GLOB if it contains a glob, and SCALAR otherwise.)

While you didn't ask this, the reason your code doesn't work is because you are adding to @pusharray , not the array referenced by $example and $ref . @pusharray = @$ref; copies the contents of the array.

#!/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));

Output:

$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
        ];

This line

my @pusharray=@$ref;

creates a copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM