Summary of Perl references

Source: Internet
Author: User

Perl references are similar to pointers in C language,

1 Reference of scalar variables:

$ Name = "Zhang ";

$ Ref =/$ name; # "/" adds this symbol to indicate a reference to the scalar $ name, that is, $ ref points to $ name.

Print $ ref; # The printed value is the address.

Print $ ref; # print the reference value. The $ sign added for Zhang. indicates that the referenced object is a scalar rather than an array.

 

$ Another = $ ref; # $ another is another reference of $ name.

$ Ref = "hello"; # $ FEF is a common scalar and is no longer referenced by $ name.

 

2 references to Arrays:

The reference method for arrays created in Perl is similar to that for scalar variables. Use a backslash to create an array:

$ Aref = // @ arr; # $ Aref contains a reference to the entire array @ arr.

You can use this reference $ Aref to access each part of @ arr:

$ Aref [0] # access the first element of array @ arr, which is the same as $ {$ Aref} [0 ].

@ $ Aref [2, 3] # access a piece of the array, which is the same as @ {$ Aref} [2, 3 ].

@ $ Aref # access the entire array, which is the same as @ {$ Aref }.

 

# Example of using references to access Arrays

Foreach (@ {$ Aref })

{

Print "$ _/N ";

}

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.