Perl---and--the role

Source: Internet
Author: User
Tags perl interpreter

Usage

There are two usages, all of which are reconciled to the reference.

The first use is to dereference.

The different types of references are solved according to the symbols followed by,

->[] represents the coefficient group reference,->{} means that the Dissolve column reference,-> () represents the solution subroutine reference.

Example:

$arr _ref =/@array;

$arr _ref->[0] Accesses the first element of an array @array.

$hash _ref =/%hash;

$hash _ref->{foo} to access the Foo component of the%hash

$sub _ref =/&test;

$sub _ref-> (1, 2, 3) uses the argument list (&test) to invoke the subroutine.

The second usage is the method that invokes the class or object.

Format:

$obj->method ();

Or

Classname->method ();

For example:

$pop 3->login ($username, $password);

My $ftp = net::ftp->new ("Some.host.name", Debug = 0);

These two usages are slightly different,

In general, however, the following rules are met:

Reference: The left operand (that is, the value on the left, such as $pop 3 and Net::ftp) is $left, and the right operand (that is, the value on the right, such as login and new) is $right, and the arithmetic rule, color=red, is:

if (ref $left valid) {# means \ $left is a reference, not a bare word

$ClassName = ref $left; # take the referenced type as the class name

}

else{

$ClassName = $left; # Just take the bare word as the class name

}

Then call:

&{$ClassName:: $right} ($left, original argument list)

This means that the class name and the right operand are spelled together as the subroutine name (note), and the left operand is treated as the first parameter.

Note: The PERL interpreter has to do more work than this, and it has to take into account the problem of inheritance.

= function

When assigning hash values, it is sometimes not obvious which elements are keys, and those are values. For example, in the following assignment, we need to carefully count the "Key,value,key,value,..." to determine whether 2.5 is a key or a vlaue:

%some_hash = ("foo", "N", "bar", 12.4, 2.5, "Hello", "Wilma", 1.72e30, "Betty", "bye/n");

If Perl can provide a way for us to easily discern which one is key and which is value, how good is that? Larry also thought about the problem, so he invented the big arrow sign (= =). For Perl, the effect is similar to a comma (,), and is sometimes referred to as a "fat comma (fat comma)". In Perl syntax, you can use a large arrow symbol to replace a comma (,), which is the same for Perl. Here's another way to assign a hash value:

Yes, there are also small arrows (-). It is used in conjunction with references, which is a high-level topic. If you are ready, see the Help manual for Perlreftut and Perlref

They are still technically different: any large arrow sign (= =) on the left side of the Bareword (by letters, numbers, underscores, but not by numbers, preceded by an optional plus or minus sign, a sequence of components) is implicitly enclosed in quotation marks. You can therefore omit the quotation marks on the left bareword of the large arrow symbol (= =). You can also ignore the quotation marks in the curly braces of the hash if there are only bareword as keys.

My%last_name = (

"Fred" = "Flintstone",

"Dino" = undef,

"Barney" = "rubble";

"Betty" = "rubble",

);

In the above code, it is easy to tell which one is the key and which is the value. Note the last comma in the list. As we discussed earlier, this comma is useless, but sometimes it gives us convenience; if we want to add a new element to the hash, we just need to know that each row has a key/value pair and a comma at the end. Perl looks at the comma between the different elements and the comma at the end of the list (this comma is not required)

Original link: http://blog.sina.com.cn/s/blog_5f8e03960100x0si.html

Perl---and--the role

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.