Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Perl module
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Perl packages and namespaces== A running Perl program has a built-in [[namespace]] called "<code>main</code>", which is the default name. For example, a subroutine called <code>Sub1</code> can be called as <code>Sub1()</code> or <code>main::Sub1()</code>. With a variable the appropriate [[Sigil (computer programming)|sigil]] is placed in front of the namespace; so a scalar variable called <code>$var1</code> can also be referred to as <code>$main::var1</code>, or even <code>$::var1</code>. Other namespaces can be created at any time. <syntaxhighlight lang="perl"> package Namespace1; $var1 = 1; # created in namespace Namespace1, which is also created if not pre-existing our $var2 = 2; # also created in that namespace; our required if use strict is applied my $var3 = 3; # lexically-scoped my-declared - NOT in any namespace, not even main </syntaxhighlight> <syntaxhighlight lang="perl"> $Namespace2::var1 = 10; # created in namespace Namespace2, also created if not pre-existing our $Namespace2::var2 = 20; # also created in that namespace my $Namespace2::var3 = 30;#compilation error:my-declared variables CAN'T belong to a package </syntaxhighlight> Package declarations apply package scope till the next package declaration or the end of the block in which the declaration is made. <syntaxhighlight lang="perl"> our $mainVar = 'a'; package Sp1; our $sp1aVar = 'aa'; print "$main::mainVar\t$sp1aVar\n"; # note mainVar needs qualifying package Sp2; our $sp2aVar = 'aaa'; print "$main::mainVar\t$Sp1::sp1aVar\t$sp2aVar\n";# note mainVar and sp1aVar need qualifying package main; print "$mainVar\t$Sp1::sp1aVar\t$Sp2::sp2aVar\n"; # note sp1aVar and sp2aVar need qualifying $mainVar = 'b'; { # NOTE previously created packages and package variables still accessible package Sp1; our $sp1bVar = 'bb'; print "$main::mainVar\t$sp1aVar\t$sp1bVar\n"; # note mainVar needs qualifying { package Sp2; our $sp2bVar = 'bbb'; print "$main::mainVar\t$Sp1::sp1aVar$Sp1::sp1bVar\t$sp2aVar$sp2bVar\n"; } # note mainVar and sp1...Var need qualifying print "$main::mainVar\t$sp1bVar$sp1aVar\t$Sp2::sp2bVar$Sp2::sp2aVar\n"; } # note package Sp1 applies by default # main applies again by default; all package variables still accessible as long as qualified print "$mainVar\t$Sp1::sp1aVar$Sp2::sp2bVar\n"; </syntaxhighlight> ===Packages and modules=== Conventionally, namespaces are associated with modules; in practice, there is usually one namespace per module and vice versa, but that's not mandated by the language. For example, the 'standard' module CGI.pm has the following declaration at its top: <syntaxhighlight lang="perl"> package CGI; </syntaxhighlight> This module, and its functionality, would commonly be invoked as follows: <syntaxhighlight lang="perl"> use CGI (':standard'); # imports many functions, including b() ... print b('Hello, world'); # outputs <b>Hello, world</b> </syntaxhighlight> A 'missing' subroutine ''could'' be added from the using program's namespace. <syntaxhighlight lang="perl"> sub CGI::bi { # define target namespace (CGI) and sub name (bi) return b(i($_[0])); } </syntaxhighlight> and invoked as below: <syntaxhighlight lang="perl"> print CGI::bi('Hello, world'); # outputs <b><i>Hello, world</i></b> </syntaxhighlight> However, though technically feasible, that would be dubious programming practice. You might just as well define the sub in the calling namespace, and call it from that namespace.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)