Can I name an anonymous array in Perl?

后端 未结 4 530
野趣味
野趣味 2021-01-13 05:06
#!/usr/bin/perl -w

use strict;

my $aref = [1, 2, 3];
my @a = @$aref;              # this line
$a[1] = 99;
print \"aref = @$aref\\n\";
print \"a = @a\\n\";
<         


        
4条回答
  •  借酒劲吻你
    2021-01-13 06:04

    One option is to use the Data::Alias package from CPAN.

    This way you can write:

    #!/usr/bin/perl
    
    use Data::Alias qw( alias );
    
    my $aref  = [1, 2, 3];
    
    alias my @a = @$aref;
    
    $a[1] = 99;
    print "aref = @$aref\n";
    print "a = @a\n";
    

    A related question on SO can be found here: Assign address of one array to another in Perl possible?

提交回复
热议问题