References
 
Overview of References
  • Each object in Perl has a location and type.
     
  • A reference contains the type and the address.
     
  • Misusing a reference creates a run-time error.
     
  • Perl does reference counting to keep track of objects that can be reused.
Using References
  • To create a reference, precede a data object with a backslash.
     
  • To dereference a reference, put the appropriate sign in front of the reference.
    my $title = "Introduction to Perl";
    my $scalar_ref  = \$title;
    print "Title is $$scalar_ref\n";
    # Title is Introduction to Perl
    
    my @stooges = qw( Moe Larry Curly );
    my $list_ref    = \@stooges;
    print join( ", ", @$list_ref ), "\n";
    # Moe, Larry, Curly
    
    my %employee = ( name => "Andy Lester", phone => 7660 );
    my $hash_ref    = \%employee;
    while ( my ($key,$value) = each %$hash_ref ) {
        print "In hash_ref, $key is $value\n";
        }
    # In hash_ref, name is Andy Lester
    # In hash_ref, phone is 7660
    
    sub howdy { my $person = shift; print "Hello, $person!\n"; }
    my $code_ref    = \&howdy;
    &$code_ref( "Perl Class" );
    # Hello, Perl Class!
Using Data
  • It's not necessary to name an object to take a reference to it.
     
  • Putting [] around a list makes it an anonymous list.
     
  • Putting {} around a list makes it anonymous hash.
    my $stooge_ref = [ "Larry", "Moe", "Curly" ];
    my $person_ref = { name => "Andy Lester", phone => 7660 };
    
Index
Introduction
What Is Perl?
Perl Resources
Running a Perl Program
Perl Thinking
Data Types
Scalars
Strings: Single Quoted
Strings: Double Quoted
Scalar operations
Scalar comparisons
Variables
Lists
Using Lists
Control Structures
Hashes
Hash Manipulation
File Handling
Regex Matching
Regex Matching: Ex. 1
Regex Matching: Ex. 2
Regex Replacing
Subroutines
Anonymous subs
References
Structures
Modules
Modules: File::Find
Modules: Internet
Modules: Win32::*
Everything Else
  Next page >>>