About Perl closures (personal understanding)

Source: Internet
Author: User

My personal understanding is that when a variable goes out of scope, it should be gone and gone, but you can still access it, which is the closure.


# Look at the example below.

# !/usr/bin/env perl-w  Use Strict; {        my$value'abc';} Print $value;

If this is the case:

Because $value does not exist after {} is exceeded, print becomes an undefined variable and an error occurs.

Let's look at one more example:

1 #!/usr/bin/env perl-w2  UseStrict;3 Subfunction_1{4         my $value='ABC';5         my $in _function_1=Sub{Print "$value \ n";};6         return $in _function_17 }8  9 my $value=function_1;Ten $value();

The results are as follows:

#
As can be seen, this $value should not exist, but also can access him, this situation is called "closure".


Perhaps you would like to say that this is not the return returned to him, because the first example does not return $value, so can not print?!

Personal view:
For the first example, if you return directly to $value, his value is returned, but the value is the same, which is not the same variable.
For the second example, the $value he visited was itself from beginning to end. (but internally, a copy of the variable may be created automatically)


Here's a quote from the word "Perl language Programming":
Represents a specific time when an anonymous function is defined in a particular lexical scope, it pretends to run in that scope,
Although it may later be called outside of this scope (but it is not pretending that it does run in that scope), it is called a "closure."


Personal advice according to the general function to understand the line, do not "too" care about the word "closure" the two words.

About Perl closures (personal understanding)

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.