Skip to content

Commit 1444d13

Browse files
committed
Indexes import where it's effectively explained
Closes #2304 Also some reflow, linking and reformatting.
1 parent 766353e commit 1444d13

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

doc/Language/module-packages.pod6

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
I<N.B.> "Module" is an overloaded term in Perl 6; this document
99
focuses on use of the C<module> declarator.
1010
11-
1211
=head1 What are modules?
1312
1413
Modules, like classes and grammars, are a kind of
1514
L<package|/language/packages>. Module objects are instances of the
16-
C<ModuleHOW> metaclass; this provides certain capabilities useful
17-
for creating namespaces, versioning, delegation and data encapsulation (see
15+
C<ModuleHOW> metaclass; this provides certain capabilities useful for
16+
creating namespaces, versioning, delegation and data encapsulation (see
1817
also L<class|/syntax/class> and L<role|/syntax/role>).
1918
2019
To create a module, use the C<module> declarator:
@@ -37,7 +36,7 @@ caller's namespace once the module has been imported with C<import>
3736
or C<use>. A module can also selectively expose symbols within its
3837
namespace for qualified reference via C<our>.
3938
40-
39+
X<|import>
4140
=head2 Working with modules
4241
4342
To illustrate module scoping and export rules, let's begin by
@@ -49,15 +48,14 @@ defining a simple module C<M>:
4948
sub friendly-greeting is export { greeting('friend') }
5049
}
5150
52-
Recall that subroutines are lexically scoped unless otherwise
53-
specified (declarator L<C<sub>|/syntax/sub>
54-
is equivalent to C<my sub>), so C<greeting> in the above example
55-
is lexically scoped to the module and inaccessible outside of it.
56-
We've also defined C<loud-greeting> with the C<our> declarator,
57-
which means that in addition to being lexically scoped it is aliased
58-
in the module's symbol table. Finally, C<friendly-greeting> is
59-
marked for export; it will be registered in the I<caller's> symbol
60-
table when the module is imported:
51+
Recall that subroutines are lexically scoped unless otherwise specified
52+
(declarator L<C<sub>|/syntax/sub> is equivalent to C<my sub>), so
53+
C<greeting> in the above example is lexically scoped to the module and
54+
inaccessible outside of it. We've also defined C<loud-greeting> with the
55+
C<our> declarator, which means that in addition to being lexically
56+
scoped it is aliased in the module's symbol table. Finally,
57+
C<friendly-greeting> is marked for export; it will be registered in the
58+
I<caller's> symbol table when the module is imported:
6159
6260
=begin code :skip-test<&friendly-greeting already exported>
6361
import M; # import the module

doc/Language/modules.pod6

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ L<need|/language/modules#need> MyModule;
9494
import MyModule;
9595
=end code
9696
97-
See also L<Selective Importing|/language/modules#Exporting_and_selective_importing> to restrict what you import.
97+
See also L<Selective Importing|/language/modules#Exporting_and_selective_importing>
98+
to restrict what you import.
9899
99100
=head3 C<require>X<|require>
100101
@@ -109,19 +110,22 @@ indirect lookup.
109110
my $name = 'MyModule';
110111
require ::($name);
111112
112-
The symbols provided by the loaded module will not be imported into the current
113-
scope. You may use L<dynamic lookup|/language/packages#index-entry-::()> or
114-
L<dynamic subsets|/language/typesystem#subset> to use them by providing the
115-
fully qualified name of a symbol, for instance:
113+
The symbols provided by the loaded module will not be imported into the
114+
current scope. You may use
115+
L<dynamic lookup|/language/packages#index-entry-::()> or
116+
L<dynamic subsets|/language/typesystem#subset> to use them by providing
117+
the fully qualified name of a symbol, for instance:
116118
117119
require ::("Test");
118120
my &mmk = ::("Test::EXPORT::DEFAULT::&ok");
119121
mmk('oi‽'); # OUTPUT: «ok 1 - ␤»
120122
121-
The FQN of C<ok> is C<Test::EXPORT::DEFAULT::&ok>. We are aliasing it to C<mmk> so that we can use that symbol provided by C<Test> in the current scope.
123+
The FQN of C<ok> is C<Test::EXPORT::DEFAULT::&ok>. We are aliasing it to
124+
C<mmk> so that we can use that symbol provided by C<Test> in the current
125+
scope.
122126
123-
To import symbols you must define them at compile time. B<NOTE:> C<require>
124-
is lexically scoped:
127+
To import symbols you must define them at compile time. B<NOTE:>
128+
C<require> is lexically scoped:
125129
126130
sub do-something {
127131
require MyModule <&something>;
@@ -134,10 +138,11 @@ is lexically scoped:
134138
135139
If C<MyModule> doesn't export C<&something> then C<require> will fail.
136140
137-
A C<require> with compile-time symbol will install a placeholder C<package>
138-
that will be updated to the loaded module, class, or package. Note that the
139-
placeholder will be kept, B<even if require failed to load the module.>
140-
This means that checking if a module loaded like this is wrong:
141+
A C<require> with compile-time symbol will install a placeholder
142+
C<package> that will be updated to the loaded module, class, or package.
143+
Note that the placeholder will be kept, B<even if require failed to load
144+
the module.> This means that checking if a module loaded like this is
145+
wrong:
141146
142147
# *** WRONG: ***
143148
try require Foo;
@@ -159,17 +164,17 @@ a C<Failure>. The correct way is:
159164
160165
=head2 Lexical module loading
161166
162-
Perl 6 takes great care to avoid global state, i.e. whatever you do in your
163-
module, it should not affect other code. That's why e.g. subroutine definitions
164-
are lexically (C<my>) scoped by default. If you want others to see them, you
165-
need to explicitly make them our scoped or export them.
167+
Perl 6 takes great care to avoid global state, i.e. whatever you do in
168+
your module, it should not affect other code. That's why e.g. subroutine
169+
definitions are lexically (C<my>) scoped by default. If you want others
170+
to see them, you need to explicitly make them our scoped or export them.
166171
167-
Classes are exported by default on the assumption that loading a module will not
168-
be of much use when you cannot access the classes it contains. This works as
169-
advertised with a small but important caveat: those classes are not only visible
170-
in the computation unit that loads the module, but globally. This means that as
171-
soon as some code loads a module, those classes are immediately visible
172-
everywhere.
172+
Classes are exported by default on the assumption that loading a module
173+
will not be of much use when you cannot access the classes it contains.
174+
This works as advertised with a small but important caveat: those
175+
classes are not only visible in the computation unit that loads the
176+
module, but globally. This means that as soon as some code loads a
177+
module, those classes are immediately visible everywhere.
173178
174179
For example, given a module C<Foo>:
175180
@@ -184,10 +189,10 @@ use Foo;
184189
my $foo = Foo.new; # works as expected
185190
my $bar = Bar.new; # huh!? Where is Bar coming from?
186191
187-
This doesn't sound so bad (it at least saves you some typing), except for that
188-
it makes another feature of Perl 6 impossible to have: the ability to load
189-
multiple versions of a module at the same time in different parts of your
190-
program:
192+
This doesn't sound so bad (it at least saves you some typing), except
193+
for that it makes another feature of Perl 6 impossible to have: the
194+
ability to load multiple versions of a module at the same time in
195+
different parts of your program:
191196
192197
=for code :skip-test
193198
{
@@ -269,15 +274,18 @@ tags: C<ALL>, C<DEFAULT> and C<MANDATORY>.
269274
use MyModule :ALL; # bag, pants, sunglasses, torch, underpants
270275
=end code
271276
272-
Note there currently is no way for the user to import a single object if
273-
the module author hasn't made provision for that, and it is not an easy
274-
task at the moment (see RT #127305). One way the author can provide
275-
such access is to give each C<export> trait its own unique tag. (And the tag
276-
can be the object name!) Then the user can either (1) import all objects:
277+
B<Note>: there currently is no way for the user to import a single
278+
object if the module author hasn't made provision for that, and it is
279+
not an easy task at the moment (see
280+
L<RT #127305|https://rt.perl.org/Public/Bug/Display.html?id=127305>).
281+
One way
282+
the author can provide such access is to give each C<export> trait its
283+
own unique tag. (And the tag can be the object name!) Then the user can
284+
either (1) import all objects:
277285
278-
=begin code :skip-test
279-
use Foo :ALL;
280-
=end code
286+
=begin code :skip-test
287+
use Foo :ALL;
288+
=end code
281289
282290
or (2) import one or more objects selectively:
283291

0 commit comments

Comments
 (0)