Skip to content

Commit 86b4228

Browse files
authored
Update require section
- require is now lexical, so toss the routine-loaded module examples - Make it clear require is lexical, including the symbol that's been required - Include examples of correct checking for whether a module got loaded
1 parent 5bd81fb commit 86b4228

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

doc/Language/modules.pod6

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,39 +78,57 @@ It is equivalent to:
7878
7979
C<require> loads a compunit and imports definite symbols at runtime.
8080
81-
sub load-mymodule {
82-
say "loading MyModule";
83-
require MyModule;
84-
}
85-
86-
load-mymodule();
81+
82+
say "loading MyModule";
83+
require MyModule;
8784
8885
The compunit name can be in a runtime variable if you put it inside an
8986
indirect lookup.
9087
91-
sub load-a-module($name){
92-
require ::($name);
93-
}
94-
95-
load-a-module('MyModule');
88+
my $name = 'MyModule';
89+
require ::($name);
9690
9791
The symbols provided by the loaded module will not be imported into the current
9892
scope. You may use L<dynamic lookup|/language/packages#index-entry-::()> or
9993
L<dynamic subsets|/language/typesystem#subset> to use them by providing the
10094
fully qualified name of a symbol.
10195
102-
To import symbols you must define them at compile time.
96+
To import symbols you must define them at compile time. B<NOTE:> C<require>
97+
is lexically scoped:
10398
10499
sub do-something {
105100
require MyModule <&something>;
101+
say ::('MyModule'); # MyModule symbol exists here
106102
something() # &something will be defined here
107103
}
108-
104+
say ::('MyModule'); # This will NOT contain the MyModule symbol
109105
do-something();
110106
# &something will not be defined here
111107
112-
113-
If C<MyModule> doesn't export C<&something> then it will fail.
108+
If C<MyModule> doesn't export C<&something> then C<require> will fail.
109+
110+
A C<require> with compile-time symbol will install a placeholder C<package>
111+
that will be updated to the loaded module, class, or package. Note that the
112+
placeholder will be kept, B<even if require failed to load the module.>
113+
This means that checking if a module loaded like this is wrong:
114+
115+
# *** WRONG: ***
116+
try require Foo;
117+
if ::('Foo') ~~ Failure { say "Failed to load Foo!"; }
118+
# *** WRONG: ***
119+
120+
As the compile-time installed package causes C<::('Foo')> to never be
121+
a C<Failure>. The correct way is:
122+
123+
# Use return value to test whether loading succeeded:
124+
(try require Foo) === Nil and say "Failed to load Foo!";
125+
126+
# Or use a run-time symbol lookup with require, to avoid compile-time
127+
# package installation:
128+
try require ::('Foo');
129+
if ::('Foo') ~~ Failure {
130+
say "Failed to load Foo!";
131+
}
114132
115133
=head2 Exporting and Selective Importing
116134

0 commit comments

Comments
 (0)