Perl Reference

From GM-RKB
Jump to navigation Jump to search

A Perl Reference is a Perl Variable that references the memory location of a Perl Data Structure.



References


Examples

Scalar References

  my $scalar = "a" ;
  my $scararRef = \$scalar ;
  print "TRUE\n" if $scalar == $$scalarRef ;

Array References

 my @Array1 ;
 my @Array2 = qw/a b c/ ;

 my $array1Ref = \@Array1 ;
 @{$array1Ref}=@Array2 ;

 print "1:@Array1\n" ;

= Hash References

  %hash = (
    'id'    => 18297,
    'name'  => "John Doe" );
  $hash_ref = \%hash;
  $name = $ { $hash_ref} { name };
  my @keys = keys % { $hash_ref };

Array of Hash References

 my @Array ;
 my $arrayRef = \@Array ;
 my $hash = {} ;
 $hash->{A} = "B" ;
 my $i = push @{$arrayRef}, $hash ;
 print "i=$i A=" . $Array[$i-1]->{A} . "\n" ;