[Perl series-] 2. Reference Usage in Perl

Source: Internet
Author: User
Tags scalar
Why should I use references in Perl?

For developers familiar with C language, the pointer concept is certainly not unfamiliar.

Perl references pointer, which can point to variables, arrays, hash tables, and even subprograms.

The two Perl reference types in perl5 are hard Perl references and symbolic Perl references. The symbol Perl references the name of a variable. It is useful for creating and locating the variable name at run time. Basically, the symbol Perl reference is like a file name or a soft link in a UNIX system. A hard Perl reference is like a hard link in a file system.

Perl4 only allows the symbol Perl reference, which makes it difficult to use. For example, you can only create an index on the symbol name hash table (named _ main {}) of the package by name. Perl5 allows hard Perl reference of data, which is more convenient.
Hard Perl references trace the Count of Perl references. When the number is zero, Perl Automatically releases the projects referenced by Perl. If this project is an object, the analysis structure is released to the memory pool. Perl itself is an object-oriented language, because everything in Perl is an object, and packages and modules make objects easier to use.


Under what conditions will the reference be used?

In perl4, the value field in the hash table can only be scalar, not list, which is inconvenient in some cases, such as the following data:

Chicago, USA
Frankfurt, Germany
Berlin, Germany
Washington, USA
Helsinki, Finland
New York, USA

We want to classify cities by country. Each country then faces the list of cities. If perl4 is used, we must combine the list of cities into strings. If perl5 is used, we can use references for the list, with reference, you can construct a complex hash structure and use the list as the hash value.


Define reference

Knowing the role of reference, how can we define a reference?

Method 1: Use the slash \

Add \

References to array variables:

my @array = (1,2,3);my $arrayref = \@array;

References to hash Variables

my %hash = ("name"=>"oscar999","age"=>30);my $hashref = \%hash;

Reference of scalar

my $scarlar = 1;my $scarlarfef = \$scarlar;

Method 2. Anonymously reference [] or {}

Anonymous references do not define variables,

For anonymous arrays, brackets [] are directly used to define references.

my $arrayref = [1,2,3];
The preceding example defines an anonymous array.

In this way, you can also set an anonymous array in an anonymous array.

my $arrayref = [        [1, 2, 3],        [4, 5, 6],        [7, 8, 9]];

Anonymous hash reference defined {}

my $href = {US=>4,CN=>6};


Use reference

Define references. How can I use them?

Method 1: add one more $

This concept is similar to that of C.

# Define the variable my $ scalar = 1; my @ array = (1, 2, 3); my % hash = ("name" => "oscar999 ", "Age" => 30); # Define reference my $ sref =\$ scalar; # scalar referencemy $ Aref =\@ array; # array referencemy $ href =\% hash; # hash reference # reference print $ sref, "\ n"; print @ $ Aref, "\ n"; print % $ href, "\ n "; print $ Aref [2], "\ n"; print $ href {'name'}, "\ n ";

Method 2: add one more $

Use of Arrays:

   @a        @{$aref}         An array   [email protected]      reverse @{$aref}    Reverse the array   $a[3]      ${$aref}[3]       An element of the array   $a[3] =17;      ${$aref}[3] =17    Assigning an element

Hash usage:

   %h          %{$href}           A hash   keys%h        keys%{$href}        Get the keys from the hash   $h{‘red‘}       ${$href}{‘red‘}       An element of the hash   $h{‘red‘} =17      ${$href}{‘red‘} =17    Assigning an element

Note: When {} is in the form of $ var, {} can be omitted, that is, @ {$ Aref} is equivalent to @ $ Aref, but it is best to add {}.


Method 3: Use the arrow symbol.

$ Aref-> [] unreferencing Arrays

$ Href-> {} hash decoding reference

$ Href-> () subprocess unreference

Let's look at the example:

$aref->[0] =3 ; $href->{name} ="autumn" ; 




Preface
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.