@@ -33,165 +33,6 @@ written in Perl 6 you can use the C<.pm6> extension for modules, and the
33
33
C < .p6 > extension for scripts. Test files still use the normal C < .t >
34
34
extension.
35
35
36
- = head2 Names
37
-
38
- A I < name > is anything that is a legal part of a variable name (not counting
39
- the sigil). This includes
40
-
41
- $foo # simple identifiers
42
- $Foo::Bar::baz # compound identifiers separated by ::
43
- $Foo::($bar)::baz # compound identifiers that perform interpolations
44
- $42 # numeric names
45
- $! # certain punctuational variables
46
-
47
- When not used as a sigil, the semantic function of C < :: > within a name is to
48
- force the preceding portion of the name to be considered a package through
49
- which the subsequent portion of the name is to be located. If the preceding
50
- portion is null, it means the package is unspecified and must be searched
51
- for according to the nature of what follows. Generally this means that an
52
- initial C < :: > following the main sigil is a no-op on names that are known at
53
- compile time, though C < ::() > can also be used to introduce an interpolation
54
- (see below). Also, in the absence of another sigil, C < :: > can serve as its
55
- own sigil indicating intentional use of a not-yet-declared package name.
56
-
57
- = head3 Package-qualified names
58
-
59
- Ordinary package-qualified names look like:
60
-
61
- $Foo::Bar::baz # the $baz variable in package Foo::Bar
62
-
63
- Sometimes it's clearer to keep the sigil with the variable name, so an
64
- alternate way to write this is:
65
-
66
- Foo::Bar::<$baz>
67
-
68
- This is resolved at compile time because the variable name is a constant.
69
-
70
- = head3 Pseudo-packages
71
-
72
- The following pseudo-package names are reserved at the front of a name:
73
-
74
- MY # Symbols in the current lexical scope (aka $?SCOPE)
75
- OUR # Symbols in the current package (aka $?PACKAGE)
76
- CORE # Outermost lexical scope, definition of standard Perl
77
- GLOBAL # Interpreter-wide package symbols, really UNIT::GLOBAL
78
- PROCESS # Process-related globals (superglobals)
79
- COMPILING # Lexical symbols in the scope being compiled
80
-
81
- The following relative names are also reserved but may be used
82
- anywhere in a name:
83
-
84
- CALLER # Contextual symbols in the immediate caller's lexical scope
85
- CALLERS # Contextual symbols in any caller's lexical scope
86
- DYNAMIC # Contextual symbols in my or any caller's lexical scope
87
- OUTER # Symbols in the next outer lexical scope
88
- OUTERS # Symbols in any outer lexical scope
89
- LEXICAL # Contextual symbols in my or any outer's lexical scope
90
- UNIT # Symbols in the outermost lexical scope of compilation unit
91
- SETTING # Lexical symbols in the unit's DSL (usually CORE)
92
- PARENT # Symbols in this package's parent package (or lexical scope)
93
- CLIENT # The nearest CALLER that comes from a different package
94
-
95
- The file's scope is known as C < UNIT > , but there are one or more lexical
96
- scopes outside of that corresponding to the linguistic setting (often known
97
- as the prelude in other cultures). Hence, the C < SETTING > scope is
98
- equivalent to C < UNIT::OUTERS > . For a standard Perl program C < SETTING > is the
99
- same as C < CORE > , but various startup options (such as C < -n > or C < -p > ) can
100
- put you into a domain specific language, in which case C < CORE > remains the
101
- scope of the standard language, while C < SETTING > represents the scope
102
- defining the DSL that functions as the setting of the current file. When used
103
- as a search term in the middle of a name, C < SETTING > includes all its outer scopes
104
- up to C < CORE > . To get I < only > the setting's outermost scope, use C < UNIT::OUTER > instead.
105
-
106
- = head3 Interpolating into names
107
-
108
- You may interpolate a string into a package or variable name using
109
- C < ::($expr) > where you'd ordinarily put a package or variable name. The
110
- string is allowed to contain additional instances of C < :: > , which will be
111
- interpreted as package nesting. You may only interpolate entire names,
112
- since the construct starts with C < :: > , and either ends immediately or is
113
- continued with another C < :: > outside the parens. Most symbolic references
114
- are done with this notation:
115
-
116
- $foo = "Bar";
117
- $foobar = "Foo::Bar";
118
- $::($foo) # lexically-scoped $Bar
119
- $::("MY::$foo") # lexically-scoped $Bar
120
- $::("OUR::$foo") # package-scoped $Bar
121
- $::("GLOBAL::$foo") # global $Bar
122
- $::("PROCESS::$foo")# process $Bar
123
- $::("PARENT::$foo") # current package's parent's $Bar
124
- $::($foobar) # $Foo::Bar
125
- $::($foobar)::baz # $Foo::Bar::baz
126
- $::($foo)::Bar::baz # $Bar::Bar::baz
127
- $::($foobar)baz # ILLEGAL at compile time (no operator baz)
128
-
129
- Note that unlike in Perl 5, initial C < :: > doesn't imply global. Here as part
130
- of the interpolation syntax it doesn't even imply package. After the
131
- interpolation of the C < ::() > component, the indirect name is looked up exactly
132
- as if it had been there in the original source code, with priority given first
133
- to leading pseudo-package names, then to names in the lexical scope (searching
134
- scopes outwards, ending at C < CORE > ). The current package is searched last.
135
-
136
- Use the C < MY > pseudopackage to limit the lookup to the current lexical
137
- scope, and C < OUR > to limit the scopes to the current package scope.
138
-
139
- = head3 Direct lookup
140
-
141
- To do direct lookup in a package's symbol table without scanning, treat the
142
- package name as a hash:
143
-
144
- Foo::Bar::{'&baz'} # same as &Foo::Bar::baz
145
- PROCESS::<$IN> # Same as $*IN
146
- Foo::<::Bar><::Baz> # same as Foo::Bar::Baz
147
-
148
- The C < :: > before the subscript is required here, because the
149
- C < Foo::Bar{...} > syntax is reserved for attaching a "WHENCE" initialization
150
- closure to an autovivifiable type object. (see S12).
151
-
152
- Unlike C < ::() > symbolic references, this does not parse the argument for
153
- C < :: > , nor does it initiate a namespace scan from that initial point. In
154
- addition, for constant subscripts, it is guaranteed to resolve the symbol at
155
- compile time.
156
-
157
- The null pseudo-package is reserved to mean the same search list as an
158
- ordinary name search. That is, the following are all identical in meaning:
159
-
160
- $foo
161
- $::{'foo'}
162
- ::{'$foo'}
163
- $::<foo>
164
- ::<$foo>
165
-
166
- That is, each of them scans lexical scopes outward, and then the current
167
- package scope (though the package scope is then disallowed when "strict" is
168
- in effect).
169
-
170
- As a result of these rules, you can write any arbitrary variable name as
171
- either of:
172
-
173
- $::{'!@#$#@'}
174
- ::{'$!@#$#@'}
175
-
176
- You can also use the C << ::<> >> form as long as there are no spaces in the
177
- name.
178
-
179
- = head3 Package lookup
180
-
181
- Subscript the package object itself as a hash object, the key of which is
182
- the variable name, including any sigil. The package object can be derived
183
- from a type name by use of the C < :: > postfix:
184
-
185
- MyType::<$foo>
186
-
187
- = head3 Globals
188
-
189
- Interpreter globals live in the C < GLOBAL > package. The user's program
190
- starts in the C < GLOBAL > package, so "our" declarations in the mainline code
191
- go into that package by default. Process-wide variables live in the
192
- C < PROCESS > package. Most predefined globals such as C < $*UID > and C < $*PID >
193
- are actually process globals.
194
-
195
36
= head2 Loading and Basic Importing
196
37
197
38
Loading a module makes the packages in the same namespace declared
0 commit comments