Reference basic in Perl

Source: Internet
Author: User
Making references

References can be created in several ways.

By using the backslash operator on a variable, subroutine, or value. (This works much like the & (address-of) Operator in C.) This typically createsAnotherReference to a variable, because there's already a reference to the variable in the symbol table. but the symbol table reference might go away, and you'll still have the reference that the backslash returned. here are some examples:

  1. $scalarref = \$foo;
  2. $arrayref = \@ARGV;
  3. $hashref = \%ENV;
  4. $coderef = \&handler;
  5. $globref = \*foo;
Using references

That's it for creating references. By now you're probably dying
Know how to use references to get back to your long-lost data. There
Are several basic methods.

  1. Anywhere you 'd put an identifier (or chain of identifiers) as part of

    Variable or subroutine name, you can replace the identifier with
    Block returning a reference of the correct type. In other words,
    Previous examples cocould be written like this:

    1. $bar = ${$scalarref};
    2. push(@{$arrayref}, $filename);
    3. ${$arrayref}[0] = "January";
    4. ${$hashref}{"KEY"} = "VALUE";
    5. &{$coderef}(1,2,3);
    6. $globref->print("output\n"); # iff IO::Handle is loaded
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: 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.