Perl6-Bible/lib/Perl6/Bible/S11.pod
=encoding utf8
=head1 NAME
Synopsis_11 - Modules
=head1 AUTHOR
Larry Wall <larry@wall.org>
=head1 VERSION
Maintainer: Larry Wall <larry@wall.org>
Date: 27 Oct 2004
Last Modified: 23 Feb 2006
Number: 11
Version: 9
=head1 Overview
This synopsis discusses those portions of Apocalypse 12 that ought to have
been in Apocalypse 11.
=head1 Modules
As in Perl 5, a module is just a kind of package. Unlike in
Perl 5, modules and classes are declared with separate keywords,
but they're still just packages with extra behaviors.
A module is declared with the C<module> keyword. There are
two basic declaration syntaxes:
module Foo; # rest of scope is in module Foo
...
module Bar {...} # block is in module Bar
The first form is allowed only as the first statement in the file.
A named module declaration can occur as part of an expression, just like
named subroutine declarations.
Since there are no barewords in Perl 6, module names must be predeclared,
or use the sigil-like C<::ModuleName> syntax. The C<::> prefix does not
imply top-levelness as it does in Perl 5. (Use C<::*> or C<GLOBAL::> for that.)
A bare C<module> declarator declares an C<our> module name in the current
package. At the start of the file, the current package is C<*>, so the
first declaration in the file is automatically global.
You can use C<our module> to explicitly
declare a module in the current package (or module, or class).
To declare a lexically scoped module, use C<my module>.
Module names are always searched for from innermost scopes to outermost.
As with an initial C<::>, the presence of a C<::> within the name
does not imply globalness (unlike in Perl 5).
The C<::*> namespace is not "main". The default namespace for the
main program is C<::*Main>, which it switches to from * as soon as
it sees the first declaration, if that declaration doesn't set the
package name. (Putting C<module Main;> at the top of your program
is redundant, except insofar as it tells Perl that the code is Perl
6 code and not Perl 5 code. But it's better to say "use v6" for that.)
But note that if you say
use v6;
module Foo {...}
you've just created Main::Foo, not *Foo.
Module traits are set using C<is>:
module Foo is bar {...}
=head1 Exportation
Exportation is now done by trait declaration on the exportable item:
module Foo; # Tagset...
sub foo is export(:DEFAULT) {...} # :DEFAULT, :ALL
sub bar is export(:DEFAULT :others) {...} # :DEFAULT, :ALL, :others
sub baz is export(:MANDATORY) {...} # (always exported)
sub bop is export {...} # :ALL
sub qux is export(:others) {...} # :ALL, :others
Declarations marked as C<is export> are bound into the C<EXPORT> inner
modules, with their tagsets as inner module names within it. For example,
the C<sub bar> above will bind as C<&Foo::EXPORT::DEFAULT::bar>,
C<&Foo::EXPORT::ALL::bar>, and C<&Foo::EXPORT::others::bar>.
Inner modules automatically add their export list to modules in all their
outer scopes:
module Foo {
sub foo is export {...}
module Bar {
sub bar is export {...}
module Baz {
sub baz is export {...}
}
}
}
The C<Foo> module will export C<&foo>, C<&bar> and C<&baz> by default;
calling C<Foo::Bar.import> will import C<&bar> and C<&baz> at runtime.
=head1 Compile-time Importation
Importing via C<use> binds into the current lexical scope by default
(rather than the current package, as in Perl 5).
use Sense <common @horse>;
You can be explicit about the desired namespace:
use Sense :MY<common> :OUR<@horse> :GLOBAL<$warming>;
That's pretty much equivalent to:
use Sense;
my &common ::= &Sense::common;
our @horse ::= @Sense::horse;
$*warming ::= $Sense::warming;
It is also possible to re-export the imported symbols:
use Sense :EXPORT; # import and re-export the defaults
use Sense <common> :EXPORT; # import "common" and re-export it
use Sense <common> :EXPORT<@horse>; # import "common" but export "@horse"
In the absence of a specific scoping specified by the caller, the module
may also specify a different scoping default by use of C<:MY> or C<:OUR>
tags as arguments to C<is export>. (Of course, mixing incompatible scoping
in different scopes is likely to lead to confusion.)
=head1 Runtime Importation
Importing via C<require> also binds into the current lexical scope by
default, but performs the binding at runtime:
require Sense <common @horse>;
require "/home/non/Sense.pm" <common @horse>;
Tagsets are not recognized in the default import list to C<:MY>, but you can
explicitly request to put them into the C<:OUR> scope:
require Sense <:ALL> # does not work
require Sense :MY<ALL> # this doesn't work either
require Sense :OUR<ALL> # but this works
If the import list is omitted, then nothing is imported. Calling C<.import>
at runtime cannot import into the lexical scope:
require Sense;
Sense.import; # goes to the OUR scope by default, not MY
=head1 Importing from a pseudo-package
You may also import symbols from the various pseudo-packages listed in S02.
They behave as if all their symbols are in the C<:ALL> export list:
use GLOBAL <$IN $OUT $ERR>;
require CALLER <$x $y>;
# Same as:
# my ($IN, $OUT, $ERR) ::= ($*IN, $*OUT, $*ERR)
# my ($x, $y) := ($CALLER::x, $CALLER::y)
As pseudo-packages are always already preloaded, C<use> and C<require> will
never attempt to load, for example, C<GLOBAL.pm> from an external source.
=head1 Versioning
When at the top of a file you say something like
module Cat;
or
class Dog;
you're really only giving one part of the name of the module.
The full name of the module or class includes other metadata,
in particular, the version, and the author.
Modules posted to CPAN or entered into any standard Perl 6 library
are required to declare their full name so that installations can know
where to keep them, such that multiple versions by different authors
can coexist, all of them available to any installed version of Perl.
The syntax of a versioned module or class declaration has three parts
separated by hyphens. The three parts are the short name of the
class/module, its version number, and a URI identifying the author
(or authorizing authority). For example:
class Dog-1.2.1-cpan:JRANDOM;
class Dog-1.2.1-http://www.some.com/~jrandom;
class Dog-1.2.1-mailto:jrandom@some.com;
Such a declaration automatically aliases the full name
of the class (or module) to the short name. So for the rest of the
lexical scope, C<Dog> refers to the longer name.
If there are extra classes or modules or packages declared within
the same file, they implicitly have a long name including the file's
version and author, but you needn't declare them again.
Since these long names are the actual names of the classes, when you say:
use Dog;
you're really wildcarding the unspecified bits:
use Dog-(Any)-(Any);
And when you say:
use Dog-1.2.1;
you're really asking for:
use Dog-1.2.1-(Any);
Saying C<1.2.1> specifies an I<exact> match on the version number,
not a minimum match. To match more than one version, put a range
operator in parens:
use Dog-(1.2.1..1.2.3);
use Dog-(1.2.1..^1.3);
use Dog-(1.2.1...);
Subversions are wildcarded, so C<1.2> really means C<1.2.0...>. If you
say:
use v6;
which is short for:
use Perl-6;
you're asking for any version of Perl 6. Say:
use Perl-6.0;
use Perl-6.0.0;
use Perl-6.2.7.1;
if you want to lock in a particular set of semantics at some greater
degree of specificity. And some large company ever forks Perl, you can say
use Perl-6-cpan:TPF
to guarantee that you get the unembraced Perl. C<:-)>
For wildcards any valid smartmatch selector works:
use Dog-(1.2.1 | 1.3.4)-(/:i jrandom/);
use Dog-(Any)-(/^cpan\:/)
Parens are optional on a closure smartmatcher. The preceding may
also be written:
use Dog-{$^ver ~~ 1.2.1 | 1.3.4}-{$^auth ~~ /:i jrandom/};
use Dog-{$^ver ~~ Any}-{$^auth ~~ /^cpan\:/}
In any event, however you select the module, its full name is
automatically aliased to the short name for the rest of your lexical
scope. So you can just say
my Dog $spot .= new("woof");
and it knows (even if you don't) that you mean
my Dog-1.3.4-cpan:JRANDOM $spot .= new("woof");
The use statement actually allows a language on the front of a module name,
so that you can use modules from other languages. The language is separated
by a colon. For instance:
use perl5:Acme::Bleach-1.12-DCONWAY;
use ruby:Rails <PR_MACHINE>;
=head1 Forcing Perl 6
To get Perl 6 parsing rather than the default Perl 5 parsing,
we said you could force Perl 6 mode in your main program with:
use Perl-6;
Actually, you can just start your main program with any of:
use v6;
module;
class;
Those all specify the latest Perl 6 semantics, and are equivalent to
use Perl-(v6.0...)-(Any);
To lock the semantics to 6.0.0, say:
use v6.0.0;
In any of those cases, strictures and warnings are the default
in your main program. But if you start your program with a bare
version number or other literal:
v6.0.0;
v6;
6;
"Coolness, dude!";
it runs Perl 6 in "lax" mode, without strictures or warnings, since obviously
a bare literal in a void context I<ought> to have produced a warning.
(Invoking perl with C<-e6> has the same effect.)