PERL package

xiaoxiao2021-03-06  110

Package and module

In object-oriented programming, each object belongs to a class. Category can be used repeatedly.

The module in Perl is equivalent to the class module is the basic unit that reuses in Perl.

Modules should be an abstract logical concept.

The module (class) is implemented with the package with this class, that is, the definition of the module.

Such as package human; # statement # ......

In order to make people reuse the definition of these modules, the file will be the same name and use the .pm extension, the PM is an abbreviation for "perl module".

2. Namespace, identifier

The package is divided into a name space. For example, when you write a code, you divide your name space for your code. So in your space, the variables of others outside, the function is not impact you, you can't afford others.

The namespace, the name is identifier, such as: package human $ mike; @john; my% stella;

Mike, John, Stella is the so-called name, that is, the identifier Mike, John is the name space of Human.

As for Stella, what is required here: My declaration variable is independent of the package: they always belong to the closure domain, whether there is any package statement. Even $ _ is also a package, but it is not a current package, but belongs to the MAIN package.

The scope of Package declaration begins with the declaration itself until the end of the closed domain, or until the other class package declaration. The role of the package is just the symbol table that will be used for the other part of the block.

3. Using the identifier in other packages

The identifier is difficult to understand, just like a lot of people don't understand what is typeglob. My understanding is SYM is an identifier, * SYM is Typeglob.

According to the above example Mike is the identifier of the human package, then the name and double colon can be limited to the human :: mike this to quote the Mike name in the human package. We know, in the human package $ Mike is a scalar, then we use $ Human :: Mike can get the scalar value of $ MIKE in the human package

Please note $ human :: mu in human, mike called identifier, and human :: mu should call the variable name

4. Symbol table

The content of the package is generally called the symbol table together. I understand that the identifier / corresponding TypeGlob pair is stored in a hash, this hash with the name of the package, but has added two colons. The name of the symbolic table of the main package is% main :: In the hash of the symbol table, each key / value pair matches a variable name and its value. The key is the symbol identifier, the value is the corresponding Typeglob

The following two are (almost) the same effect * SYM = * main :: variable; * SYM = $ main :: {"variable"};

The first kind I is based on 3 reference identifiers to understand the second kind of understanding of the symbolic table

转载请注明原文地址:https://www.9cbs.com/read-103545.html

New Post(0)