@@ -78,39 +78,57 @@ It is equivalent to:
78
78
79
79
C < require > loads a compunit and imports definite symbols at runtime.
80
80
81
- sub load-mymodule {
82
- say "loading MyModule";
83
- require MyModule;
84
- }
85
-
86
- load-mymodule();
81
+
82
+ say "loading MyModule";
83
+ require MyModule;
87
84
88
85
The compunit name can be in a runtime variable if you put it inside an
89
86
indirect lookup.
90
87
91
- sub load-a-module($name){
92
- require ::($name);
93
- }
94
-
95
- load-a-module('MyModule');
88
+ my $name = 'MyModule';
89
+ require ::($name);
96
90
97
91
The symbols provided by the loaded module will not be imported into the current
98
92
scope. You may use L < dynamic lookup|/language/packages#index-entry-::() > or
99
93
L < dynamic subsets|/language/typesystem#subset > to use them by providing the
100
94
fully qualified name of a symbol.
101
95
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:
103
98
104
99
sub do-something {
105
100
require MyModule <&something>;
101
+ say ::('MyModule'); # MyModule symbol exists here
106
102
something() # &something will be defined here
107
103
}
108
-
104
+ say ::('MyModule'); # This will NOT contain the MyModule symbol
109
105
do-something();
110
106
# &something will not be defined here
111
107
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
+ }
114
132
115
133
= head2 Exporting and Selective Importing
116
134
0 commit comments