Perl Package related

Source: Internet
Author: User

Tags: default env EFI includes define lexical needs app $

Name Conflict issues

If you use require in SUM2.PM to import a code file SUM1.PM:

#!/usr/bin/env perluse strict;use warnings;use 5.010;require '/perlapp/sum1.pm';sub sum {    say "sum2: sum()";}1;

If there is also a sum subroutine in sum1.pm:

#!/usr/bin/env perluse strict;use warnings;use 5.010;sub sum {    say "sum1: sum()";}1;

Then, when running SUM2.PM, the warning subroutine is redefined. Because SUM2.PM first compiles its own sum () and then require import the file during runtime , the sum () is defined again, overwriting the operation:

Subroutine sum redefined at /perlapp/sum1.pm.....

This name conflict problem is resolved by declaring the package.

Defining package and Access package properties

To define a package, simply add the keyword packages.

package PKG_NAME [ VERSION_NUM ];

The above statement is used to declare a package, which can take a package version number, for example package pkg1 0.01; .

For example, in SUM1.PM:

#!/usr/bin/env perluse strict;use warnings;use 5.010;package Sum1;sub sum {    say "pkg:Sum1,sum()";}1;

After the other files are imported into sum1.pm, you can 包名::属性 access the properties in the sum1.pm, such as subroutines, in the same way.

#!/usr/bin/env perluse strict;use warnings;use 5.010;require '/perlapp/sum1.pm';sub sum {    say "file: sum2,sum()";}sum();         # 访问本文件定义的sum子程序Sum1::sum();   # 访问包Sum1::sum子程序,括号不能少1;

In addition to subroutines, the properties of other non-lexical scopes in the package can also be accessed, including: scalar, array, hash, file handle. For example:

$Sum1::name;@Sum1::arr;

Each file is defined in at least one package, and if there is no explicit package instruction given, the package defaults to the package main . Therefore, you can use the method to access the properties of this program file main:: + 属性 . For example, in SUM2.PM:

#!/usr/bin/env perluse strict;use warnings;use 5.010;require '/perlapp/sum1.pm';sub sum {    say "file: sum2,sum()";}sum();         # 访问本文件定义的sum子程序main::sum();   # 等价于上一行直接访问sum()Sum1::sum();   # 访问包Sum1::sum子程序,括号不能少1;
Multiple packages in one file

In general, a file defines only one package. But allows a file definition to pass through the package.

As follows:

package Pkg1;...code here belong to Pkg1...package Pkg2;...code here belong to Pkg2...

When defining multiple packages, all the attributes from Package 1 to package 2 belong to package 1.

For example, in SUM1.PM:

#!/usr/bin/env perluse strict;use warnings;use 5.010;sub sum {      # 位于默认的main包    say "pkg:main,sum()";}package Sum1;  # 第一个包sub sum {      # 位于Sum1包    say "pkg:Sum1,sum()";}sum();        # 访问的是Sum1包的sum()main::sum();  # 访问的是main包的sum()package Sum2;   # 第二个包sub sum {       # 位于Sum2包    say "pkg:Sum2,sum()";}sum();        # 访问的是Sum2包的sum()Sum1::sum();  # 访问的是Sum1包的sum()main::sum();  # 访问的是main包的sum()1;

Some word names always belong to the main package: ARGV, Argvout, ENV, INC, SIG, STDERR, STDIN, and STDOUT. Some names with special punctuation, such as $_,$2, $! , are either all in main or all belong to a package, meaning that these variable names have "atomicity".

In addition, lexical variables cannot be accessed using the package name, because the properties accessed using the package are global properties. Therefore, to access the global properties in the code block, you can add the package name:

package Sum1;out $var="1234";sub mysub {    my $var;    ...$var...;  # 访问的是my $var    $Sum1::var;  # 访问的是Sum1包中的$var}

If you put a package declaration into a code block, the domain of the code block disappears:

package Sum1;{    package main;    sub sum {        say "in main"    }    sum();  # 调用main中的sum}  # 退出代码块,重新回到Sum1包sub sum {code}   # 属于Sum1包的sum子程序
Package code block

Starting with Perl 5.12, the package code block is supported:

use v5.12;package pkg1 {    ...}package pkg2 {    ...}

The package code block is equivalent to the lexical scope:

package Navigation {    my @homeport = (21.283, ?157.842); # 属于包    sub get_me_home {        my @homeport;     # 声明词法变量        ... @homeport ... # 访问的是词法变量        ... @Navigation::homeport ... # 访问的是包变量    }    ... @homeport ... # 访问的是包变量}

It is equivalent to:
{
Package Navigation;
My @homeport = (21.283,? 157.842); # belongs to the package
Sub Get_me_home {
My @homeport; # declaring lexical variables
... @homeport ... # access to lexical variables
... @Navigation:: homeport ... # access to package variables
}
... @homeport ... # refers to the package variable
}

Perl Package related

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.

Tags Index: