You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
98
99
99
100
=head3C<require>X<|require>
100
101
@@ -109,19 +110,22 @@ indirect lookup.
109
110
my $name = 'MyModule';
110
111
require ::($name);
111
112
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:
116
118
117
119
require ::("Test");
118
120
my &mmk = ::("Test::EXPORT::DEFAULT::&ok");
119
121
mmk('oi‽'); # OUTPUT: «ok 1 - »
120
122
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.
122
126
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:
125
129
126
130
sub do-something {
127
131
require MyModule <&something>;
@@ -134,10 +138,11 @@ is lexically scoped:
134
138
135
139
If C<MyModule> doesn't export C<&something> then C<require> will fail.
136
140
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:
141
146
142
147
# *** WRONG: ***
143
148
try require Foo;
@@ -159,17 +164,17 @@ a C<Failure>. The correct way is:
159
164
160
165
=head2Lexical module loading
161
166
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.
166
171
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.
173
178
174
179
For example, given a module C<Foo>:
175
180
@@ -184,10 +189,10 @@ use Foo;
184
189
my $foo = Foo.new; # works as expected
185
190
my $bar = Bar.new; # huh!? Where is Bar coming from?
186
191
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:
191
196
192
197
=forcode :skip-test
193
198
{
@@ -269,15 +274,18 @@ tags: C<ALL>, C<DEFAULT> and C<MANDATORY>.
269
274
use MyModule :ALL; # bag, pants, sunglasses, torch, underpants
270
275
=endcode
271
276
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
0 commit comments