Reference Basic in Perl

來源:互聯網
上載者: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 creates another reference 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 to
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 a

    variable or subroutine name, you can replace the identifier with a
    BLOCK returning a reference of the correct type. In other words, the
    previous examples could 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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.