Perl Package related

Source: Internet
Author: User

Name Conflict issues

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

#!/usr/bin/env perl
use 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 perl
use 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 examplepackage pkg1 0.01;.

For example, in SUM1.PM:

#!/usr/bin/env perl
use 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 Package Name::Attribute access the properties in the sum1.pm, such as subroutines, in the same way.

#!/usr/bin/env perl
Use strict;
Use warnings;
Use 5.010;

Require '/perlapp/sum1.pm';

Sub sum {
     Say "file: sum2,sum()";
}

Sum(); # access the sum subroutine defined in this file
Sum1::sum(); # Access package Sum1::sum subroutine, no less parentheses

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 explicitpackageinstruction given, the package defaults to the packagemain. Therefore, you can use the method to access the properties of this program filemain:: + Attributes. For example, in SUM2.PM:

#!/usr/bin/env perl
Use strict;
Use warnings;
Use 5.010;

Require '/perlapp/sum1.pm';

Sub sum {
     Say "file: sum2,sum()";
}

Sum(); # access the sum subroutine defined in this file
Main::sum(); # Equivalent to the previous line directly access sum()
Sum1::sum(); # Access package Sum1::sum subroutine, no less parentheses

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 perl
Use strict;
Use warnings;
Use 5.010;

Sub sum { # is located in the default main package
     Say "pkg:main,sum()";
}

Package Sum1; # first package

Sub sum { # located in the Sum1 package
     Say "pkg:Sum1,sum()";
}

Sum(); # access to the sum() of the Sum1 package
Main::sum(); # access to the main package's sum()

Package Sum2; # 2 package

Sub sum { # located in the Sum2 package
     Say "pkg:Sum2,sum()";
}

Sum(); # access to the sum() of the Sum2 package
Sum1::sum(); # is the sum() of the Sum1 package.
Main::sum(); # access to the main package's 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...; # is accessing my $var
     $Sum1::var; # is accessing $var in the Sum1 package
}

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(); # call sum in main
} # Exit the code block and return to the Sum1 package

Sub sum {code} # belongs to the sum subroutine of the Sum1 package
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:

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

package pkg2 {
    ...
}

It is equivalent to:


Package Navigation {
     My @homeport = (21.283, ?157.842); # belongs to the package
     Sub get_me_home {
         My @homeport; # declare lexical variables
         ... @homeport ... # access to lexical variables
         ... @Navigation::homeport ... # access to the package variable
     }
     ... @homeport ... # access to the package variable
}


{
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.

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.