rfcv 发表于 2015-12-29 08:08:41

Perl中的引用和解引用

  对$scalar的引用:
  my $variable;
my $reference=\$variable;
  对$scalar的解引用:
  $$reference;
  
对@array的引用:
  my @array;
my $reference=\@array;
  对@array的解引用:
  $$reference;
$reference->;
@$reference; #to access the whole array

  
对%hash的引用
  my %hash;
my $reference=\%hash;
  对%hash的解引用:
  $$reference{'key'};
$reference->{'key'};
%$reference; #to access the whole hash

  
对函数的解引用:
  &$function(arguments);
$function->(arguments);
  对函数的引用:
  sub function{}
my $function=\&function;
  
匿名数组:
  my $array;
@$array=("a","b");
$$array="c";
$array->="d";
print @$array;
  
匿名函数:
  my $reference=sub {};
&$reference(parameters);
  or
  sub function{}
  ${\function(parameters)};

  
ref函数返回相应的引用类型:
  ref(\@array)=ARRAY;
ref(\%hash)=HASH;
ref(\&function)=CODE;
ref(\\@array)=REF;
ref(\*hash)=GLOB;
  
数组的数组:

  $array[$i]->[$j];
$arrat[$i][$j]
  
引用不能作为hash中的键字。


  ${a}=$a;
${"a"}=$a; #是一个符号引用
  如果不使用符号引用: use strict 'refs';使用:"no strict 'refs'";
  $name="bam";
  $$name=1; #$bam=1
  $name->=4; # @bam,$bam=4
  $name->{X}="Y";
  @$name=(); # clear @bam
  &$name; #call &bam

页: [1]
查看完整版本: Perl中的引用和解引用