Can I name an anonymous array in Perl?

后端 未结 4 557
野趣味
野趣味 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条回答
  •  旧时难觅i
    2021-01-13 06:00

    1. our @array; local *array = $aref;
      

      Pros: Built-in feature since 5.6.
      Cons: Ugly. Uses a global variable, so the variable is seen by called subs.

    2. use Data::Alias qw( alias );
      alias my @array = @$aref;
      

      Pros: Clean.
      Cons: This module gets broken by just about every Perl release (though it gets fixed quickly if not before the actual release).

    3. use feature qw( refaliasing );
      no warnings qw( experimental::refaliasing );
      \my @array = $aref;
      

      Pros: Built-in feature.
      Cons: Requires Perl 5.22+, and even then, the feature is experimental.

提交回复
热议问题