Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | =head1 NAME |
2 | ||
3 | perlvar - Perl predefined variables | |
4 | ||
5 | =head1 DESCRIPTION | |
6 | ||
b0c22438 | 7 | =head2 The Syntax of Variable Names |
8 | ||
241a59d9 | 9 | Variable names in Perl can have several formats. Usually, they |
b0c22438 | 10 | must begin with a letter or underscore, in which case they can be |
11 | arbitrarily long (up to an internal limit of 251 characters) and | |
12 | may contain letters, digits, underscores, or the special sequence | |
241a59d9 | 13 | C<::> or C<'>. In this case, the part before the last C<::> or |
b0c22438 | 14 | C<'> is taken to be a I<package qualifier>; see L<perlmod>. |
ce4793f1 KW |
15 | A Unicode letter that is not ASCII is not considered to be a letter |
16 | unless S<C<"use utf8">> is in effect, and somewhat more complicated | |
17 | rules apply; see L<perldata/Identifier parsing> for details. | |
b0c22438 | 18 | |
ce4793f1 KW |
19 | Perl variable names may also be a sequence of digits, a single |
20 | punctuation character, or the two-character sequence: C<^> (caret or | |
21 | CIRCUMFLEX ACCENT) followed by any one of the characters C<[][A-Z^_?\]>. | |
22 | These names are all reserved for | |
b0c22438 | 23 | special uses by Perl; for example, the all-digits names are used |
24 | to hold data captured by backreferences after a regular expression | |
ce4793f1 | 25 | match. |
b0c22438 | 26 | |
ce4793f1 | 27 | Since Perl v5.6.0, Perl variable names may also be alphanumeric strings |
2cfe7629 YO |
28 | preceded by a caret. These must all be written using the demarcated |
29 | variable form using curly braces such as C<${^Foo}>; | |
30 | the braces are B<not> optional. C<${^Foo}> denotes the scalar variable | |
ce4793f1 | 31 | whose name is considered to be a control-C<F> followed by two C<o>'s. |
2cfe7629 YO |
32 | (See L<perldata/"Demarcated variable names using braces"> for more |
33 | information on this form of spelling a variable name or specifying | |
34 | access to an element of an array or a hash). | |
ce4793f1 | 35 | These variables are |
b0c22438 | 36 | reserved for future special uses by Perl, except for the ones that |
ce4793f1 KW |
37 | begin with C<^_> (caret-underscore). No |
38 | name that begins with C<^_> will acquire a special | |
b0c22438 | 39 | meaning in any future version of Perl; such names may therefore be |
241a59d9 | 40 | used safely in programs. C<$^_> itself, however, I<is> reserved. |
b0c22438 | 41 | |
2cfe7629 YO |
42 | Note that you also B<must> use the demarcated form to access subscripts |
43 | of variables of this type when interpolating, for instance to access the | |
44 | first element of the C<@{^CAPTURE}> variable inside of a double quoted | |
45 | string you would write C<"${^CAPTURE[0]}"> and NOT C<"${^CAPTURE}[0]"> | |
46 | which would mean to reference a scalar variable named C<${^CAPTURE}> and | |
47 | not index 0 of the magic C<@{^CAPTURE}> array which is populated by the | |
48 | regex engine. | |
49 | ||
ce4793f1 | 50 | Perl identifiers that begin with digits or |
b0c22438 | 51 | punctuation characters are exempt from the effects of the C<package> |
52 | declaration and are always forced to be in package C<main>; they are | |
241a59d9 | 53 | also exempt from C<strict 'vars'> errors. A few other names are also |
b0c22438 | 54 | exempt in these ways: |
55 | ||
9548c15c FC |
56 | ENV STDIN |
57 | INC STDOUT | |
58 | ARGV STDERR | |
59 | ARGVOUT | |
60 | SIG | |
b0c22438 | 61 | |
69520822 | 62 | In particular, the special C<${^_XYZ}> variables are always taken |
b0c22438 | 63 | to be in package C<main>, regardless of any C<package> declarations |
64 | presently in scope. | |
65 | ||
66 | =head1 SPECIAL VARIABLES | |
a0d0e21e | 67 | |
241a59d9 | 68 | The following names have special meaning to Perl. Most punctuation |
0b9346e6 | 69 | names have reasonable mnemonics, or analogs in the shells. |
70 | Nevertheless, if you wish to use long variable names, you need only say: | |
a0d0e21e | 71 | |
9548c15c | 72 | use English; |
a0d0e21e | 73 | |
241a59d9 FC |
74 | at the top of your program. This aliases all the short names to the long |
75 | names in the current package. Some even have medium names, generally | |
1e7d0944 | 76 | borrowed from B<awk>. For more info, please see L<English>. |
a1ce9542 | 77 | |
241a59d9 | 78 | Before you continue, note the sort order for variables. In general, we |
0b9346e6 | 79 | first list the variables in case-insensitive, almost-lexigraphical |
80 | order (ignoring the C<{> or C<^> preceding words, as in C<${^UNICODE}> | |
81 | or C<$^T>), although C<$_> and C<@_> move up to the top of the pile. | |
82 | For variables with the same identifier, we list it in order of scalar, | |
83 | array, hash, and bareword. | |
a1ce9542 | 84 | |
b0c22438 | 85 | =head2 General Variables |
a0d0e21e | 86 | |
84dabc03 | 87 | =over 8 |
88 | ||
a0d0e21e LW |
89 | =item $ARG |
90 | ||
91 | =item $_ | |
a054c801 | 92 | X<$_> X<$ARG> |
a0d0e21e | 93 | |
241a59d9 | 94 | The default input and pattern-searching space. The following pairs are |
a0d0e21e LW |
95 | equivalent: |
96 | ||
9548c15c FC |
97 | while (<>) {...} # equivalent only in while! |
98 | while (defined($_ = <>)) {...} | |
a0d0e21e | 99 | |
9548c15c FC |
100 | /^Subject:/ |
101 | $_ =~ /^Subject:/ | |
a0d0e21e | 102 | |
9548c15c FC |
103 | tr/a-z/A-Z/ |
104 | $_ =~ tr/a-z/A-Z/ | |
a0d0e21e | 105 | |
9548c15c FC |
106 | chomp |
107 | chomp($_) | |
a0d0e21e | 108 | |
0b9346e6 | 109 | Here are the places where Perl will assume C<$_> even if you don't use it: |
cb1a09d0 AD |
110 | |
111 | =over 3 | |
112 | ||
113 | =item * | |
114 | ||
84dabc03 | 115 | The following functions use C<$_> as a default argument: |
db1511c8 | 116 | |
f61f53cc | 117 | abs, alarm, chomp, chop, chr, chroot, |
ae815a4d FC |
118 | cos, defined, eval, evalbytes, exp, fc, glob, hex, int, lc, |
119 | lcfirst, length, log, lstat, mkdir, oct, ord, pos, print, printf, | |
b0169937 | 120 | quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only), |
ae815a4d FC |
121 | rmdir, say, sin, split (for its second |
122 | argument), sqrt, stat, study, uc, ucfirst, | |
b0169937 | 123 | unlink, unpack. |
cb1a09d0 AD |
124 | |
125 | =item * | |
126 | ||
db1511c8 GS |
127 | All file tests (C<-f>, C<-d>) except for C<-t>, which defaults to STDIN. |
128 | See L<perlfunc/-X> | |
129 | ||
cb1a09d0 AD |
130 | =item * |
131 | ||
b0169937 GS |
132 | The pattern matching operations C<m//>, C<s///> and C<tr///> (aka C<y///>) |
133 | when used without an C<=~> operator. | |
cb1a09d0 | 134 | |
54310121 | 135 | =item * |
cb1a09d0 AD |
136 | |
137 | The default iterator variable in a C<foreach> loop if no other | |
138 | variable is supplied. | |
139 | ||
54310121 | 140 | =item * |
cb1a09d0 | 141 | |
b0c22438 | 142 | The implicit iterator variable in the C<grep()> and C<map()> functions. |
cb1a09d0 | 143 | |
54310121 | 144 | =item * |
cb1a09d0 | 145 | |
b0c22438 | 146 | The implicit variable of C<given()>. |
db1511c8 GS |
147 | |
148 | =item * | |
149 | ||
ae815a4d FC |
150 | The default place to put the next value or input record |
151 | when a C<< <FH> >>, C<readline>, C<readdir> or C<each> | |
cb1a09d0 | 152 | operation's result is tested by itself as the sole criterion of a C<while> |
241a59d9 | 153 | test. Outside a C<while> test, this will not happen. |
cb1a09d0 AD |
154 | |
155 | =back | |
156 | ||
5f6af817 RS |
157 | C<$_> is a global variable. |
158 | ||
159 | However, between perl v5.10.0 and v5.24.0, it could be used lexically by | |
160 | writing C<my $_>. Making C<$_> refer to the global C<$_> in the same scope | |
161 | was then possible with C<our $_>. This experimental feature was removed and is | |
162 | now a fatal error, but you may encounter it in older code. | |
59f00321 | 163 | |
b0c22438 | 164 | Mnemonic: underline is understood in certain operations. |
a0d0e21e | 165 | |
0b9346e6 | 166 | =item @ARG |
cde0cee5 | 167 | |
0b9346e6 | 168 | =item @_ |
169 | X<@_> X<@ARG> | |
a0d0e21e | 170 | |
0b9346e6 | 171 | Within a subroutine the array C<@_> contains the parameters passed to |
241a59d9 | 172 | that subroutine. Inside a subroutine, C<@_> is the default array for |
256ca3d3 | 173 | the array operators C<pop> and C<shift>. |
a0d0e21e | 174 | |
0b9346e6 | 175 | See L<perlsub>. |
a0d0e21e | 176 | |
1311257d | 177 | =item $LIST_SEPARATOR |
178 | ||
179 | =item $" | |
180 | X<$"> X<$LIST_SEPARATOR> | |
181 | ||
69520822 | 182 | When an array or an array slice is interpolated into a double-quoted |
183 | string or a similar context such as C</.../>, its elements are | |
241a59d9 | 184 | separated by this value. Default is a space. For example, this: |
69520822 | 185 | |
9548c15c | 186 | print "The array is: @array\n"; |
69520822 | 187 | |
188 | is equivalent to this: | |
189 | ||
9548c15c | 190 | print "The array is: " . join($", @array) . "\n"; |
69520822 | 191 | |
192 | Mnemonic: works in double-quoted context. | |
1311257d | 193 | |
b0c22438 | 194 | =item $PROCESS_ID |
cde0cee5 | 195 | |
b0c22438 | 196 | =item $PID |
a0d0e21e | 197 | |
b0c22438 | 198 | =item $$ |
199 | X<$$> X<$PID> X<$PROCESS_ID> | |
a0d0e21e | 200 | |
241a59d9 | 201 | The process number of the Perl running this script. Though you I<can> set |
4a904372 | 202 | this variable, doing so is generally discouraged, although it can be |
241a59d9 | 203 | invaluable for some testing purposes. It will be reset automatically |
b0c22438 | 204 | across C<fork()> calls. |
a0d0e21e | 205 | |
d7c042c9 AB |
206 | Note for Linux and Debian GNU/kFreeBSD users: Before Perl v5.16.0 perl |
207 | would emulate POSIX semantics on Linux systems using LinuxThreads, a | |
208 | partial implementation of POSIX Threads that has since been superseded | |
209 | by the Native POSIX Thread Library (NPTL). | |
210 | ||
e3f68f70 | 211 | LinuxThreads is now obsolete on Linux, and caching C<getpid()> |
d7c042c9 AB |
212 | like this made embedding perl unnecessarily complex (since you'd have |
213 | to manually update the value of $$), so now C<$$> and C<getppid()> | |
214 | will always return the same values as the underlying C library. | |
215 | ||
216 | Debian GNU/kFreeBSD systems also used LinuxThreads up until and | |
217 | including the 6.0 release, but after that moved to FreeBSD thread | |
218 | semantics, which are POSIX-like. | |
219 | ||
220 | To see if your system is affected by this discrepancy check if | |
221 | C<getconf GNU_LIBPTHREAD_VERSION | grep -q NPTL> returns a false | |
1e7d0944 | 222 | value. NTPL threads preserve the POSIX semantics. |
a0d0e21e | 223 | |
b0c22438 | 224 | Mnemonic: same as shells. |
ad83b128 | 225 | |
66d7055b DR |
226 | =item $PROGRAM_NAME |
227 | ||
228 | =item $0 | |
229 | X<$0> X<$PROGRAM_NAME> | |
230 | ||
231 | Contains the name of the program being executed. | |
232 | ||
233 | On some (but not all) operating systems assigning to C<$0> modifies | |
241a59d9 | 234 | the argument area that the C<ps> program sees. On some platforms you |
66d7055b | 235 | may have to use special C<ps> options or a different C<ps> to see the |
241a59d9 | 236 | changes. Modifying the C<$0> is more useful as a way of indicating the |
66d7055b DR |
237 | current program state than it is for hiding the program you're |
238 | running. | |
239 | ||
240 | Note that there are platform-specific limitations on the maximum | |
241a59d9 | 241 | length of C<$0>. In the most extreme case it may be limited to the |
66d7055b DR |
242 | space occupied by the original C<$0>. |
243 | ||
244 | In some platforms there may be arbitrary amount of padding, for | |
245 | example space characters, after the modified name as shown by C<ps>. | |
246 | In some platforms this padding may extend all the way to the original | |
247 | length of the argument area, no matter what you do (this is the case | |
248 | for example with Linux 2.2). | |
249 | ||
250 | Note for BSD users: setting C<$0> does not completely remove "perl" | |
241a59d9 | 251 | from the ps(1) output. For example, setting C<$0> to C<"foobar"> may |
66d7055b DR |
252 | result in C<"perl: foobar (perl)"> (whether both the C<"perl: "> prefix |
253 | and the " (perl)" suffix are shown depends on your exact BSD variant | |
241a59d9 | 254 | and version). This is an operating system feature, Perl cannot help it. |
66d7055b DR |
255 | |
256 | In multithreaded scripts Perl coordinates the threads so that any | |
257 | thread may modify its copy of the C<$0> and the change becomes visible | |
241a59d9 | 258 | to ps(1) (assuming the operating system plays along). Note that |
66d7055b DR |
259 | the view of C<$0> the other threads have will not change since they |
260 | have their own copies of it. | |
261 | ||
262 | If the program has been given to perl via the switches C<-e> or C<-E>, | |
263 | C<$0> will contain the string C<"-e">. | |
264 | ||
60cf4914 | 265 | On Linux as of perl v5.14.0 the legacy process name will be set with |
66d7055b | 266 | C<prctl(2)>, in addition to altering the POSIX name via C<argv[0]> as |
241a59d9 | 267 | perl has done since version 4.000. Now system utilities that read the |
66d7055b | 268 | legacy process name such as ps, top and killall will recognize the |
241a59d9 | 269 | name you set when assigning to C<$0>. The string you supply will be |
66d7055b DR |
270 | cut off at 16 bytes, this is a limitation imposed by Linux. |
271 | ||
2d3b3561 FG |
272 | Wide characters are invalid in C<$0> values. For historical reasons, |
273 | though, Perl accepts them and encodes them to UTF-8. When this | |
274 | happens a wide-character warning is triggered. | |
275 | ||
66d7055b DR |
276 | Mnemonic: same as B<sh> and B<ksh>. |
277 | ||
b0c22438 | 278 | =item $REAL_GROUP_ID |
a01268b5 | 279 | |
b0c22438 | 280 | =item $GID |
a01268b5 | 281 | |
b0c22438 | 282 | =item $( |
283 | X<$(> X<$GID> X<$REAL_GROUP_ID> | |
a01268b5 | 284 | |
241a59d9 | 285 | The real gid of this process. If you are on a machine that supports |
b0c22438 | 286 | membership in multiple groups simultaneously, gives a space separated |
241a59d9 | 287 | list of groups you are in. The first number is the one returned by |
b0c22438 | 288 | C<getgid()>, and the subsequent ones by C<getgroups()>, one of which may be |
289 | the same as the first number. | |
a01268b5 | 290 | |
b0c22438 | 291 | However, a value assigned to C<$(> must be a single number used to |
241a59d9 FC |
292 | set the real gid. So the value given by C<$(> should I<not> be assigned |
293 | back to C<$(> without being forced numeric, such as by adding zero. Note | |
b0c22438 | 294 | that this is different to the effective gid (C<$)>) which does take a |
295 | list. | |
fe307981 | 296 | |
b0c22438 | 297 | You can change both the real gid and the effective gid at the same |
241a59d9 FC |
298 | time by using C<POSIX::setgid()>. Changes |
299 | to C<$(> require a check to C<$!> | |
b0c22438 | 300 | to detect any possible errors after an attempted change. |
6cef1e77 | 301 | |
241a59d9 | 302 | Mnemonic: parentheses are used to I<group> things. The real gid is the |
b0c22438 | 303 | group you I<left>, if you're running setgid. |
6cef1e77 | 304 | |
b0c22438 | 305 | =item $EFFECTIVE_GROUP_ID |
8e08999f | 306 | |
b0c22438 | 307 | =item $EGID |
81714fb9 | 308 | |
b0c22438 | 309 | =item $) |
310 | X<$)> X<$EGID> X<$EFFECTIVE_GROUP_ID> | |
81714fb9 | 311 | |
241a59d9 | 312 | The effective gid of this process. If you are on a machine that |
b0c22438 | 313 | supports membership in multiple groups simultaneously, gives a space |
241a59d9 | 314 | separated list of groups you are in. The first number is the one |
b0c22438 | 315 | returned by C<getegid()>, and the subsequent ones by C<getgroups()>, |
316 | one of which may be the same as the first number. | |
81714fb9 | 317 | |
b0c22438 | 318 | Similarly, a value assigned to C<$)> must also be a space-separated |
241a59d9 FC |
319 | list of numbers. The first number sets the effective gid, and |
320 | the rest (if any) are passed to C<setgroups()>. To get the effect of an | |
b0c22438 | 321 | empty list for C<setgroups()>, just repeat the new effective gid; that is, |
322 | to force an effective gid of 5 and an effectively empty C<setgroups()> | |
323 | list, say C< $) = "5 5" >. | |
81714fb9 | 324 | |
b0c22438 | 325 | You can change both the effective gid and the real gid at the same |
326 | time by using C<POSIX::setgid()> (use only a single numeric argument). | |
327 | Changes to C<$)> require a check to C<$!> to detect any possible errors | |
328 | after an attempted change. | |
44a2ac75 | 329 | |
b0c22438 | 330 | C<< $< >>, C<< $> >>, C<$(> and C<$)> can be set only on |
241a59d9 | 331 | machines that support the corresponding I<set[re][ug]id()> routine. C<$(> |
b0c22438 | 332 | and C<$)> can be swapped only on machines supporting C<setregid()>. |
3195cf34 | 333 | |
241a59d9 | 334 | Mnemonic: parentheses are used to I<group> things. The effective gid |
b0c22438 | 335 | is the group that's I<right> for you, if you're running setgid. |
44a2ac75 | 336 | |
c82f2f4e DR |
337 | =item $REAL_USER_ID |
338 | ||
339 | =item $UID | |
340 | ||
341 | =item $< | |
342 | X<< $< >> X<$UID> X<$REAL_USER_ID> | |
343 | ||
241a59d9 FC |
344 | The real uid of this process. You can change both the real uid and the |
345 | effective uid at the same time by using C<POSIX::setuid()>. Since | |
c82f2f4e DR |
346 | changes to C<< $< >> require a system call, check C<$!> after a change |
347 | attempt to detect any possible errors. | |
348 | ||
349 | Mnemonic: it's the uid you came I<from>, if you're running setuid. | |
350 | ||
351 | =item $EFFECTIVE_USER_ID | |
352 | ||
353 | =item $EUID | |
354 | ||
355 | =item $> | |
356 | X<< $> >> X<$EUID> X<$EFFECTIVE_USER_ID> | |
357 | ||
241a59d9 | 358 | The effective uid of this process. For example: |
c82f2f4e | 359 | |
9548c15c FC |
360 | $< = $>; # set real to effective uid |
361 | ($<,$>) = ($>,$<); # swap real and effective uids | |
c82f2f4e DR |
362 | |
363 | You can change both the effective uid and the real uid at the same | |
241a59d9 | 364 | time by using C<POSIX::setuid()>. Changes to C<< $> >> require a check |
c82f2f4e DR |
365 | to C<$!> to detect any possible errors after an attempted change. |
366 | ||
367 | C<< $< >> and C<< $> >> can be swapped only on machines | |
368 | supporting C<setreuid()>. | |
369 | ||
370 | Mnemonic: it's the uid you went I<to>, if you're running setuid. | |
371 | ||
0b9346e6 | 372 | =item $SUBSCRIPT_SEPARATOR |
373 | ||
374 | =item $SUBSEP | |
375 | ||
376 | =item $; | |
377 | X<$;> X<$SUBSEP> X<SUBSCRIPT_SEPARATOR> | |
378 | ||
241a59d9 | 379 | The subscript separator for multidimensional array emulation. If you |
0b9346e6 | 380 | refer to a hash element as |
381 | ||
592708b4 | 382 | $foo{$x,$y,$z} |
0b9346e6 | 383 | |
384 | it really means | |
385 | ||
592708b4 | 386 | $foo{join($;, $x, $y, $z)} |
0b9346e6 | 387 | |
388 | But don't put | |
389 | ||
0763c253 | 390 | @foo{$x,$y,$z} # a slice--note the @ |
0b9346e6 | 391 | |
392 | which means | |
393 | ||
592708b4 | 394 | ($foo{$x},$foo{$y},$foo{$z}) |
0b9346e6 | 395 | |
241a59d9 | 396 | Default is "\034", the same as SUBSEP in B<awk>. If your keys contain |
0b9346e6 | 397 | binary data there might not be any safe value for C<$;>. |
398 | ||
399 | Consider using "real" multidimensional arrays as described | |
400 | in L<perllol>. | |
401 | ||
402 | Mnemonic: comma (the syntactic subscript separator) is a semi-semicolon. | |
403 | ||
0b9346e6 | 404 | =item $a |
405 | ||
406 | =item $b | |
407 | X<$a> X<$b> | |
408 | ||
409 | Special package variables when using C<sort()>, see L<perlfunc/sort>. | |
410 | Because of this specialness C<$a> and C<$b> don't need to be declared | |
411 | (using C<use vars>, or C<our()>) even when using the C<strict 'vars'> | |
241a59d9 | 412 | pragma. Don't lexicalize them with C<my $a> or C<my $b> if you want to |
0b9346e6 | 413 | be able to use them in the C<sort()> comparison block or function. |
414 | ||
0b9346e6 | 415 | =item %ENV |
416 | X<%ENV> | |
417 | ||
241a59d9 | 418 | The hash C<%ENV> contains your current environment. Setting a |
0b9346e6 | 419 | value in C<ENV> changes the environment for any child processes |
420 | you subsequently C<fork()> off. | |
421 | ||
32e006ac | 422 | As of v5.18.0, both keys and values stored in C<%ENV> are stringified. |
a5effbbc KF |
423 | |
424 | my $foo = 1; | |
425 | $ENV{'bar'} = \$foo; | |
426 | if( ref $ENV{'bar'} ) { | |
32e006ac | 427 | say "Pre 5.18.0 Behaviour"; |
a5effbbc | 428 | } else { |
32e006ac | 429 | say "Post 5.18.0 Behaviour"; |
a5effbbc KF |
430 | } |
431 | ||
432 | Previously, only child processes received stringified values: | |
433 | ||
434 | my $foo = 1; | |
435 | $ENV{'bar'} = \$foo; | |
436 | ||
437 | # Always printed 'non ref' | |
6d3f582d FC |
438 | system($^X, '-e', |
439 | q/print ( ref $ENV{'bar'} ? 'ref' : 'non ref' ) /); | |
a5effbbc KF |
440 | |
441 | This happens because you can't really share arbitrary data structures with | |
442 | foreign processes. | |
443 | ||
d2578154 KE |
444 | =item $OLD_PERL_VERSION |
445 | ||
4ad0ecd4 | 446 | =item $] |
b77ebf74 | 447 | X<$]> X<$OLD_PERL_VERSION> |
4ad0ecd4 KE |
448 | |
449 | The revision, version, and subversion of the Perl interpreter, represented | |
450 | as a decimal of the form 5.XXXYYY, where XXX is the version / 1e3 and YYY | |
451 | is the subversion / 1e6. For example, Perl v5.10.1 would be "5.010001". | |
452 | ||
453 | This variable can be used to determine whether the Perl interpreter | |
454 | executing a script is in the right range of versions: | |
455 | ||
601390ce | 456 | warn "No PerlIO!\n" if "$]" < 5.008; |
4ad0ecd4 | 457 | |
601390ce DB |
458 | When comparing C<$]>, numeric comparison operators should be used, but the |
459 | variable should be stringified first to avoid issues where its original | |
460 | numeric value is inaccurate. | |
4ad0ecd4 | 461 | |
cd79443d PE |
462 | See also the documentation of L<C<use VERSION>|perlfunc/use VERSION> and |
463 | C<require VERSION> for a convenient way to fail if the running Perl | |
464 | interpreter is too old. | |
4ad0ecd4 KE |
465 | |
466 | See L</$^V> for a representation of the Perl version as a L<version> | |
467 | object, which allows more flexible string comparisons. | |
468 | ||
469 | The main advantage of C<$]> over C<$^V> is that it works the same on any | |
470 | version of Perl. The disadvantages are that it can't easily be compared | |
471 | to versions in other formats (e.g. literal v-strings, "v1.2.3" or | |
601390ce DB |
472 | version objects) and numeric comparisons are subject to the binary |
473 | floating point representation; it's good for numeric literal version | |
474 | checks and bad for comparing to a variable that hasn't been | |
475 | sanity-checked. | |
4ad0ecd4 | 476 | |
9fcc2314 AP |
477 | The C<$OLD_PERL_VERSION> form was added in Perl v5.20.0 for historical |
478 | reasons but its use is discouraged. (If your reason to use C<$]> is to | |
479 | run code on old perls then referring to it as C<$OLD_PERL_VERSION> would | |
480 | be self-defeating.) | |
4ad0ecd4 | 481 | |
9fcc2314 | 482 | Mnemonic: Is this version of perl in the right bracket? |
83c1fffe | 483 | |
b0c22438 | 484 | =item $SYSTEM_FD_MAX |
5b2b9c68 | 485 | |
b0c22438 | 486 | =item $^F |
487 | X<$^F> X<$SYSTEM_FD_MAX> | |
5b2b9c68 | 488 | |
241a59d9 | 489 | The maximum system file descriptor, ordinarily 2. System file |
b0c22438 | 490 | descriptors are passed to C<exec()>ed processes, while higher file |
241a59d9 FC |
491 | descriptors are not. Also, during an |
492 | C<open()>, system file descriptors are | |
b0c22438 | 493 | preserved even if the C<open()> fails (ordinary file descriptors are |
241a59d9 | 494 | closed before the C<open()> is attempted). The close-on-exec |
b0c22438 | 495 | status of a file descriptor will be decided according to the value of |
496 | C<$^F> when the corresponding file, pipe, or socket was opened, not the | |
497 | time of the C<exec()>. | |
5b2b9c68 | 498 | |
0b9346e6 | 499 | =item @F |
500 | X<@F> | |
501 | ||
502 | The array C<@F> contains the fields of each line read in when autosplit | |
028611fa DB |
503 | mode is turned on. See L<perlrun|perlrun/-a> for the B<-a> switch. This |
504 | array is package-specific, and must be declared or given a full package | |
505 | name if not in package main when running under C<strict 'vars'>. | |
0b9346e6 | 506 | |
0b9346e6 | 507 | =item @INC |
508 | X<@INC> | |
509 | ||
510 | The array C<@INC> contains the list of places that the C<do EXPR>, | |
241a59d9 | 511 | C<require>, or C<use> constructs look for their library files. It |
0b9346e6 | 512 | initially consists of the arguments to any B<-I> command-line |
513 | switches, followed by the default Perl library, probably | |
87e54f02 N |
514 | F</usr/local/lib/perl>. |
515 | Prior to Perl 5.26, C<.> -which represents the current directory, was included | |
516 | in C<@INC>; it has been removed. This change in behavior is documented | |
517 | in L<C<PERL_USE_UNSAFE_INC>|perlrun/PERL_USE_UNSAFE_INC> and it is | |
518 | not recommended that C<.> be re-added to C<@INC>. | |
519 | If you need to modify C<@INC> at runtime, you should use the C<use lib> pragma | |
520 | to get the machine-dependent library properly loaded as well: | |
0b9346e6 | 521 | |
9548c15c FC |
522 | use lib '/mypath/libdir/'; |
523 | use SomeMod; | |
0b9346e6 | 524 | |
525 | You can also insert hooks into the file inclusion system by putting Perl | |
241a59d9 FC |
526 | code directly into C<@INC>. Those hooks may be subroutine references, |
527 | array references or blessed objects. See L<perlfunc/require> for details. | |
0b9346e6 | 528 | |
529 | =item %INC | |
530 | X<%INC> | |
531 | ||
532 | The hash C<%INC> contains entries for each filename included via the | |
241a59d9 | 533 | C<do>, C<require>, or C<use> operators. The key is the filename |
0b9346e6 | 534 | you specified (with module names converted to pathnames), and the |
241a59d9 | 535 | value is the location of the file found. The C<require> |
0b9346e6 | 536 | operator uses this hash to determine whether a particular file has |
537 | already been included. | |
538 | ||
539 | If the file was loaded via a hook (e.g. a subroutine reference, see | |
540 | L<perlfunc/require> for a description of these hooks), this hook is | |
241a59d9 | 541 | by default inserted into C<%INC> in place of a filename. Note, however, |
0b9346e6 | 542 | that the hook may have set the C<%INC> entry by itself to provide some more |
543 | specific info. | |
544 | ||
494c34ea YO |
545 | =item $INC |
546 | X<$INC> | |
547 | ||
548 | As of 5.37.7 when an C<@INC> hook is executed the index of the C<@INC> | |
549 | array that holds the hook will be localized into the C<$INC> variable. | |
550 | When the hook returns the integer successor of its value will be used to | |
551 | determine the next index in C<@INC> that will be checked, thus if it is | |
552 | set to -1 (or C<undef>) the traversal over the C<@INC> array will be | |
553 | restarted from its beginning. | |
554 | ||
555 | Normally traversal through the C<@INC> array is from beginning to end | |
556 | (C<0 .. $#INC>), and if the C<@INC> array is modified by the hook the | |
557 | iterator may be left in a state where newly added entries are skipped. | |
558 | Changing this value allows an C<@INC> hook to rewrite the C<@INC> array | |
559 | and tell Perl where to continue afterwards. See L<perlfunc/require> for | |
560 | details on C<@INC> hooks. | |
561 | ||
b0c22438 | 562 | =item $INPLACE_EDIT |
a0d0e21e | 563 | |
b0c22438 | 564 | =item $^I |
565 | X<$^I> X<$INPLACE_EDIT> | |
a0d0e21e | 566 | |
241a59d9 | 567 | The current value of the inplace-edit extension. Use C<undef> to disable |
b0c22438 | 568 | inplace editing. |
a0d0e21e | 569 | |
b0c22438 | 570 | Mnemonic: value of B<-i> switch. |
a0d0e21e | 571 | |
bf38d944 H |
572 | =item @ISA |
573 | X<@ISA> | |
574 | ||
575 | Each package contains a special array called C<@ISA> which contains a list | |
576 | of that class's parent classes, if any. This array is simply a list of | |
577 | scalars, each of which is a string that corresponds to a package name. The | |
578 | array is examined when Perl does method resolution, which is covered in | |
579 | L<perlobj>. | |
580 | ||
5312fe6c AP |
581 | To load packages while adding them to C<@ISA>, see the L<parent> pragma. The |
582 | discouraged L<base> pragma does this as well, but should not be used except | |
583 | when compatibility with the discouraged L<fields> pragma is required. | |
bf38d944 | 584 | |
b0c22438 | 585 | =item $^M |
586 | X<$^M> | |
a0d0e21e | 587 | |
b0c22438 | 588 | By default, running out of memory is an untrappable, fatal error. |
589 | However, if suitably built, Perl can use the contents of C<$^M> | |
241a59d9 | 590 | as an emergency memory pool after C<die()>ing. Suppose that your Perl |
b0c22438 | 591 | were compiled with C<-DPERL_EMERGENCY_SBRK> and used Perl's malloc. |
592 | Then | |
a0d0e21e | 593 | |
9548c15c | 594 | $^M = 'a' x (1 << 16); |
a0d0e21e | 595 | |
241a59d9 | 596 | would allocate a 64K buffer for use in an emergency. See the |
00b38873 | 597 | L<INSTALL> file in the Perl distribution for information on how to |
241a59d9 | 598 | add custom C compilation flags when compiling perl. To discourage casual |
b0c22438 | 599 | use of this advanced feature, there is no L<English|English> long name for |
600 | this variable. | |
a0d0e21e | 601 | |
b0c22438 | 602 | This variable was added in Perl 5.004. |
a0d0e21e | 603 | |
741a5c73 YO |
604 | =item ${^MAX_NESTED_EVAL_BEGIN_BLOCKS} |
605 | ||
606 | This variable determines the maximum number C<eval EXPR>/C<BEGIN> or | |
607 | C<require>/C<BEGIN> block nesting that is allowed. This means it also | |
608 | controls the maximum nesting of C<use> statements as well. | |
609 | ||
610 | The default of 1000 should be sufficiently large for normal working | |
611 | purposes, and if you must raise it then you should be conservative | |
612 | with your choice or you may encounter segfaults from exhaustion of | |
613 | the C stack. It seems unlikely that real code has a use depth above | |
614 | 1000, but we have left this configurable just in case. | |
615 | ||
616 | When set to C<0> then C<BEGIN> blocks inside of C<eval EXPR> or | |
617 | C<require EXPR> are forbidden entirely and will trigger an exception | |
618 | which will terminate the compilation and in the case of C<require> | |
619 | will throw an exception, or in the case of C<eval> return the error in | |
620 | C<$@> as usual. | |
621 | ||
622 | Consider the code | |
623 | ||
624 | perl -le'sub f { eval "BEGIN { f() }"; } f()' | |
625 | ||
626 | each invocation of C<f()> will consume considerable C stack, and this | |
627 | variable is used to cause code like this to die instead of exhausting | |
628 | the C stack and triggering a segfault. Needless to say code like this is | |
629 | unusual, it is unlikely you will actually need to raise the setting. | |
630 | However it may be useful to set it to 0 for a limited time period to | |
631 | prevent BEGIN{} blocks from being executed during an C<eval EXPR>. | |
632 | ||
633 | Note that setting this to 1 would NOT affect code like this: | |
634 | ||
635 | BEGIN { $n += 1; BEGIN { $n += 2; BEGIN { $n += 4 } } } | |
636 | ||
637 | The reason is that BEGIN blocks are executed immediately after they are | |
638 | completed, thus the innermost will execute before the ones which contain | |
639 | it have even finished compiling, and the depth will not go above 1. In | |
640 | fact the above code is equivalent to | |
641 | ||
642 | BEGIN { $n+=4 } | |
643 | BEGIN { $n+=2 } | |
644 | BEGIN { $n+=1 } | |
645 | ||
646 | which makes it obvious why a ${^MAX_EVAL_BEGIN_DEPTH} of 1 would not | |
647 | block this code. | |
648 | ||
649 | Only C<BEGIN>'s executed inside of an C<eval> or C<require> (possibly via | |
650 | C<use>) are affected. | |
651 | ||
b0c22438 | 652 | =item $OSNAME |
a0d0e21e | 653 | |
b0c22438 | 654 | =item $^O |
655 | X<$^O> X<$OSNAME> | |
a0d0e21e | 656 | |
b0c22438 | 657 | The name of the operating system under which this copy of Perl was |
241a59d9 | 658 | built, as determined during the configuration process. For examples |
b0c22438 | 659 | see L<perlport/PLATFORMS>. |
a0d0e21e | 660 | |
241a59d9 | 661 | The value is identical to C<$Config{'osname'}>. See also L<Config> |
028611fa | 662 | and the B<-V> command-line switch documented in L<perlrun|perlrun/-V>. |
a0d0e21e | 663 | |
b0c22438 | 664 | In Windows platforms, C<$^O> is not very helpful: since it is always |
665 | C<MSWin32>, it doesn't tell the difference between | |
241a59d9 | 666 | 95/98/ME/NT/2000/XP/CE/.NET. Use C<Win32::GetOSName()> or |
b0c22438 | 667 | Win32::GetOSVersion() (see L<Win32> and L<perlport>) to distinguish |
668 | between the variants. | |
a0d0e21e | 669 | |
b0c22438 | 670 | This variable was added in Perl 5.003. |
a0d0e21e | 671 | |
1fa81471 DR |
672 | =item %SIG |
673 | X<%SIG> | |
a0d0e21e | 674 | |
241a59d9 | 675 | The hash C<%SIG> contains signal handlers for signals. For example: |
a0d0e21e | 676 | |
9548c15c | 677 | sub handler { # 1st argument is signal name |
0763c253 | 678 | my($sig) = @_; |
679 | print "Caught a SIG$sig--shutting down\n"; | |
680 | close(LOG); | |
681 | exit(0); | |
682 | } | |
a0d0e21e | 683 | |
9548c15c FC |
684 | $SIG{'INT'} = \&handler; |
685 | $SIG{'QUIT'} = \&handler; | |
686 | ... | |
687 | $SIG{'INT'} = 'DEFAULT'; # restore default action | |
688 | $SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT | |
a0d0e21e | 689 | |
1fa81471 | 690 | Using a value of C<'IGNORE'> usually has the effect of ignoring the |
241a59d9 | 691 | signal, except for the C<CHLD> signal. See L<perlipc> for more about |
40719f1d C |
692 | this special case. Using an empty string or C<undef> as the value has |
693 | the same effect as C<'DEFAULT'>. | |
a0d0e21e | 694 | |
1fa81471 | 695 | Here are some other examples: |
a0d0e21e | 696 | |
9548c15c | 697 | $SIG{"PIPE"} = "Plumber"; # assumes main::Plumber (not |
0763c253 | 698 | # recommended) |
9548c15c | 699 | $SIG{"PIPE"} = \&Plumber; # just fine; assume current |
0763c253 | 700 | # Plumber |
9548c15c FC |
701 | $SIG{"PIPE"} = *Plumber; # somewhat esoteric |
702 | $SIG{"PIPE"} = Plumber(); # oops, what did Plumber() | |
0763c253 | 703 | # return?? |
a0d0e21e | 704 | |
1fa81471 DR |
705 | Be sure not to use a bareword as the name of a signal handler, |
706 | lest you inadvertently call it. | |
a0d0e21e | 707 | |
40719f1d C |
708 | Using a string that doesn't correspond to any existing function or a |
709 | glob that doesn't contain a code slot is equivalent to C<'IGNORE'>, | |
710 | but a warning is emitted when the handler is being called (the warning | |
711 | is not emitted for the internal hooks described below). | |
712 | ||
1fa81471 | 713 | If your system has the C<sigaction()> function then signal handlers |
241a59d9 | 714 | are installed using it. This means you get reliable signal handling. |
a0d0e21e | 715 | |
60cf4914 | 716 | The default delivery policy of signals changed in Perl v5.8.0 from |
1fa81471 | 717 | immediate (also known as "unsafe") to deferred, also known as "safe |
241a59d9 | 718 | signals". See L<perlipc> for more information. |
a0d0e21e | 719 | |
241a59d9 | 720 | Certain internal hooks can be also set using the C<%SIG> hash. The |
1fa81471 | 721 | routine indicated by C<$SIG{__WARN__}> is called when a warning |
241a59d9 FC |
722 | message is about to be printed. The warning message is passed as the |
723 | first argument. The presence of a C<__WARN__> hook causes the | |
724 | ordinary printing of warnings to C<STDERR> to be suppressed. You can | |
1fa81471 DR |
725 | use this to save warnings in a variable, or turn warnings into fatal |
726 | errors, like this: | |
a0d0e21e | 727 | |
9548c15c FC |
728 | local $SIG{__WARN__} = sub { die $_[0] }; |
729 | eval $proggie; | |
a8f8344d | 730 | |
40719f1d C |
731 | As the C<'IGNORE'> hook is not supported by C<__WARN__>, its effect is |
732 | the same as using C<'DEFAULT'>. You can disable warnings using the | |
733 | empty subroutine: | |
f86702cc | 734 | |
9548c15c | 735 | local $SIG{__WARN__} = sub {}; |
55602bd2 | 736 | |
b0c22438 | 737 | The routine indicated by C<$SIG{__DIE__}> is called when a fatal |
241a59d9 FC |
738 | exception is about to be thrown. The error message is passed as the |
739 | first argument. When a C<__DIE__> hook routine returns, the exception | |
b0c22438 | 740 | processing continues as it would have in the absence of the hook, |
c94b42ea DM |
741 | unless the hook routine itself exits via a C<goto &sub>, a loop exit, |
742 | or a C<die()>. The C<__DIE__> handler is explicitly disabled during | |
743 | the call, so that you can die from a C<__DIE__> handler. Similarly | |
744 | for C<__WARN__>. | |
e5218da5 | 745 | |
4a29ab5e A |
746 | The C<$SIG{__DIE__}> hook is called even inside an C<eval()>. It was |
747 | never intended to happen this way, but an implementation glitch made | |
748 | this possible. This used to be deprecated, as it allowed strange action | |
749 | at a distance like rewriting a pending exception in C<$@>. Plans to | |
19151286 | 750 | rectify this have been scrapped, as users found that rewriting a |
4a29ab5e | 751 | pending exception is actually a useful feature, and not a bug. |
b0c22438 | 752 | |
bba48ac3 | 753 | The C<$SIG{__DIE__}> doesn't support C<'IGNORE'>; it has the same |
40719f1d C |
754 | effect as C<'DEFAULT'>. |
755 | ||
b0c22438 | 756 | C<__DIE__>/C<__WARN__> handlers are very special in one respect: they |
241a59d9 | 757 | may be called to report (probable) errors found by the parser. In such |
b0c22438 | 758 | a case the parser may be in inconsistent state, so any attempt to |
759 | evaluate Perl code from such a handler will probably result in a | |
241a59d9 | 760 | segfault. This means that warnings or errors that result from parsing |
b0c22438 | 761 | Perl should be used with extreme caution, like this: |
e5218da5 | 762 | |
9548c15c FC |
763 | require Carp if defined $^S; |
764 | Carp::confess("Something wrong") if defined &Carp::confess; | |
765 | die "Something wrong, but could not load Carp to give " | |
766 | . "backtrace...\n\t" | |
767 | . "To see backtrace try starting Perl with -MCarp switch"; | |
e5218da5 | 768 | |
b0c22438 | 769 | Here the first line will load C<Carp> I<unless> it is the parser who |
241a59d9 FC |
770 | called the handler. The second line will print backtrace and die if |
771 | C<Carp> was available. The third line will be executed only if C<Carp> was | |
b0c22438 | 772 | not available. |
0a378802 | 773 | |
0b9346e6 | 774 | Having to even think about the C<$^S> variable in your exception |
241a59d9 FC |
775 | handlers is simply wrong. C<$SIG{__DIE__}> as currently implemented |
776 | invites grievous and difficult to track down errors. Avoid it | |
0b9346e6 | 777 | and use an C<END{}> or CORE::GLOBAL::die override instead. |
778 | ||
b0c22438 | 779 | See L<perlfunc/die>, L<perlfunc/warn>, L<perlfunc/eval>, and |
780 | L<warnings> for additional information. | |
0a378802 | 781 | |
93f6f965 YO |
782 | =item %{^HOOK} |
783 | X<%{^HOOK}> | |
784 | ||
785 | This hash contains coderefs which are called when various perl keywords | |
786 | which are hard or impossible to wrap are called. The keys of this hash | |
787 | are named after the keyword that is being hooked, followed by two | |
788 | underbars and then a phase term; either "before" or "after". | |
789 | ||
790 | Perl will throw an error if you attempt modify a key which is not | |
791 | documented to exist, or if you attempt to store anything other than a | |
792 | code reference or undef in the hash. If you wish to use an object to | |
793 | implement a hook you can use currying to embed the object into an | |
794 | anonymous code reference. | |
795 | ||
796 | Currently there is only one keyword which can be hooked, C<require>, but | |
797 | it is expected that in future releases there will be additional keywords | |
798 | with hook support. | |
799 | ||
800 | =over 4 | |
801 | ||
802 | =item require__before | |
803 | ||
804 | The routine indicated by C<${^HOOK}{require__before}> is called by | |
805 | C<require> B<before> it checks C<%INC>, looks up C<@INC>, calls INC | |
806 | hooks, or compiles any code. It is called with a single argument, the | |
807 | filename for the item being required (package names are converted to | |
808 | paths). It may alter this filename to change what file is loaded. If | |
809 | the hook dies during execution then it will block the require from executing. | |
810 | ||
811 | In order to make it easy to perform an action with shared state both | |
812 | before and after the require keyword was executed the C<require__before> | |
813 | hook may return a "post-action" coderef which will in turn be executed when | |
814 | the C<require> completes. This coderef will be executed regardless as to | |
815 | whether the require completed succesfully or threw an exception. It will | |
816 | be called with the filename that was required. You can check %INC to | |
817 | determine if the require was successful. Any other return from the | |
818 | C<require__before> hook will be silently ignored. | |
819 | ||
820 | C<require__before> hooks are called in FIFO order, and if the hook | |
821 | returns a code reference those code references will be called in FILO | |
822 | order. In other words if A requires B requires C, then | |
823 | C<require__before> will be called first for A, then B and then C, and | |
824 | the post-action code reference will executed first for C, then B and | |
825 | then finally A. | |
826 | ||
827 | Well behaved code should ensure that when setting up a | |
828 | C<require__before> hook that any prior installed hook will be called, | |
829 | and that their return value, if a code reference, will be called as | |
830 | well. See L<perlfunc/require> for an example implementation. | |
831 | ||
832 | =item require__after | |
833 | ||
834 | The routine indicated by C<${^HOOK}{require__after}> is called by | |
835 | C<require> B<after> the require completes. It is called with a single | |
836 | argument, the filename for the item being required (package names are | |
837 | converted to paths). It is executed when the C<require> completes, | |
838 | either via exception or via completion of the require statement, and you | |
839 | can check C<%INC> to determine if the require was successful. | |
840 | ||
841 | The C<require__after> hook is called for each required file in FILO | |
842 | order. In other words if A requires B requires C, then C<require__after> | |
843 | will be called first for C, then B and then A. | |
844 | ||
845 | =back | |
846 | ||
b0c22438 | 847 | =item $BASETIME |
6ab308ee | 848 | |
b0c22438 | 849 | =item $^T |
850 | X<$^T> X<$BASETIME> | |
6ab308ee | 851 | |
b0c22438 | 852 | The time at which the program began running, in seconds since the |
241a59d9 | 853 | epoch (beginning of 1970). The values returned by the B<-M>, B<-A>, |
b0c22438 | 854 | and B<-C> filetests are based on this value. |
a0d0e21e | 855 | |
b0c22438 | 856 | =item $PERL_VERSION |
a0d0e21e | 857 | |
b0c22438 | 858 | =item $^V |
859 | X<$^V> X<$PERL_VERSION> | |
a0d0e21e | 860 | |
e34263aa KW |
861 | =for comment |
862 | These are documented in the generated file lib/Config.pod. This looks | |
863 | like as good a place as any to give notice that they are documented. | |
864 | ||
b0c22438 | 865 | The revision, version, and subversion of the Perl interpreter, |
eb82332c | 866 | represented as a L<version> object. |
748a9306 | 867 | |
60cf4914 BF |
868 | This variable first appeared in perl v5.6.0; earlier versions of perl |
869 | will see an undefined value. Before perl v5.10.0 C<$^V> was represented | |
f20d3573 | 870 | as a v-string rather than a L<version> object. |
55602bd2 | 871 | |
b0c22438 | 872 | C<$^V> can be used to determine whether the Perl interpreter executing |
241a59d9 | 873 | a script is in the right range of versions. For example: |
a0d0e21e | 874 | |
9548c15c | 875 | warn "Hashes not randomized!\n" if !$^V or $^V lt v5.8.1 |
a0d0e21e | 876 | |
f20d3573 DG |
877 | While version objects overload stringification, to portably convert |
878 | C<$^V> into its string representation, use C<sprintf()>'s C<"%vd"> | |
879 | conversion, which works for both v-strings or version objects: | |
a0d0e21e | 880 | |
9548c15c | 881 | printf "version is v%vd\n", $^V; # Perl's version |
a0d0e21e | 882 | |
b0c22438 | 883 | See the documentation of C<use VERSION> and C<require VERSION> |
884 | for a convenient way to fail if the running Perl interpreter is too old. | |
4d76a344 | 885 | |
6a29646e | 886 | See also C<L</$]>> for a decimal representation of the Perl version. |
a0d0e21e | 887 | |
f20d3573 DG |
888 | The main advantage of C<$^V> over C<$]> is that, for Perl v5.10.0 or |
889 | later, it overloads operators, allowing easy comparison against other | |
890 | version representations (e.g. decimal, literal v-string, "v1.2.3", or | |
891 | objects). The disadvantage is that prior to v5.10.0, it was only a | |
6a29646e KW |
892 | literal v-string, which can't be easily printed or compared, whereas |
893 | the behavior of C<$]> is unchanged on all versions of Perl. | |
a0d0e21e | 894 | |
eb82332c | 895 | Mnemonic: use ^V for a version object. |
a0d0e21e | 896 | |
b0c22438 | 897 | =item $EXECUTABLE_NAME |
a0d0e21e | 898 | |
b0c22438 | 899 | =item $^X |
900 | X<$^X> X<$EXECUTABLE_NAME> | |
a0d0e21e | 901 | |
b0c22438 | 902 | The name used to execute the current copy of Perl, from C's |
903 | C<argv[0]> or (where supported) F</proc/self/exe>. | |
a043a685 | 904 | |
b0c22438 | 905 | Depending on the host operating system, the value of C<$^X> may be |
906 | a relative or absolute pathname of the perl program file, or may | |
907 | be the string used to invoke perl but not the pathname of the | |
241a59d9 | 908 | perl program file. Also, most operating systems permit invoking |
b0c22438 | 909 | programs that are not in the PATH environment variable, so there |
241a59d9 | 910 | is no guarantee that the value of C<$^X> is in PATH. For VMS, the |
b0c22438 | 911 | value may or may not include a version number. |
a0d0e21e | 912 | |
b0c22438 | 913 | You usually can use the value of C<$^X> to re-invoke an independent |
914 | copy of the same perl that is currently running, e.g., | |
a0d0e21e | 915 | |
9548c15c | 916 | @first_run = `$^X -le "print int rand 100 for 1..100"`; |
a0d0e21e | 917 | |
b0c22438 | 918 | But recall that not all operating systems support forking or |
919 | capturing of the output of commands, so this complex statement | |
920 | may not be portable. | |
a0d0e21e | 921 | |
b0c22438 | 922 | It is not safe to use the value of C<$^X> as a path name of a file, |
923 | as some operating systems that have a mandatory suffix on | |
924 | executable files do not require use of the suffix when invoking | |
241a59d9 | 925 | a command. To convert the value of C<$^X> to a path name, use the |
b0c22438 | 926 | following statements: |
8cc95fdb | 927 | |
9548c15c FC |
928 | # Build up a set of file names (not command names). |
929 | use Config; | |
930 | my $this_perl = $^X; | |
931 | if ($^O ne 'VMS') { | |
0763c253 | 932 | $this_perl .= $Config{_exe} |
933 | unless $this_perl =~ m/$Config{_exe}$/i; | |
934 | } | |
8cc95fdb | 935 | |
b0c22438 | 936 | Because many operating systems permit anyone with read access to |
937 | the Perl program file to make a copy of it, patch the copy, and | |
938 | then execute the copy, the security-conscious Perl programmer | |
939 | should take care to invoke the installed copy of perl, not the | |
241a59d9 | 940 | copy referenced by C<$^X>. The following statements accomplish |
b0c22438 | 941 | this goal, and produce a pathname that can be invoked as a |
942 | command or referenced as a file. | |
a043a685 | 943 | |
9548c15c FC |
944 | use Config; |
945 | my $secure_perl_path = $Config{perlpath}; | |
946 | if ($^O ne 'VMS') { | |
0763c253 | 947 | $secure_perl_path .= $Config{_exe} |
948 | unless $secure_perl_path =~ m/$Config{_exe}$/i; | |
949 | } | |
a0d0e21e | 950 | |
b0c22438 | 951 | =back |
a0d0e21e | 952 | |
b0c22438 | 953 | =head2 Variables related to regular expressions |
954 | ||
955 | Most of the special variables related to regular expressions are side | |
2a51adc4 YO |
956 | effects. Perl sets these variables when it has completed a match |
957 | successfully, so you should check the match result before using them. | |
958 | For instance: | |
b0c22438 | 959 | |
9548c15c | 960 | if( /P(A)TT(ER)N/ ) { |
0763c253 | 961 | print "I found $1 and $2\n"; |
962 | } | |
b0c22438 | 963 | |
2a51adc4 YO |
964 | These variables are read-only and behave similarly to a dynamically |
965 | scoped variable, with only a few exceptions which are explicitly | |
966 | documented as behaving otherwise. See the following section for more | |
967 | details. | |
b0c22438 | 968 | |
2a51adc4 | 969 | =head3 Scoping Rules of Regex Variables |
6abb319d | 970 | X<Scoping Rules of Regex Variables> |
b0c22438 | 971 | |
2a51adc4 YO |
972 | Regular expression variables allow the programmer to access the state of |
973 | the most recent I<successful> regex match in the current dynamic scope. | |
0b9346e6 | 974 | |
2a51adc4 YO |
975 | The variables themselves are global and unscoped, but the data they |
976 | access is scoped similarly to dynamically scoped variables, in that | |
977 | every successful match behaves as though it localizes a global state | |
978 | object to the current block or file scope. | |
979 | (See L<perlsyn/"Compound Statements"> for more details on dynamic | |
980 | scoping and the C<local> keyword.) | |
0b9346e6 | 981 | |
2a51adc4 YO |
982 | A I<successful match> includes any successful match performed by the |
983 | search and replace operator C<s///> as well as those performed by the | |
984 | C<m//> operator. | |
0b9346e6 | 985 | |
2a51adc4 | 986 | Consider the following code: |
0b9346e6 | 987 | |
2a51adc4 YO |
988 | my @state; |
989 | sub matchit { | |
990 | push @state, $1; # pushes "baz" | |
991 | my $str = shift; | |
992 | $str =~ /(zat)/; # matches "zat" | |
993 | push @state, $1; # pushes "zat" | |
994 | } | |
0b9346e6 | 995 | |
2a51adc4 YO |
996 | { |
997 | $str = "foo bar baz blorp zat"; | |
998 | $str =~ /(foo)/; # matches "foo" | |
999 | push @state, $1; # pushes "foo" | |
1000 | { | |
1001 | $str =~ /(pizza)/; # does NOT match | |
1002 | push @state, $1; # pushes "foo" | |
1003 | $str =~ /(bar)/; # matches "bar" | |
1004 | push @state, $1; # pushes "bar" | |
1005 | $str =~ /(baz)/; # matches "baz" | |
1006 | matchit($str); # see above | |
1007 | push @state, $1; # pushes "baz" | |
1008 | } | |
1009 | $str =~ s/noodles/rice/; # does NOT match | |
1010 | push @state, $1; # pushes "foo" | |
1011 | $str =~ s/(blorp)/zwoop/; # matches "blorp" | |
1012 | push @state, $1; # pushes "blorp" | |
9548c15c | 1013 | } |
2a51adc4 YO |
1014 | # the following prints "foo, foo, bar, baz, zat, baz, foo, blorp" |
1015 | print join ",", @state; | |
b0c22438 | 1016 | |
2a51adc4 YO |
1017 | Notice that each successful match in the exact same scope overrides the |
1018 | match context of the previous successful match, but that unsuccessful | |
1019 | matches do not. Also note that in an inner nested scope the previous | |
1020 | state from an outer dynamic scope persists until it has been overriden | |
1021 | by another successful match, but that when the inner nested scope exits | |
1022 | whatever match context was in effect before the inner successful match | |
1023 | is restored when the scope concludes. | |
b0c22438 | 1024 | |
2a51adc4 YO |
1025 | It is a known issue that C<goto LABEL> may interact poorly with the |
1026 | dynamically scoped match context. This may not be fixable, and is | |
1027 | considered to be one of many good reasons to avoid C<goto LABEL>. | |
a0d0e21e | 1028 | |
40445027 | 1029 | =head3 Performance issues |
0b9346e6 | 1030 | |
40445027 DM |
1031 | Traditionally in Perl, any use of any of the three variables C<$`>, C<$&> |
1032 | or C<$'> (or their C<use English> equivalents) anywhere in the code, caused | |
1033 | all subsequent successful pattern matches to make a copy of the matched | |
1034 | string, in case the code might subsequently access one of those variables. | |
1035 | This imposed a considerable performance penalty across the whole program, | |
1036 | so generally the use of these variables has been discouraged. | |
0b9346e6 | 1037 | |
40445027 DM |
1038 | In Perl 5.6.0 the C<@-> and C<@+> dynamic arrays were introduced that |
1039 | supply the indices of successful matches. So you could for example do | |
1040 | this: | |
1041 | ||
1042 | $str =~ /pattern/; | |
1043 | ||
33f0d962 | 1044 | print $`, $&, $'; # bad: performance hit |
40445027 | 1045 | |
33f0d962 | 1046 | print # good: no performance hit |
0763c253 | 1047 | substr($str, 0, $-[0]), |
1048 | substr($str, $-[0], $+[0]-$-[0]), | |
1049 | substr($str, $+[0]); | |
40445027 DM |
1050 | |
1051 | In Perl 5.10.0 the C</p> match operator flag and the C<${^PREMATCH}>, | |
1052 | C<${^MATCH}>, and C<${^POSTMATCH}> variables were introduced, that allowed | |
1053 | you to suffer the penalties only on patterns marked with C</p>. | |
1054 | ||
1055 | In Perl 5.18.0 onwards, perl started noting the presence of each of the | |
1056 | three variables separately, and only copied that part of the string | |
1057 | required; so in | |
1058 | ||
1059 | $`; $&; "abcdefgh" =~ /d/ | |
1060 | ||
1061 | perl would only copy the "abcd" part of the string. That could make a big | |
1062 | difference in something like | |
1063 | ||
1064 | $str = 'x' x 1_000_000; | |
1065 | $&; # whoops | |
1066 | $str =~ /x/g # one char copied a million times, not a million chars | |
1067 | ||
1068 | In Perl 5.20.0 a new copy-on-write system was enabled by default, which | |
60c52570 YO |
1069 | finally fixes most of the performance issues with these three variables, and |
1070 | makes them safe to use anywhere. | |
40445027 DM |
1071 | |
1072 | The C<Devel::NYTProf> and C<Devel::FindAmpersand> modules can help you | |
1073 | find uses of these problematic match variables in your code. | |
13b0f67d | 1074 | |
b0c22438 | 1075 | =over 8 |
a0d0e21e | 1076 | |
b0c22438 | 1077 | =item $<I<digits>> ($1, $2, ...) |
27deb0cf | 1078 | X<$1> X<$2> X<$3> X<$I<digits>> |
8cc95fdb | 1079 | |
b0c22438 | 1080 | Contains the subpattern from the corresponding set of capturing |
2a51adc4 YO |
1081 | parentheses from the last successful pattern match in the current |
1082 | dynamic scope. (See L</Scoping Rules of Regex Variables>.) | |
8cc95fdb | 1083 | |
27deb0cf YO |
1084 | Note there is a distinction between a capture buffer which matches |
1085 | the empty string a capture buffer which is optional. Eg, C<(x?)> and | |
1086 | C<(x)?> The latter may be undef, the former not. | |
1087 | ||
2a51adc4 | 1088 | These variables are read-only. |
a043a685 | 1089 | |
b0c22438 | 1090 | Mnemonic: like \digits. |
a0d0e21e | 1091 | |
27deb0cf YO |
1092 | =item @{^CAPTURE} |
1093 | X<@{^CAPTURE}> X<@^CAPTURE> | |
1094 | ||
95013431 YO |
1095 | An array which exposes the contents of the capture buffers, if any, of |
1096 | the last successful pattern match, not counting patterns matched | |
27deb0cf YO |
1097 | in nested blocks that have been exited already. |
1098 | ||
0ee1ec43 SB |
1099 | Note that the 0 index of C<@{^CAPTURE}> is equivalent to C<$1>, the 1 index |
1100 | is equivalent to C<$2>, etc. | |
27deb0cf YO |
1101 | |
1102 | if ("foal"=~/(.)(.)(.)(.)/) { | |
1103 | print join "-", @{^CAPTURE}; | |
1104 | } | |
1105 | ||
1106 | should output "f-o-a-l". | |
1107 | ||
135226fa KW |
1108 | See also L<<< /$<I<digits>> ($1, $2, ...) >>>, L</%{^CAPTURE}> and |
1109 | L</%{^CAPTURE_ALL}>. | |
27deb0cf | 1110 | |
95013431 | 1111 | Note that unlike most other regex magic variables there is no single |
2cfe7629 YO |
1112 | letter equivalent to C<@{^CAPTURE}>. Also be aware that when |
1113 | interpolating subscripts of this array you B<must> use the demarcated | |
1114 | variable form, for instance | |
1115 | ||
1116 | print "${^CAPTURE[0]}" | |
1117 | ||
1118 | see L<perldata/"Demarcated variable names using braces"> for more | |
1119 | information on this form and its uses. | |
95013431 | 1120 | |
27deb0cf YO |
1121 | This variable was added in 5.25.7 |
1122 | ||
0ee1ec43 SB |
1123 | If you need access to this functionality in older Perls you can use this |
1124 | function immediately after your regexp. | |
1125 | ||
909ecddf KE |
1126 | sub get_captures { |
1127 | no strict 'refs'; | |
0ee1ec43 | 1128 | |
909ecddf KE |
1129 | my $last_idx = scalar(@-) - 1; |
1130 | my @arr = 1 .. $last_idx; | |
1131 | my @ret = map { $$_; } @arr; | |
0ee1ec43 | 1132 | |
909ecddf KE |
1133 | return @ret; |
1134 | } | |
0ee1ec43 | 1135 | |
b0c22438 | 1136 | =item $MATCH |
a0d0e21e | 1137 | |
b0c22438 | 1138 | =item $& |
1139 | X<$&> X<$MATCH> | |
a0d0e21e | 1140 | |
2a51adc4 YO |
1141 | The string matched by the last successful pattern match. |
1142 | (See L</Scoping Rules of Regex Variables>.) | |
a0d0e21e | 1143 | |
40445027 DM |
1144 | See L</Performance issues> above for the serious performance implications |
1145 | of using this variable (even once) in your code. | |
80bca1b4 | 1146 | |
2a51adc4 | 1147 | This variable is read-only, and its value is dynamically scoped. |
f9cbb277 | 1148 | |
b0c22438 | 1149 | Mnemonic: like C<&> in some editors. |
0b9346e6 | 1150 | |
b0c22438 | 1151 | =item ${^MATCH} |
1152 | X<${^MATCH}> | |
a0d0e21e | 1153 | |
60c52570 YO |
1154 | It is only guaranteed to return a defined value when the pattern was |
1155 | compiled or executed with the C</p> modifier. | |
40445027 | 1156 | |
60c52570 YO |
1157 | This is similar to C<$&> (C<$MATCH>) except that to use it you must |
1158 | use the C</p> modifier when executing the pattern, and it does not incur | |
6380554d | 1159 | the performance penalty associated with that variable. |
40445027 | 1160 | |
60c52570 | 1161 | See L</Performance issues> above. |
80bca1b4 | 1162 | |
60cf4914 | 1163 | This variable was added in Perl v5.10.0. |
4bc88a62 | 1164 | |
2a51adc4 | 1165 | This variable is read-only, and its value is dynamically scoped. |
e2975953 | 1166 | |
b0c22438 | 1167 | =item $PREMATCH |
52c447a8 | 1168 | |
b0c22438 | 1169 | =item $` |
60c52570 | 1170 | X<$`> X<$PREMATCH> |
7636ea95 | 1171 | |
b0c22438 | 1172 | The string preceding whatever was matched by the last successful |
2a51adc4 | 1173 | pattern match. (See L</Scoping Rules of Regex Variables>). |
a0d0e21e | 1174 | |
40445027 DM |
1175 | See L</Performance issues> above for the serious performance implications |
1176 | of using this variable (even once) in your code. | |
a0d0e21e | 1177 | |
2a51adc4 | 1178 | This variable is read-only, and its value is dynamically scoped. |
a0d0e21e | 1179 | |
b0c22438 | 1180 | Mnemonic: C<`> often precedes a quoted string. |
f83ed198 | 1181 | |
b0c22438 | 1182 | =item ${^PREMATCH} |
60c52570 YO |
1183 | X<${^PREMATCH}> |
1184 | ||
1185 | It is only guaranteed to return a defined value when the pattern was | |
1186 | executed with the C</p> modifier. | |
a0d0e21e | 1187 | |
60c52570 YO |
1188 | This is similar to C<$`> ($PREMATCH) except that to use it you must |
1189 | use the C</p> modifier when executing the pattern, and it does not incur | |
6380554d | 1190 | the performance penalty associated with that variable. |
40445027 DM |
1191 | |
1192 | See L</Performance issues> above. | |
1193 | ||
a0d0e21e | 1194 | |
4a70680a | 1195 | This variable was added in Perl v5.10.0. |
a0d0e21e | 1196 | |
2a51adc4 | 1197 | This variable is read-only, and its value is dynamically scoped. |
a0d0e21e | 1198 | |
b0c22438 | 1199 | =item $POSTMATCH |
16070b82 | 1200 | |
b0c22438 | 1201 | =item $' |
60c52570 | 1202 | X<$'> X<$POSTMATCH> X<@-> |
305aace0 | 1203 | |
b0c22438 | 1204 | The string following whatever was matched by the last successful |
2a51adc4 | 1205 | pattern match. (See L</Scoping Rules of Regex Variables>). Example: |
305aace0 | 1206 | |
9548c15c FC |
1207 | local $_ = 'abcdefghi'; |
1208 | /def/; | |
0763c253 | 1209 | print "$`:$&:$'\n"; # prints abc:def:ghi |
305aace0 | 1210 | |
40445027 DM |
1211 | See L</Performance issues> above for the serious performance implications |
1212 | of using this variable (even once) in your code. | |
a0d0e21e | 1213 | |
2a51adc4 | 1214 | This variable is read-only, and its value is dynamically scoped. |
b0c22438 | 1215 | |
1216 | Mnemonic: C<'> often follows a quoted string. | |
1217 | ||
1218 | =item ${^POSTMATCH} | |
60c52570 | 1219 | X<${^POSTMATCH}> |
b0c22438 | 1220 | |
60c52570 YO |
1221 | It is only guaranteed to return a defined value when the pattern was |
1222 | compiled or executed with the C</p> modifier. | |
40445027 | 1223 | |
60c52570 YO |
1224 | This is similar to C<$'> (C<$POSTMATCH>) except that to use it you must |
1225 | use the C</p> modifier when executing the pattern, and it does not incur | |
6380554d | 1226 | the performance penalty associated with that variable. |
40445027 | 1227 | |
60c52570 | 1228 | See L</Performance issues> above. |
b0c22438 | 1229 | |
60cf4914 | 1230 | This variable was added in Perl v5.10.0. |
b0c22438 | 1231 | |
2a51adc4 | 1232 | This variable is read-only, and its value is dynamically scoped. |
b0c22438 | 1233 | |
1234 | =item $LAST_PAREN_MATCH | |
1235 | ||
1236 | =item $+ | |
1237 | X<$+> X<$LAST_PAREN_MATCH> | |
1238 | ||
0b9dad94 | 1239 | The text matched by the highest used capture group of the last |
2a51adc4 YO |
1240 | successful search pattern. (See L</Scoping Rules of Regex Variables>). |
1241 | It is logically equivalent to the highest | |
0b9dad94 DM |
1242 | numbered capture variable (C<$1>, C<$2>, ...) which has a defined value. |
1243 | ||
b0c22438 | 1244 | This is useful if you don't know which one of a set of alternative patterns |
241a59d9 | 1245 | matched. For example: |
b0c22438 | 1246 | |
9548c15c | 1247 | /Version: (.*)|Revision: (.*)/ && ($rev = $+); |
b0c22438 | 1248 | |
2a51adc4 | 1249 | This variable is read-only, and its value is dynamically scoped. |
b0c22438 | 1250 | |
1251 | Mnemonic: be positive and forward looking. | |
1252 | ||
1253 | =item $LAST_SUBMATCH_RESULT | |
1254 | ||
1255 | =item $^N | |
5b442a2a | 1256 | X<$^N> X<$LAST_SUBMATCH_RESULT> |
b0c22438 | 1257 | |
1258 | The text matched by the used group most-recently closed (i.e. the group | |
2a51adc4 YO |
1259 | with the rightmost closing parenthesis) of the last successful match. |
1260 | (See L</Scoping Rules of Regex Variables>). | |
1261 | ||
1262 | ||
1263 | This is subtly different from C<$+>. For example in | |
0b9dad94 DM |
1264 | |
1265 | "ab" =~ /^((.)(.))$/ | |
1266 | ||
1267 | we have | |
1268 | ||
1269 | $1,$^N have the value "ab" | |
1270 | $2 has the value "a" | |
1271 | $3,$+ have the value "b" | |
b0c22438 | 1272 | |
1273 | This is primarily used inside C<(?{...})> blocks for examining text | |
241a59d9 | 1274 | recently matched. For example, to effectively capture text to a variable |
b0c22438 | 1275 | (in addition to C<$1>, C<$2>, etc.), replace C<(...)> with |
1276 | ||
9548c15c | 1277 | (?:(...)(?{ $var = $^N })) |
b0c22438 | 1278 | |
1279 | By setting and then using C<$var> in this way relieves you from having to | |
1280 | worry about exactly which numbered set of parentheses they are. | |
1281 | ||
2a51adc4 YO |
1282 | This variable is read-only, and its value is dynamically scoped. |
1283 | ||
60cf4914 | 1284 | This variable was added in Perl v5.8.0. |
b0c22438 | 1285 | |
1286 | Mnemonic: the (possibly) Nested parenthesis that most recently closed. | |
1287 | ||
1288 | =item @LAST_MATCH_END | |
1289 | ||
1290 | =item @+ | |
1291 | X<@+> X<@LAST_MATCH_END> | |
1292 | ||
1293 | This array holds the offsets of the ends of the last successful | |
2a51adc4 YO |
1294 | match and any matching capture buffers that the pattern contains. |
1295 | (See L</Scoping Rules of Regex Variables>) | |
1296 | ||
1297 | The number of elements it contains will be one more than the number | |
1298 | of capture buffers in the pattern, regardless of which capture buffers | |
1299 | actually matched. You can use this to determine how many capture | |
1300 | buffers there are in the pattern. (As opposed to C<@-> which may | |
1301 | have fewer elements.) | |
1302 | ||
1303 | C<$+[0]> is the offset into the string of the end of the entire match. | |
1304 | This is the same value as what the C<pos> function returns when called | |
1305 | on the variable that was matched against. The I<n>th element of this | |
1306 | array holds the offset of the I<n>th submatch, so C<$+[1]> is the offset | |
1307 | past where C<$1> ends, C<$+[2]> the offset past where C<$2> ends, and so | |
1308 | on. You can use C<$#+> to determine how many subgroups were in the last | |
1309 | successful match. See the examples given for the C<@-> variable. | |
1310 | ||
1311 | This variable is read-only, and its value is dynamically scoped. | |
b0c22438 | 1312 | |
60cf4914 | 1313 | This variable was added in Perl v5.6.0. |
b0c22438 | 1314 | |
27deb0cf YO |
1315 | =item %{^CAPTURE} |
1316 | ||
b0c22438 | 1317 | =item %LAST_PAREN_MATCH |
1318 | ||
1319 | =item %+ | |
27deb0cf | 1320 | X<%+> X<%LAST_PAREN_MATCH> X<%{^CAPTURE}> |
b0c22438 | 1321 | |
1322 | Similar to C<@+>, the C<%+> hash allows access to the named capture | |
1323 | buffers, should they exist, in the last successful match in the | |
2a51adc4 | 1324 | currently active dynamic scope. (See L</Scoping Rules of Regex Variables>). |
b0c22438 | 1325 | |
1326 | For example, C<$+{foo}> is equivalent to C<$1> after the following match: | |
1327 | ||
9548c15c | 1328 | 'foo' =~ /(?<foo>foo)/; |
b0c22438 | 1329 | |
1330 | The keys of the C<%+> hash list only the names of buffers that have | |
1331 | captured (and that are thus associated to defined values). | |
1332 | ||
33727e0f LM |
1333 | If multiple distinct capture groups have the same name, then |
1334 | C<$+{NAME}> will refer to the leftmost defined group in the match. | |
1335 | ||
b0c22438 | 1336 | The underlying behaviour of C<%+> is provided by the |
1337 | L<Tie::Hash::NamedCapture> module. | |
1338 | ||
1339 | B<Note:> C<%-> and C<%+> are tied views into a common internal hash | |
241a59d9 | 1340 | associated with the last successful regular expression. Therefore mixing |
b0c22438 | 1341 | iterative access to them via C<each> may have unpredictable results. |
1342 | Likewise, if the last successful match changes, then the results may be | |
1343 | surprising. | |
1344 | ||
27deb0cf YO |
1345 | This variable was added in Perl v5.10.0. The C<%{^CAPTURE}> alias was |
1346 | added in 5.25.7. | |
a0d0e21e | 1347 | |
2a51adc4 | 1348 | This variable is read-only, and its value is dynamically scoped. |
b0c22438 | 1349 | |
1350 | =item @LAST_MATCH_START | |
1351 | ||
1352 | =item @- | |
1353 | X<@-> X<@LAST_MATCH_START> | |
1354 | ||
2a51adc4 YO |
1355 | This array holds the offsets of the beginnings of the last successful |
1356 | match and any capture buffers it contains. | |
1357 | (See L</Scoping Rules of Regex Variables>). | |
1358 | ||
1359 | The number of elements it contains will be one more than the number of | |
1360 | the highest capture buffers (also called a subgroup) that actually | |
5a3cea7d | 1361 | matched something. (As opposed to C<@+> which may have more elements.) |
2a51adc4 | 1362 | |
b0c22438 | 1363 | C<$-[0]> is the offset of the start of the last successful match. |
8dbb2d95 | 1364 | C<$-[I<n>]> is the offset of the start of the substring matched by |
b0c22438 | 1365 | I<n>-th subpattern, or undef if the subpattern did not match. |
1366 | ||
2a51adc4 YO |
1367 | Thus, after a match against C<$_>, C<$&> coincides with |
1368 | C<substr $_, $-[0], $+[0] - $-[0]>. Similarly, C<$I<n>> coincides | |
1369 | with C<substr $_, $-[n], $+[n] - $-[n]> if C<$-[n]> is defined, and | |
1370 | C<$+> coincides with C<substr $_, $-[$#-], $+[$#-] - $-[$#-]>. | |
1371 | One can use C<$#-> to find the last matched subgroup in the last | |
1372 | successful match. Contrast with C<$#+>, the number of subgroups | |
1373 | in the regular expression. | |
b0c22438 | 1374 | |
b0c22438 | 1375 | C<$-[0]> is the offset into the string of the beginning of the |
241a59d9 | 1376 | entire match. The I<n>th element of this array holds the offset |
b0c22438 | 1377 | of the I<n>th submatch, so C<$-[1]> is the offset where C<$1> |
1378 | begins, C<$-[2]> the offset where C<$2> begins, and so on. | |
1379 | ||
1380 | After a match against some variable C<$var>: | |
1381 | ||
1382 | =over 5 | |
1383 | ||
1384 | =item C<$`> is the same as C<substr($var, 0, $-[0])> | |
1385 | ||
1386 | =item C<$&> is the same as C<substr($var, $-[0], $+[0] - $-[0])> | |
1387 | ||
1388 | =item C<$'> is the same as C<substr($var, $+[0])> | |
1389 | ||
1390 | =item C<$1> is the same as C<substr($var, $-[1], $+[1] - $-[1])> | |
1391 | ||
1392 | =item C<$2> is the same as C<substr($var, $-[2], $+[2] - $-[2])> | |
1393 | ||
1394 | =item C<$3> is the same as C<substr($var, $-[3], $+[3] - $-[3])> | |
1395 | ||
1396 | =back | |
1397 | ||
2a51adc4 YO |
1398 | This variable is read-only, and its value is dynamically scoped. |
1399 | ||
60cf4914 | 1400 | This variable was added in Perl v5.6.0. |
b0c22438 | 1401 | |
27deb0cf YO |
1402 | =item %{^CAPTURE_ALL} |
1403 | X<%{^CAPTURE_ALL}> | |
1404 | ||
b0c22438 | 1405 | =item %- |
2e67aae7 | 1406 | X<%-> |
b0c22438 | 1407 | |
2a51adc4 YO |
1408 | Similar to C<%+>, this variable allows access to the named capture |
1409 | groups in the last successful match in the currently active dynamic | |
1410 | scope. (See L</Scoping Rules of Regex Variables>). To each capture group | |
1411 | name found in the regular expression, it associates a reference to an | |
1412 | array containing the list of values captured by all buffers with that | |
1413 | name (should there be several of them), in the order where they appear. | |
b0c22438 | 1414 | |
1415 | Here's an example: | |
1416 | ||
1417 | if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) { | |
1418 | foreach my $bufname (sort keys %-) { | |
1419 | my $ary = $-{$bufname}; | |
1420 | foreach my $idx (0..$#$ary) { | |
1421 | print "\$-{$bufname}[$idx] : ", | |
9548c15c FC |
1422 | (defined($ary->[$idx]) |
1423 | ? "'$ary->[$idx]'" | |
1424 | : "undef"), | |
b0c22438 | 1425 | "\n"; |
1426 | } | |
1427 | } | |
1428 | } | |
1429 | ||
1430 | would print out: | |
1431 | ||
9548c15c FC |
1432 | $-{A}[0] : '1' |
1433 | $-{A}[1] : '3' | |
1434 | $-{B}[0] : '2' | |
1435 | $-{B}[1] : '4' | |
b0c22438 | 1436 | |
1437 | The keys of the C<%-> hash correspond to all buffer names found in | |
1438 | the regular expression. | |
1439 | ||
1440 | The behaviour of C<%-> is implemented via the | |
1441 | L<Tie::Hash::NamedCapture> module. | |
1442 | ||
1443 | B<Note:> C<%-> and C<%+> are tied views into a common internal hash | |
241a59d9 | 1444 | associated with the last successful regular expression. Therefore mixing |
b0c22438 | 1445 | iterative access to them via C<each> may have unpredictable results. |
1446 | Likewise, if the last successful match changes, then the results may be | |
2a51adc4 | 1447 | surprising. See L</Scoping Rules of Regex Variables>. |
b0c22438 | 1448 | |
27deb0cf YO |
1449 | This variable was added in Perl v5.10.0. The C<%{^CAPTURE_ALL}> alias was |
1450 | added in 5.25.7. | |
b0c22438 | 1451 | |
2a51adc4 | 1452 | This variable is read-only, and its value is dynamically scoped. |
b0c22438 | 1453 | |
d80a076d YO |
1454 | =item ${^LAST_SUCCESSFUL_PATTERN} |
1455 | ||
1456 | The last successful pattern that matched in the current scope. The empty | |
1457 | pattern defaults to matching to this. For instance: | |
1458 | ||
1459 | if (m/foo/ || m/bar/) { | |
1460 | s//BLAH/; | |
1461 | } | |
1462 | ||
1463 | and | |
1464 | ||
1465 | if (m/foo/ || m/bar/) { | |
1466 | s/${^LAST_SUCCESSFUL_PATTERN}/BLAH/; | |
1467 | } | |
1468 | ||
655778a2 | 1469 | are equivalent. |
d80a076d YO |
1470 | |
1471 | You can use this to debug which pattern matched last, or to match with it again. | |
1472 | ||
1473 | Added in Perl 5.37.10. | |
1474 | ||
b0c22438 | 1475 | =item $LAST_REGEXP_CODE_RESULT |
1476 | ||
1477 | =item $^R | |
1478 | X<$^R> X<$LAST_REGEXP_CODE_RESULT> | |
1479 | ||
1480 | The result of evaluation of the last successful C<(?{ code })> | |
2a51adc4 YO |
1481 | regular expression assertion (see L<perlre>). |
1482 | ||
1483 | This variable may be written to, and its value is scoped normally, | |
1484 | unlike most other regex variables. | |
b0c22438 | 1485 | |
1486 | This variable was added in Perl 5.005. | |
a0d0e21e | 1487 | |
6ef7fe53 KW |
1488 | =item ${^RE_COMPILE_RECURSION_LIMIT} |
1489 | X<${^RE_COMPILE_RECURSION_LIMIT}> | |
1490 | ||
1491 | The current value giving the maximum number of open but unclosed | |
1492 | parenthetical groups there may be at any point during a regular | |
1493 | expression compilation. The default is currently 1000 nested groups. | |
1494 | You may adjust it depending on your needs and the amount of memory | |
1495 | available. | |
1496 | ||
1497 | This variable was added in Perl v5.30.0. | |
1498 | ||
a3621e74 | 1499 | =item ${^RE_DEBUG_FLAGS} |
ca1b95ae | 1500 | X<${^RE_DEBUG_FLAGS}> |
a3621e74 | 1501 | |
241a59d9 FC |
1502 | The current value of the regex debugging flags. Set to 0 for no debug output |
1503 | even when the C<re 'debug'> module is loaded. See L<re> for details. | |
b0c22438 | 1504 | |
60cf4914 | 1505 | This variable was added in Perl v5.10.0. |
a3621e74 | 1506 | |
0111c4fd | 1507 | =item ${^RE_TRIE_MAXBUF} |
ca1b95ae | 1508 | X<${^RE_TRIE_MAXBUF}> |
a3621e74 YO |
1509 | |
1510 | Controls how certain regex optimisations are applied and how much memory they | |
241a59d9 FC |
1511 | utilize. This value by default is 65536 which corresponds to a 512kB |
1512 | temporary cache. Set this to a higher value to trade | |
1513 | memory for speed when matching large alternations. Set | |
1514 | it to a lower value if you want the optimisations to | |
a3621e74 YO |
1515 | be as conservative of memory as possible but still occur, and set it to a |
1516 | negative value to prevent the optimisation and conserve the most memory. | |
1517 | Under normal situations this variable should be of no interest to you. | |
1518 | ||
60cf4914 | 1519 | This variable was added in Perl v5.10.0. |
a0d0e21e | 1520 | |
b0c22438 | 1521 | =back |
a0d0e21e | 1522 | |
b0c22438 | 1523 | =head2 Variables related to filehandles |
a0d0e21e | 1524 | |
b0c22438 | 1525 | Variables that depend on the currently selected filehandle may be set |
1526 | by calling an appropriate object method on the C<IO::Handle> object, | |
1527 | although this is less efficient than using the regular built-in | |
241a59d9 | 1528 | variables. (Summary lines below for this contain the word HANDLE.) |
b0c22438 | 1529 | First you must say |
6e2995f4 | 1530 | |
9548c15c | 1531 | use IO::Handle; |
0462a1ab | 1532 | |
b0c22438 | 1533 | after which you may use either |
0462a1ab | 1534 | |
9548c15c | 1535 | method HANDLE EXPR |
0462a1ab | 1536 | |
b0c22438 | 1537 | or more safely, |
0462a1ab | 1538 | |
9548c15c | 1539 | HANDLE->method(EXPR) |
0462a1ab | 1540 | |
241a59d9 | 1541 | Each method returns the old value of the C<IO::Handle> attribute. The |
b0c22438 | 1542 | methods each take an optional EXPR, which, if supplied, specifies the |
241a59d9 | 1543 | new value for the C<IO::Handle> attribute in question. If not |
b0c22438 | 1544 | supplied, most methods do nothing to the current value--except for |
1545 | C<autoflush()>, which will assume a 1 for you, just to be different. | |
0462a1ab | 1546 | |
b0c22438 | 1547 | Because loading in the C<IO::Handle> class is an expensive operation, |
1548 | you should learn how to use the regular built-in variables. | |
1549 | ||
241a59d9 | 1550 | A few of these variables are considered "read-only". This means that |
b0c22438 | 1551 | if you try to assign to this variable, either directly or indirectly |
1552 | through a reference, you'll raise a run-time exception. | |
1553 | ||
1554 | You should be very careful when modifying the default values of most | |
241a59d9 | 1555 | special variables described in this document. In most cases you want |
b0c22438 | 1556 | to localize these variables before changing them, since if you don't, |
1557 | the change may affect other modules which rely on the default values | |
241a59d9 | 1558 | of the special variables that you have changed. This is one of the |
b0c22438 | 1559 | correct ways to read the whole file at once: |
1560 | ||
9548c15c FC |
1561 | open my $fh, "<", "foo" or die $!; |
1562 | local $/; # enable localized slurp mode | |
1563 | my $content = <$fh>; | |
1564 | close $fh; | |
b0c22438 | 1565 | |
1566 | But the following code is quite bad: | |
1567 | ||
9548c15c FC |
1568 | open my $fh, "<", "foo" or die $!; |
1569 | undef $/; # enable slurp mode | |
1570 | my $content = <$fh>; | |
1571 | close $fh; | |
b0c22438 | 1572 | |
1573 | since some other module, may want to read data from some file in the | |
1574 | default "line mode", so if the code we have just presented has been | |
1575 | executed, the global value of C<$/> is now changed for any other code | |
1576 | running inside the same Perl interpreter. | |
1577 | ||
1578 | Usually when a variable is localized you want to make sure that this | |
241a59d9 FC |
1579 | change affects the shortest scope possible. So unless you are already |
1580 | inside some short C<{}> block, you should create one yourself. For | |
b0c22438 | 1581 | example: |
1582 | ||
9548c15c FC |
1583 | my $content = ''; |
1584 | open my $fh, "<", "foo" or die $!; | |
1585 | { | |
0763c253 | 1586 | local $/; |
1587 | $content = <$fh>; | |
9548c15c FC |
1588 | } |
1589 | close $fh; | |
0462a1ab | 1590 | |
b0c22438 | 1591 | Here is an example of how your own code can go broken: |
0462a1ab | 1592 | |
9548c15c | 1593 | for ( 1..3 ){ |
0763c253 | 1594 | $\ = "\r\n"; |
1595 | nasty_break(); | |
1596 | print "$_"; | |
9548c15c | 1597 | } |
0b9346e6 | 1598 | |
9548c15c | 1599 | sub nasty_break { |
0763c253 | 1600 | $\ = "\f"; |
1601 | # do something with $_ | |
9548c15c | 1602 | } |
0462a1ab | 1603 | |
0b9346e6 | 1604 | You probably expect this code to print the equivalent of |
0462a1ab | 1605 | |
0b9346e6 | 1606 | "1\r\n2\r\n3\r\n" |
0462a1ab | 1607 | |
b0c22438 | 1608 | but instead you get: |
0462a1ab | 1609 | |
0b9346e6 | 1610 | "1\f2\f3\f" |
0462a1ab | 1611 | |
0b9346e6 | 1612 | Why? Because C<nasty_break()> modifies C<$\> without localizing it |
241a59d9 FC |
1613 | first. The value you set in C<nasty_break()> is still there when you |
1614 | return. The fix is to add C<local()> so the value doesn't leak out of | |
0b9346e6 | 1615 | C<nasty_break()>: |
6e2995f4 | 1616 | |
9548c15c | 1617 | local $\ = "\f"; |
a0d0e21e | 1618 | |
b0c22438 | 1619 | It's easy to notice the problem in such a short example, but in more |
1620 | complicated code you are looking for trouble if you don't localize | |
1621 | changes to the special variables. | |
a0d0e21e | 1622 | |
b0c22438 | 1623 | =over 8 |
a0d0e21e | 1624 | |
b0c22438 | 1625 | =item $ARGV |
1626 | X<$ARGV> | |
fb73857a | 1627 | |
ca1b95ae | 1628 | Contains the name of the current file when reading from C<< <> >>. |
b0c22438 | 1629 | |
1630 | =item @ARGV | |
1631 | X<@ARGV> | |
1632 | ||
ca1b95ae | 1633 | The array C<@ARGV> contains the command-line arguments intended for |
241a59d9 | 1634 | the script. C<$#ARGV> is generally the number of arguments minus |
b0c22438 | 1635 | one, because C<$ARGV[0]> is the first argument, I<not> the program's |
241a59d9 | 1636 | command name itself. See L</$0> for the command name. |
b0c22438 | 1637 | |
84dabc03 | 1638 | =item ARGV |
1639 | X<ARGV> | |
1640 | ||
1641 | The special filehandle that iterates over command-line filenames in | |
241a59d9 FC |
1642 | C<@ARGV>. Usually written as the null filehandle in the angle operator |
1643 | C<< <> >>. Note that currently C<ARGV> only has its magical effect | |
84dabc03 | 1644 | within the C<< <> >> operator; elsewhere it is just a plain filehandle |
241a59d9 | 1645 | corresponding to the last file opened by C<< <> >>. In particular, |
84dabc03 | 1646 | passing C<\*ARGV> as a parameter to a function that expects a filehandle |
1647 | may not cause your function to automatically read the contents of all the | |
1648 | files in C<@ARGV>. | |
1649 | ||
b0c22438 | 1650 | =item ARGVOUT |
1651 | X<ARGVOUT> | |
1652 | ||
1653 | The special filehandle that points to the currently open output file | |
241a59d9 FC |
1654 | when doing edit-in-place processing with B<-i>. Useful when you have |
1655 | to do a lot of inserting and don't want to keep modifying C<$_>. See | |
028611fa | 1656 | L<perlrun|perlrun/-i[extension]> for the B<-i> switch. |
b0c22438 | 1657 | |
96948506 | 1658 | =item IO::Handle->output_field_separator( EXPR ) |
84dabc03 | 1659 | |
1660 | =item $OUTPUT_FIELD_SEPARATOR | |
1661 | ||
1662 | =item $OFS | |
1663 | ||
1664 | =item $, | |
1665 | X<$,> X<$OFS> X<$OUTPUT_FIELD_SEPARATOR> | |
1666 | ||
241a59d9 FC |
1667 | The output field separator for the print operator. If defined, this |
1668 | value is printed between each of print's arguments. Default is C<undef>. | |
84dabc03 | 1669 | |
96948506 | 1670 | You cannot call C<output_field_separator()> on a handle, only as a |
008f9687 | 1671 | static method. See L<IO::Handle|IO::Handle>. |
96948506 | 1672 | |
84dabc03 | 1673 | Mnemonic: what is printed when there is a "," in your print statement. |
1674 | ||
5b442a2a | 1675 | =item HANDLE->input_line_number( EXPR ) |
b0c22438 | 1676 | |
1677 | =item $INPUT_LINE_NUMBER | |
1678 | ||
1679 | =item $NR | |
1680 | ||
1681 | =item $. | |
1682 | X<$.> X<$NR> X<$INPUT_LINE_NUMBER> X<line number> | |
1683 | ||
1684 | Current line number for the last filehandle accessed. | |
1685 | ||
1686 | Each filehandle in Perl counts the number of lines that have been read | |
241a59d9 | 1687 | from it. (Depending on the value of C<$/>, Perl's idea of what |
b0c22438 | 1688 | constitutes a line may not match yours.) When a line is read from a |
1689 | filehandle (via C<readline()> or C<< <> >>), or when C<tell()> or | |
1690 | C<seek()> is called on it, C<$.> becomes an alias to the line counter | |
1691 | for that filehandle. | |
1692 | ||
1693 | You can adjust the counter by assigning to C<$.>, but this will not | |
241a59d9 FC |
1694 | actually move the seek pointer. I<Localizing C<$.> will not localize |
1695 | the filehandle's line count>. Instead, it will localize perl's notion | |
b0c22438 | 1696 | of which filehandle C<$.> is currently aliased to. |
1697 | ||
1698 | C<$.> is reset when the filehandle is closed, but B<not> when an open | |
241a59d9 FC |
1699 | filehandle is reopened without an intervening C<close()>. For more |
1700 | details, see L<perlop/"IE<sol>O Operators">. Because C<< <> >> never does | |
b0c22438 | 1701 | an explicit close, line numbers increase across C<ARGV> files (but see |
1702 | examples in L<perlfunc/eof>). | |
1703 | ||
1704 | You can also use C<< HANDLE->input_line_number(EXPR) >> to access the | |
1705 | line counter for a given filehandle without having to worry about | |
1706 | which handle you last accessed. | |
1707 | ||
1708 | Mnemonic: many programs use "." to mean the current line number. | |
1709 | ||
96948506 | 1710 | =item IO::Handle->input_record_separator( EXPR ) |
b0c22438 | 1711 | |
1712 | =item $INPUT_RECORD_SEPARATOR | |
1713 | ||
1714 | =item $RS | |
1715 | ||
1716 | =item $/ | |
1717 | X<$/> X<$RS> X<$INPUT_RECORD_SEPARATOR> | |
1718 | ||
241a59d9 FC |
1719 | The input record separator, newline by default. This influences Perl's |
1720 | idea of what a "line" is. Works like B<awk>'s RS variable, including | |
84dabc03 | 1721 | treating empty lines as a terminator if set to the null string (an |
241a59d9 | 1722 | empty line cannot contain any spaces or tabs). You may set it to a |
84dabc03 | 1723 | multi-character string to match a multi-character terminator, or to |
241a59d9 | 1724 | C<undef> to read through the end of file. Setting it to C<"\n\n"> |
84dabc03 | 1725 | means something slightly different than setting to C<"">, if the file |
241a59d9 FC |
1726 | contains consecutive empty lines. Setting to C<""> will treat two or |
1727 | more consecutive empty lines as a single empty line. Setting to | |
84dabc03 | 1728 | C<"\n\n"> will blindly assume that the next input character belongs to |
1729 | the next paragraph, even if it's a newline. | |
b0c22438 | 1730 | |
1731 | local $/; # enable "slurp" mode | |
1732 | local $_ = <FH>; # whole file now here | |
1733 | s/\n[ \t]+/ /g; | |
1734 | ||
241a59d9 | 1735 | Remember: the value of C<$/> is a string, not a regex. B<awk> has to |
b0c22438 | 1736 | be better for something. :-) |
1737 | ||
440af013 JK |
1738 | Setting C<$/> to an empty string -- the so-called I<paragraph mode> -- merits |
1739 | special attention. When C<$/> is set to C<""> and the entire file is read in | |
6e46265c | 1740 | with that setting, any sequence of one or more consecutive newlines at the |
440af013 JK |
1741 | beginning of the file is discarded. With the exception of the final record in |
1742 | the file, each sequence of characters ending in two or more newlines is | |
1743 | treated as one record and is read in to end in exactly two newlines. If the | |
1744 | last record in the file ends in zero or one consecutive newlines, that record | |
1745 | is read in with that number of newlines. If the last record ends in two or | |
1746 | more consecutive newlines, it is read in with two newlines like all preceding | |
1747 | records. | |
1748 | ||
1749 | Suppose we wrote the following string to a file: | |
1750 | ||
1751 | my $string = "\n\n\n"; | |
1752 | $string .= "alpha beta\ngamma delta\n\n\n"; | |
1753 | $string .= "epsilon zeta eta\n\n"; | |
1754 | $string .= "theta\n"; | |
1755 | ||
19151286 | 1756 | my $file = 'simple_file.txt'; |
440af013 JK |
1757 | open my $OUT, '>', $file or die; |
1758 | print $OUT $string; | |
1759 | close $OUT or die; | |
1760 | ||
1761 | Now we read that file in paragraph mode: | |
1762 | ||
1763 | local $/ = ""; # paragraph mode | |
1764 | open my $IN, '<', $file or die; | |
1765 | my @records = <$IN>; | |
1766 | close $IN or die; | |
1767 | ||
1768 | C<@records> will consist of these 3 strings: | |
1769 | ||
1770 | ( | |
1771 | "alpha beta\ngamma delta\n\n", | |
1772 | "epsilon zeta eta\n\n", | |
1773 | "theta\n", | |
1774 | ) | |
1775 | ||
b0c22438 | 1776 | Setting C<$/> to a reference to an integer, scalar containing an |
1777 | integer, or scalar that's convertible to an integer will attempt to | |
1778 | read records instead of lines, with the maximum record size being the | |
3d249121 | 1779 | referenced integer number of characters. So this: |
b0c22438 | 1780 | |
1781 | local $/ = \32768; # or \"32768", or \$var_containing_32768 | |
1782 | open my $fh, "<", $myfile or die $!; | |
1783 | local $_ = <$fh>; | |
fb73857a | 1784 | |
f1ee460b | 1785 | will read a record of no more than 32768 characters from $fh. If you're |
b0c22438 | 1786 | not reading from a record-oriented file (or your OS doesn't have |
1787 | record-oriented files), then you'll likely get a full chunk of data | |
241a59d9 FC |
1788 | with every read. If a record is larger than the record size you've |
1789 | set, you'll get the record back in pieces. Trying to set the record | |
b3a2acfa YO |
1790 | size to zero or less is deprecated and will cause $/ to have the value |
1791 | of "undef", which will cause reading in the (rest of the) whole file. | |
1792 | ||
1793 | As of 5.19.9 setting C<$/> to any other form of reference will throw a | |
1794 | fatal exception. This is in preparation for supporting new ways to set | |
1795 | C<$/> in the future. | |
6e2995f4 | 1796 | |
78c28381 | 1797 | On VMS only, record reads bypass PerlIO layers and any associated |
3d249121 | 1798 | buffering, so you must not mix record and non-record reads on the |
78c28381 CB |
1799 | same filehandle. Record mode mixes with line mode only when the |
1800 | same buffering layer is in use for both modes. | |
5c055ba3 | 1801 | |
96948506 | 1802 | You cannot call C<input_record_separator()> on a handle, only as a |
008f9687 | 1803 | static method. See L<IO::Handle|IO::Handle>. |
96948506 | 1804 | |
008f9687 | 1805 | See also L<perlport/"Newlines">. Also see L</$.>. |
9bf22702 | 1806 | |
b0c22438 | 1807 | Mnemonic: / delimits line boundaries when quoting poetry. |
5c055ba3 | 1808 | |
96948506 | 1809 | =item IO::Handle->output_record_separator( EXPR ) |
84902520 | 1810 | |
b0c22438 | 1811 | =item $OUTPUT_RECORD_SEPARATOR |
84902520 | 1812 | |
b0c22438 | 1813 | =item $ORS |
84902520 | 1814 | |
b0c22438 | 1815 | =item $\ |
1816 | X<$\> X<$ORS> X<$OUTPUT_RECORD_SEPARATOR> | |
84902520 | 1817 | |
241a59d9 FC |
1818 | The output record separator for the print operator. If defined, this |
1819 | value is printed after the last of print's arguments. Default is C<undef>. | |
84902520 | 1820 | |
96948506 | 1821 | You cannot call C<output_record_separator()> on a handle, only as a |
008f9687 | 1822 | static method. See L<IO::Handle|IO::Handle>. |
96948506 | 1823 | |
b0c22438 | 1824 | Mnemonic: you set C<$\> instead of adding "\n" at the end of the print. |
1825 | Also, it's just like C<$/>, but it's what you get "back" from Perl. | |
84902520 | 1826 | |
5b442a2a | 1827 | =item HANDLE->autoflush( EXPR ) |
1828 | ||
1829 | =item $OUTPUT_AUTOFLUSH | |
1830 | ||
84dabc03 | 1831 | =item $| |
1832 | X<$|> X<autoflush> X<flush> X<$OUTPUT_AUTOFLUSH> | |
84902520 | 1833 | |
84dabc03 | 1834 | If set to nonzero, forces a flush right away and after every write or |
241a59d9 | 1835 | print on the currently selected output channel. Default is 0 |
84dabc03 | 1836 | (regardless of whether the channel is really buffered by the system or |
1837 | not; C<$|> tells you only whether you've asked Perl explicitly to | |
241a59d9 FC |
1838 | flush after each write). STDOUT will typically be line buffered if |
1839 | output is to the terminal and block buffered otherwise. Setting this | |
84dabc03 | 1840 | variable is useful primarily when you are outputting to a pipe or |
1841 | socket, such as when you are running a Perl program under B<rsh> and | |
241a59d9 FC |
1842 | want to see the output as it's happening. This has no effect on input |
1843 | buffering. See L<perlfunc/getc> for that. See L<perlfunc/select> on | |
1844 | how to select the output channel. See also L<IO::Handle>. | |
84dabc03 | 1845 | |
1846 | Mnemonic: when you want your pipes to be piping hot. | |
1847 | ||
8561ea1d FC |
1848 | =item ${^LAST_FH} |
1849 | X<${^LAST_FH}> | |
1850 | ||
1851 | This read-only variable contains a reference to the last-read filehandle. | |
1852 | This is set by C<< <HANDLE> >>, C<readline>, C<tell>, C<eof> and C<seek>. | |
1853 | This is the same handle that C<$.> and C<tell> and C<eof> without arguments | |
1854 | use. It is also the handle used when Perl appends ", <STDIN> line 1" to | |
1855 | an error or warning message. | |
1856 | ||
1857 | This variable was added in Perl v5.18.0. | |
1858 | ||
84dabc03 | 1859 | =back |
84902520 | 1860 | |
b0c22438 | 1861 | =head3 Variables related to formats |
83ee9e09 | 1862 | |
b0c22438 | 1863 | The special variables for formats are a subset of those for |
241a59d9 | 1864 | filehandles. See L<perlform> for more information about Perl's |
69b55ccc | 1865 | formats. |
83ee9e09 | 1866 | |
b0c22438 | 1867 | =over 8 |
83ee9e09 | 1868 | |
84dabc03 | 1869 | =item $ACCUMULATOR |
1870 | ||
1871 | =item $^A | |
1872 | X<$^A> X<$ACCUMULATOR> | |
1873 | ||
1874 | The current value of the C<write()> accumulator for C<format()> lines. | |
1875 | A format contains C<formline()> calls that put their result into | |
241a59d9 FC |
1876 | C<$^A>. After calling its format, C<write()> prints out the contents |
1877 | of C<$^A> and empties. So you never really see the contents of C<$^A> | |
1878 | unless you call C<formline()> yourself and then look at it. See | |
96090e4f | 1879 | L<perlform> and L<perlfunc/"formline PICTURE,LIST">. |
84dabc03 | 1880 | |
96948506 | 1881 | =item IO::Handle->format_formfeed(EXPR) |
5b442a2a | 1882 | |
1883 | =item $FORMAT_FORMFEED | |
1884 | ||
84dabc03 | 1885 | =item $^L |
1886 | X<$^L> X<$FORMAT_FORMFEED> | |
1887 | ||
241a59d9 | 1888 | What formats output as a form feed. The default is C<\f>. |
84dabc03 | 1889 | |
96948506 | 1890 | You cannot call C<format_formfeed()> on a handle, only as a static |
008f9687 | 1891 | method. See L<IO::Handle|IO::Handle>. |
96948506 | 1892 | |
b0c22438 | 1893 | =item HANDLE->format_page_number(EXPR) |
83ee9e09 | 1894 | |
b0c22438 | 1895 | =item $FORMAT_PAGE_NUMBER |
83ee9e09 | 1896 | |
b0c22438 | 1897 | =item $% |
1898 | X<$%> X<$FORMAT_PAGE_NUMBER> | |
83ee9e09 | 1899 | |
b0c22438 | 1900 | The current page number of the currently selected output channel. |
83ee9e09 | 1901 | |
b0c22438 | 1902 | Mnemonic: C<%> is page number in B<nroff>. |
7619c85e | 1903 | |
b0c22438 | 1904 | =item HANDLE->format_lines_left(EXPR) |
b9ac3b5b | 1905 | |
b0c22438 | 1906 | =item $FORMAT_LINES_LEFT |
66558a10 | 1907 | |
b0c22438 | 1908 | =item $- |
1909 | X<$-> X<$FORMAT_LINES_LEFT> | |
fb73857a | 1910 | |
b0c22438 | 1911 | The number of lines left on the page of the currently selected output |
1912 | channel. | |
fa05a9fd | 1913 | |
b0c22438 | 1914 | Mnemonic: lines_on_page - lines_printed. |
fa05a9fd | 1915 | |
96948506 | 1916 | =item IO::Handle->format_line_break_characters EXPR |
fb73857a | 1917 | |
84dabc03 | 1918 | =item $FORMAT_LINE_BREAK_CHARACTERS |
a0d0e21e | 1919 | |
84dabc03 | 1920 | =item $: |
1921 | X<$:> X<FORMAT_LINE_BREAK_CHARACTERS> | |
a0d0e21e | 1922 | |
84dabc03 | 1923 | The current set of characters after which a string may be broken to |
241a59d9 | 1924 | fill continuation fields (starting with C<^>) in a format. The default is |
84dabc03 | 1925 | S<" \n-">, to break on a space, newline, or a hyphen. |
a0d0e21e | 1926 | |
96948506 | 1927 | You cannot call C<format_line_break_characters()> on a handle, only as |
008f9687 | 1928 | a static method. See L<IO::Handle|IO::Handle>. |
96948506 | 1929 | |
84dabc03 | 1930 | Mnemonic: a "colon" in poetry is a part of a line. |
1931 | ||
1932 | =item HANDLE->format_lines_per_page(EXPR) | |
1933 | ||
1934 | =item $FORMAT_LINES_PER_PAGE | |
1935 | ||
1936 | =item $= | |
1937 | X<$=> X<$FORMAT_LINES_PER_PAGE> | |
1938 | ||
1939 | The current page length (printable lines) of the currently selected | |
241a59d9 | 1940 | output channel. The default is 60. |
84dabc03 | 1941 | |
1942 | Mnemonic: = has horizontal lines. | |
7c36658b | 1943 | |
b0c22438 | 1944 | =item HANDLE->format_top_name(EXPR) |
7c36658b | 1945 | |
b0c22438 | 1946 | =item $FORMAT_TOP_NAME |
a05d7ebb | 1947 | |
b0c22438 | 1948 | =item $^ |
1949 | X<$^> X<$FORMAT_TOP_NAME> | |
fde18df1 | 1950 | |
b0c22438 | 1951 | The name of the current top-of-page format for the currently selected |
241a59d9 FC |
1952 | output channel. The default is the name of the filehandle with C<_TOP> |
1953 | appended. For example, the default format top name for the C<STDOUT> | |
12abbafd | 1954 | filehandle is C<STDOUT_TOP>. |
e07ea26a | 1955 | |
b0c22438 | 1956 | Mnemonic: points to top of page. |
e07ea26a | 1957 | |
84dabc03 | 1958 | =item HANDLE->format_name(EXPR) |
16070b82 | 1959 | |
84dabc03 | 1960 | =item $FORMAT_NAME |
aa2f2a36 | 1961 | |
84dabc03 | 1962 | =item $~ |
1963 | X<$~> X<$FORMAT_NAME> | |
aa2f2a36 | 1964 | |
84dabc03 | 1965 | The name of the current report format for the currently selected |
241a59d9 FC |
1966 | output channel. The default format name is the same as the filehandle |
1967 | name. For example, the default format name for the C<STDOUT> | |
84dabc03 | 1968 | filehandle is just C<STDOUT>. |
16070b82 | 1969 | |
84dabc03 | 1970 | Mnemonic: brother to C<$^>. |
16070b82 | 1971 | |
b0c22438 | 1972 | =back |
a0d0e21e | 1973 | |
84dabc03 | 1974 | =head2 Error Variables |
b0c22438 | 1975 | X<error> X<exception> |
a0d0e21e | 1976 | |
b0c22438 | 1977 | The variables C<$@>, C<$!>, C<$^E>, and C<$?> contain information |
1978 | about different types of error conditions that may appear during | |
241a59d9 | 1979 | execution of a Perl program. The variables are shown ordered by |
b0c22438 | 1980 | the "distance" between the subsystem which reported the error and |
241a59d9 | 1981 | the Perl process. They correspond to errors detected by the Perl |
b0c22438 | 1982 | interpreter, C library, operating system, or an external program, |
1983 | respectively. | |
4438c4b7 | 1984 | |
b0c22438 | 1985 | To illustrate the differences between these variables, consider the |
241a59d9 | 1986 | following Perl expression, which uses a single-quoted string. After |
7fd683ff | 1987 | execution of this statement, perl may have set all four special error |
7333b1c4 | 1988 | variables: |
4438c4b7 | 1989 | |
9548c15c | 1990 | eval q{ |
0763c253 | 1991 | open my $pipe, "/cdrom/install |" or die $!; |
1992 | my @res = <$pipe>; | |
1993 | close $pipe or die "bad pipe: $?, $!"; | |
9548c15c | 1994 | }; |
a0d0e21e | 1995 | |
7333b1c4 | 1996 | When perl executes the C<eval()> expression, it translates the |
1997 | C<open()>, C<< <PIPE> >>, and C<close> calls in the C run-time library | |
241a59d9 | 1998 | and thence to the operating system kernel. perl sets C<$!> to |
7333b1c4 | 1999 | the C library's C<errno> if one of these calls fails. |
2a8c8378 | 2000 | |
84dabc03 | 2001 | C<$@> is set if the string to be C<eval>-ed did not compile (this may |
2002 | happen if C<open> or C<close> were imported with bad prototypes), or | |
241a59d9 | 2003 | if Perl code executed during evaluation C<die()>d. In these cases the |
0b9346e6 | 2004 | value of C<$@> is the compile error, or the argument to C<die> (which |
241a59d9 | 2005 | will interpolate C<$!> and C<$?>). (See also L<Fatal>, though.) |
2a8c8378 | 2006 | |
84dabc03 | 2007 | Under a few operating systems, C<$^E> may contain a more verbose error |
241a59d9 | 2008 | indicator, such as in this case, "CDROM tray not closed." Systems that |
84dabc03 | 2009 | do not support extended error messages leave C<$^E> the same as C<$!>. |
a0d0e21e | 2010 | |
2e6ba115 | 2011 | Finally, C<$?> may be set to a non-0 value if the external program |
241a59d9 | 2012 | F</cdrom/install> fails. The upper eight bits reflect specific error |
84dabc03 | 2013 | conditions encountered by the program (the program's C<exit()> value). |
2014 | The lower eight bits reflect mode of failure, like signal death and | |
241a59d9 | 2015 | core dump information. See L<wait(2)> for details. In contrast to |
2e6ba115 | 2016 | C<$!> and C<$^E>, which are set only if an error condition is detected, |
84dabc03 | 2017 | the variable C<$?> is set on each C<wait> or pipe C<close>, |
241a59d9 | 2018 | overwriting the old value. This is more like C<$@>, which on every |
84dabc03 | 2019 | C<eval()> is always set on failure and cleared on success. |
a0d0e21e | 2020 | |
b0c22438 | 2021 | For more details, see the individual descriptions at C<$@>, C<$!>, |
2022 | C<$^E>, and C<$?>. | |
38e4f4ae | 2023 | |
0b9346e6 | 2024 | =over 8 |
2025 | ||
b0c22438 | 2026 | =item ${^CHILD_ERROR_NATIVE} |
2027 | X<$^CHILD_ERROR_NATIVE> | |
a0d0e21e | 2028 | |
b0c22438 | 2029 | The native status returned by the last pipe close, backtick (C<``>) |
2030 | command, successful call to C<wait()> or C<waitpid()>, or from the | |
241a59d9 | 2031 | C<system()> operator. On POSIX-like systems this value can be decoded |
f2febc77 KW |
2032 | with the WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, and |
2033 | WSTOPSIG functions provided by the L<POSIX> module. | |
a0d0e21e | 2034 | |
b0c22438 | 2035 | Under VMS this reflects the actual VMS exit status; i.e. it is the |
2036 | same as C<$?> when the pragma C<use vmsish 'status'> is in effect. | |
a0d0e21e | 2037 | |
60cf4914 | 2038 | This variable was added in Perl v5.10.0. |
a0d0e21e | 2039 | |
5b442a2a | 2040 | =item $EXTENDED_OS_ERROR |
2041 | ||
84dabc03 | 2042 | =item $^E |
2043 | X<$^E> X<$EXTENDED_OS_ERROR> | |
2044 | ||
241a59d9 | 2045 | Error information specific to the current operating system. At the |
a804e657 | 2046 | moment, this differs from C<L</$!>> under only VMS, OS/2, and Win32 (and |
241a59d9 | 2047 | for MacPerl). On all other platforms, C<$^E> is always just the same |
84dabc03 | 2048 | as C<$!>. |
2049 | ||
2050 | Under VMS, C<$^E> provides the VMS status value from the last system | |
241a59d9 FC |
2051 | error. This is more specific information about the last system error |
2052 | than that provided by C<$!>. This is particularly important when C<$!> | |
84dabc03 | 2053 | is set to B<EVMSERR>. |
2054 | ||
2055 | Under OS/2, C<$^E> is set to the error code of the last call to OS/2 | |
2056 | API either via CRT, or directly from perl. | |
2057 | ||
2058 | Under Win32, C<$^E> always returns the last error information reported | |
2059 | by the Win32 call C<GetLastError()> which describes the last error | |
241a59d9 FC |
2060 | from within the Win32 API. Most Win32-specific code will report errors |
2061 | via C<$^E>. ANSI C and Unix-like calls set C<errno> and so most | |
84dabc03 | 2062 | portable Perl code will report errors via C<$!>. |
2063 | ||
a95b3d6a | 2064 | Caveats mentioned in the description of C<L</$!>> generally apply to |
84dabc03 | 2065 | C<$^E>, also. |
2066 | ||
2067 | This variable was added in Perl 5.003. | |
2068 | ||
2069 | Mnemonic: Extra error explanation. | |
0b9346e6 | 2070 | |
84dabc03 | 2071 | =item $EXCEPTIONS_BEING_CAUGHT |
2072 | ||
2073 | =item $^S | |
2074 | X<$^S> X<$EXCEPTIONS_BEING_CAUGHT> | |
2075 | ||
2076 | Current state of the interpreter. | |
2077 | ||
0763c253 | 2078 | $^S State |
2079 | --------- ------------------------------------- | |
2080 | undef Parsing module, eval, or main program | |
afded0c3 | 2081 | true (1) Executing an eval or try block |
0763c253 | 2082 | false (0) Otherwise |
84dabc03 | 2083 | |
2084 | The first state may happen in C<$SIG{__DIE__}> and C<$SIG{__WARN__}> | |
2085 | handlers. | |
2086 | ||
aa959a20 FC |
2087 | The English name $EXCEPTIONS_BEING_CAUGHT is slightly misleading, because |
2088 | the C<undef> value does not indicate whether exceptions are being caught, | |
2089 | since compilation of the main program does not catch exceptions. | |
2090 | ||
84dabc03 | 2091 | This variable was added in Perl 5.004. |
2092 | ||
2093 | =item $WARNING | |
2094 | ||
2095 | =item $^W | |
2096 | X<$^W> X<$WARNING> | |
2097 | ||
2098 | The current value of the warning switch, initially true if B<-w> was | |
2099 | used, false otherwise, but directly modifiable. | |
2100 | ||
2101 | See also L<warnings>. | |
2102 | ||
0b9346e6 | 2103 | Mnemonic: related to the B<-w> switch. |
84dabc03 | 2104 | |
2105 | =item ${^WARNING_BITS} | |
ca1b95ae | 2106 | X<${^WARNING_BITS}> |
84dabc03 | 2107 | |
2108 | The current set of warning checks enabled by the C<use warnings> pragma. | |
44567c86 FC |
2109 | It has the same scoping as the C<$^H> and C<%^H> variables. The exact |
2110 | values are considered internal to the L<warnings> pragma and may change | |
2111 | between versions of Perl. | |
84dabc03 | 2112 | |
5079aff1 YO |
2113 | Each time a statement completes being compiled, the current value of |
2114 | C<${^WARNING_BITS}> is stored with that statement, and can later be | |
2115 | retrieved via C<(caller($level))[9]>. | |
2116 | ||
60cf4914 | 2117 | This variable was added in Perl v5.6.0. |
84dabc03 | 2118 | |
b0c22438 | 2119 | =item $OS_ERROR |
5ccee41e | 2120 | |
b0c22438 | 2121 | =item $ERRNO |
5ccee41e | 2122 | |
b0c22438 | 2123 | =item $! |
2124 | X<$!> X<$ERRNO> X<$OS_ERROR> | |
9b0e6e7a | 2125 | |
a73bef78 JL |
2126 | When referenced, C<$!> retrieves the current value |
2127 | of the C C<errno> integer variable. | |
2128 | If C<$!> is assigned a numerical value, that value is stored in C<errno>. | |
2129 | When referenced as a string, C<$!> yields the system error string | |
2130 | corresponding to C<errno>. | |
2131 | ||
2132 | Many system or library calls set C<errno> if they fail, | |
2133 | to indicate the cause of failure. They usually do B<not> | |
e275abc0 TC |
2134 | set C<errno> to zero if they succeed and may set C<errno> to a |
2135 | non-zero value on success. This means C<errno>, hence C<$!>, is | |
2136 | meaningful only I<immediately> after a B<failure>: | |
a73bef78 JL |
2137 | |
2138 | if (open my $fh, "<", $filename) { | |
0763c253 | 2139 | # Here $! is meaningless. |
2140 | ... | |
7fd683ff | 2141 | } |
ca1b95ae | 2142 | else { |
0763c253 | 2143 | # ONLY here is $! meaningful. |
2144 | ... | |
2145 | # Already here $! might be meaningless. | |
b0c22438 | 2146 | } |
2147 | # Since here we might have either success or failure, | |
a73bef78 | 2148 | # $! is meaningless. |
a0d0e21e | 2149 | |
a73bef78 JL |
2150 | Here, I<meaningless> means that C<$!> may be unrelated to the outcome |
2151 | of the C<open()> operator. Assignment to C<$!> is similarly ephemeral. | |
2152 | It can be used immediately before invoking the C<die()> operator, | |
2153 | to set the exit value, or to inspect the system error string | |
2154 | corresponding to error I<n>, or to restore C<$!> to a meaningful state. | |
d54b56d5 | 2155 | |
8a65a5dc TC |
2156 | Perl itself may set C<errno> to a non-zero on failure even if no |
2157 | system call is performed. | |
2158 | ||
b0c22438 | 2159 | Mnemonic: What just went bang? |
314d39ce | 2160 | |
b0c22438 | 2161 | =item %OS_ERROR |
fb73857a | 2162 | |
b0c22438 | 2163 | =item %ERRNO |
fb73857a | 2164 | |
b0c22438 | 2165 | =item %! |
5b442a2a | 2166 | X<%!> X<%OS_ERROR> X<%ERRNO> |
a0d0e21e | 2167 | |
b0c22438 | 2168 | Each element of C<%!> has a true value only if C<$!> is set to that |
241a59d9 | 2169 | value. For example, C<$!{ENOENT}> is true if and only if the current |
84dabc03 | 2170 | value of C<$!> is C<ENOENT>; that is, if the most recent error was "No |
2171 | such file or directory" (or its moral equivalent: not all operating | |
3b90fd91 RS |
2172 | systems give that exact error, and certainly not all languages). The |
2173 | specific true value is not guaranteed, but in the past has generally | |
2174 | been the numeric value of C<$!>. To check if a particular key is | |
2175 | meaningful on your system, use C<exists $!{the_key}>; for a list of legal | |
2176 | keys, use C<keys %!>. See L<Errno> for more information, and also see | |
2177 | L</$!>. | |
a0d0e21e | 2178 | |
b0c22438 | 2179 | This variable was added in Perl 5.005. |
44f0be63 | 2180 | |
84dabc03 | 2181 | =item $CHILD_ERROR |
b687b08b | 2182 | |
84dabc03 | 2183 | =item $? |
2184 | X<$?> X<$CHILD_ERROR> | |
a0d0e21e | 2185 | |
84dabc03 | 2186 | The status returned by the last pipe close, backtick (C<``>) command, |
2187 | successful call to C<wait()> or C<waitpid()>, or from the C<system()> | |
241a59d9 | 2188 | operator. This is just the 16-bit status word returned by the |
84dabc03 | 2189 | traditional Unix C<wait()> system call (or else is made up to look |
241a59d9 | 2190 | like it). Thus, the exit value of the subprocess is really (C<<< $? >> |
84dabc03 | 2191 | 8 >>>), and C<$? & 127> gives which signal, if any, the process died |
2192 | from, and C<$? & 128> reports whether there was a core dump. | |
a0d0e21e | 2193 | |
84dabc03 | 2194 | Additionally, if the C<h_errno> variable is supported in C, its value |
2195 | is returned via C<$?> if any C<gethost*()> function fails. | |
b687b08b | 2196 | |
84dabc03 | 2197 | If you have installed a signal handler for C<SIGCHLD>, the |
2198 | value of C<$?> will usually be wrong outside that handler. | |
a0d0e21e | 2199 | |
84dabc03 | 2200 | Inside an C<END> subroutine C<$?> contains the value that is going to be |
241a59d9 FC |
2201 | given to C<exit()>. You can modify C<$?> in an C<END> subroutine to |
2202 | change the exit status of your program. For example: | |
a0d0e21e | 2203 | |
84dabc03 | 2204 | END { |
0763c253 | 2205 | $? = 1 if $? == 255; # die would make it 255 |
84dabc03 | 2206 | } |
a0d0e21e | 2207 | |
84dabc03 | 2208 | Under VMS, the pragma C<use vmsish 'status'> makes C<$?> reflect the |
2209 | actual VMS exit status, instead of the default emulation of POSIX | |
2210 | status; see L<perlvms/$?> for details. | |
2211 | ||
2212 | Mnemonic: similar to B<sh> and B<ksh>. | |
a0d0e21e | 2213 | |
b0c22438 | 2214 | =item $EVAL_ERROR |
f648820c | 2215 | |
b0c22438 | 2216 | =item $@ |
2217 | X<$@> X<$EVAL_ERROR> | |
a0d0e21e | 2218 | |
2e6ba115 LM |
2219 | The Perl error from the last C<eval> operator, i.e. the last exception that |
2220 | was caught. For C<eval BLOCK>, this is either a runtime error message or the | |
2221 | string or reference C<die> was called with. The C<eval STRING> form also | |
2222 | catches syntax errors and other compile time exceptions. | |
2223 | ||
2224 | If no error occurs, C<eval> sets C<$@> to the empty string. | |
a0d0e21e | 2225 | |
241a59d9 | 2226 | Warning messages are not collected in this variable. You can, however, |
b0c22438 | 2227 | set up a routine to process warnings by setting C<$SIG{__WARN__}> as |
7333b1c4 | 2228 | described in L</%SIG>. |
748a9306 | 2229 | |
2e6ba115 | 2230 | Mnemonic: Where was the error "at"? |
7f315d2e | 2231 | |
b0c22438 | 2232 | =back |
7f315d2e | 2233 | |
1fa81471 DR |
2234 | =head2 Variables related to the interpreter state |
2235 | ||
2236 | These variables provide information about the current interpreter state. | |
2237 | ||
2238 | =over 8 | |
2239 | ||
2240 | =item $COMPILING | |
2241 | ||
2242 | =item $^C | |
2243 | X<$^C> X<$COMPILING> | |
2244 | ||
2245 | The current value of the flag associated with the B<-c> switch. | |
2246 | Mainly of use with B<-MO=...> to allow code to alter its behavior | |
2247 | when being compiled, such as for example to C<AUTOLOAD> at compile | |
241a59d9 | 2248 | time rather than normal, deferred loading. Setting |
1fa81471 DR |
2249 | C<$^C = 1> is similar to calling C<B::minus_c>. |
2250 | ||
60cf4914 | 2251 | This variable was added in Perl v5.6.0. |
1fa81471 DR |
2252 | |
2253 | =item $DEBUGGING | |
2254 | ||
2255 | =item $^D | |
2256 | X<$^D> X<$DEBUGGING> | |
2257 | ||
241a59d9 | 2258 | The current value of the debugging flags. May be read or set. Like its |
a975eeba | 2259 | L<command-line equivalent|perlrun/B<-D>I<letters>>, you can use numeric |
8af589bf KW |
2260 | or symbolic values, e.g. C<$^D = 10> or C<$^D = "st">. See |
2261 | L<perlrun/B<-D>I<number>>. The contents of this variable also affects the | |
2262 | debugger operation. See L<perldebguts/Debugger Internals>. | |
1fa81471 DR |
2263 | |
2264 | Mnemonic: value of B<-D> switch. | |
2265 | ||
1fa81471 DR |
2266 | =item ${^GLOBAL_PHASE} |
2267 | X<${^GLOBAL_PHASE}> | |
2268 | ||
2269 | The current phase of the perl interpreter. | |
2270 | ||
2271 | Possible values are: | |
2272 | ||
2273 | =over 8 | |
2274 | ||
2275 | =item CONSTRUCT | |
2276 | ||
241a59d9 | 2277 | The C<PerlInterpreter*> is being constructed via C<perl_construct>. This |
1fa81471 | 2278 | value is mostly there for completeness and for use via the |
241a59d9 | 2279 | underlying C variable C<PL_phase>. It's not really possible for Perl |
1fa81471 DR |
2280 | code to be executed unless construction of the interpreter is |
2281 | finished. | |
2282 | ||
2283 | =item START | |
2284 | ||
241a59d9 | 2285 | This is the global compile-time. That includes, basically, every |
1fa81471 DR |
2286 | C<BEGIN> block executed directly or indirectly from during the |
2287 | compile-time of the top-level program. | |
2288 | ||
2289 | This phase is not called "BEGIN" to avoid confusion with | |
2290 | C<BEGIN>-blocks, as those are executed during compile-time of any | |
241a59d9 | 2291 | compilation unit, not just the top-level program. A new, localised |
1fa81471 DR |
2292 | compile-time entered at run-time, for example by constructs as |
2293 | C<eval "use SomeModule"> are not global interpreter phases, and | |
2294 | therefore aren't reflected by C<${^GLOBAL_PHASE}>. | |
2295 | ||
2296 | =item CHECK | |
2297 | ||
2298 | Execution of any C<CHECK> blocks. | |
2299 | ||
2300 | =item INIT | |
2301 | ||
2302 | Similar to "CHECK", but for C<INIT>-blocks, not C<CHECK> blocks. | |
2303 | ||
2304 | =item RUN | |
2305 | ||
2306 | The main run-time, i.e. the execution of C<PL_main_root>. | |
2307 | ||
2308 | =item END | |
2309 | ||
2310 | Execution of any C<END> blocks. | |
2311 | ||
2312 | =item DESTRUCT | |
2313 | ||
2314 | Global destruction. | |
2315 | ||
2316 | =back | |
2317 | ||
241a59d9 | 2318 | Also note that there's no value for UNITCHECK-blocks. That's because |
1fa81471 DR |
2319 | those are run for each compilation unit individually, and therefore is |
2320 | not a global interpreter phase. | |
2321 | ||
2322 | Not every program has to go through each of the possible phases, but | |
2323 | transition from one phase to another can only happen in the order | |
2324 | described in the above list. | |
2325 | ||
2326 | An example of all of the phases Perl code can see: | |
2327 | ||
2328 | BEGIN { print "compile-time: ${^GLOBAL_PHASE}\n" } | |
2329 | ||
2330 | INIT { print "init-time: ${^GLOBAL_PHASE}\n" } | |
2331 | ||
2332 | CHECK { print "check-time: ${^GLOBAL_PHASE}\n" } | |
2333 | ||
2334 | { | |
2335 | package Print::Phase; | |
2336 | ||
2337 | sub new { | |
2338 | my ($class, $time) = @_; | |
2339 | return bless \$time, $class; | |
2340 | } | |
2341 | ||
2342 | sub DESTROY { | |
2343 | my $self = shift; | |
2344 | print "$$self: ${^GLOBAL_PHASE}\n"; | |
2345 | } | |
2346 | } | |
2347 | ||
2348 | print "run-time: ${^GLOBAL_PHASE}\n"; | |
2349 | ||
2350 | my $runtime = Print::Phase->new( | |
2351 | "lexical variables are garbage collected before END" | |
2352 | ); | |
2353 | ||
2354 | END { print "end-time: ${^GLOBAL_PHASE}\n" } | |
2355 | ||
2356 | our $destruct = Print::Phase->new( | |
2357 | "package variables are garbage collected after END" | |
2358 | ); | |
2359 | ||
2360 | This will print out | |
2361 | ||
2362 | compile-time: START | |
2363 | check-time: CHECK | |
2364 | init-time: INIT | |
2365 | run-time: RUN | |
2366 | lexical variables are garbage collected before END: RUN | |
2367 | end-time: END | |
2368 | package variables are garbage collected after END: DESTRUCT | |
2369 | ||
2370 | This variable was added in Perl 5.14.0. | |
2371 | ||
2372 | =item $^H | |
2373 | X<$^H> | |
2374 | ||
241a59d9 FC |
2375 | WARNING: This variable is strictly for |
2376 | internal use only. Its availability, | |
1fa81471 DR |
2377 | behavior, and contents are subject to change without notice. |
2378 | ||
241a59d9 | 2379 | This variable contains compile-time hints for the Perl interpreter. At the |
1fa81471 DR |
2380 | end of compilation of a BLOCK the value of this variable is restored to the |
2381 | value when the interpreter started to compile the BLOCK. | |
2382 | ||
5079aff1 YO |
2383 | Each time a statement completes being compiled, the current value of |
2384 | C<$^H> is stored with that statement, and can later be retrieved via | |
19151286 | 2385 | C<(caller($level))[8]>. See L<perlfunc/caller EXPR>. |
5079aff1 | 2386 | |
1fa81471 DR |
2387 | When perl begins to parse any block construct that provides a lexical scope |
2388 | (e.g., eval body, required file, subroutine body, loop body, or conditional | |
2389 | block), the existing value of C<$^H> is saved, but its value is left unchanged. | |
2390 | When the compilation of the block is completed, it regains the saved value. | |
2391 | Between the points where its value is saved and restored, code that | |
2392 | executes within BEGIN blocks is free to change the value of C<$^H>. | |
2393 | ||
2394 | This behavior provides the semantic of lexical scoping, and is used in, | |
2395 | for instance, the C<use strict> pragma. | |
2396 | ||
2397 | The contents should be an integer; different bits of it are used for | |
241a59d9 | 2398 | different pragmatic flags. Here's an example: |
1fa81471 | 2399 | |
9548c15c | 2400 | sub add_100 { $^H |= 0x100 } |
1fa81471 | 2401 | |
9548c15c | 2402 | sub foo { |
0763c253 | 2403 | BEGIN { add_100() } |
2404 | bar->baz($boon); | |
9548c15c | 2405 | } |
1fa81471 | 2406 | |
241a59d9 | 2407 | Consider what happens during execution of the BEGIN block. At this point |
1fa81471 | 2408 | the BEGIN block has already been compiled, but the body of C<foo()> is still |
241a59d9 FC |
2409 | being compiled. The new value of C<$^H> |
2410 | will therefore be visible only while | |
1fa81471 DR |
2411 | the body of C<foo()> is being compiled. |
2412 | ||
2413 | Substitution of C<BEGIN { add_100() }> block with: | |
2414 | ||
9548c15c | 2415 | BEGIN { require strict; strict->import('vars') } |
1fa81471 | 2416 | |
241a59d9 | 2417 | demonstrates how C<use strict 'vars'> is implemented. Here's a conditional |
1fa81471 DR |
2418 | version of the same lexical pragma: |
2419 | ||
9548c15c | 2420 | BEGIN { |
0763c253 | 2421 | require strict; strict->import('vars') if $condition |
9548c15c | 2422 | } |
1fa81471 DR |
2423 | |
2424 | This variable was added in Perl 5.003. | |
2425 | ||
2426 | =item %^H | |
2427 | X<%^H> | |
2428 | ||
19151286 LM |
2429 | The C<%^H> hash provides the same scoping semantics as L<C<$^H>|/$^H>. This |
2430 | makes it useful for implementing lexically scoped pragmas. See L<perlpragma>. | |
2431 | All the entries are stringified when accessed at runtime, so only simple values | |
2432 | can be accommodated. This means no references to objects, for example. | |
1fa81471 | 2433 | |
5079aff1 YO |
2434 | Each time a statement completes being compiled, the current value of |
2435 | C<%^H> is stored with that statement, and can later be retrieved via | |
19151286 | 2436 | C<(caller($level))[10]>. See L<perlfunc/caller EXPR>. |
5079aff1 | 2437 | |
1fa81471 DR |
2438 | When putting items into C<%^H>, in order to avoid conflicting with other |
2439 | users of the hash there is a convention regarding which keys to use. | |
2440 | A module should use only keys that begin with the module's name (the | |
2441 | name of its main package) and a "/" character. For example, a module | |
2442 | C<Foo::Bar> should use keys such as C<Foo::Bar/baz>. | |
2443 | ||
60cf4914 | 2444 | This variable was added in Perl v5.6.0. |
1fa81471 DR |
2445 | |
2446 | =item ${^OPEN} | |
2447 | X<${^OPEN}> | |
2448 | ||
e038729f | 2449 | An internal variable used by L<PerlIO>. A string in two parts, separated |
1fa81471 DR |
2450 | by a C<\0> byte, the first part describes the input layers, the second |
2451 | part describes the output layers. | |
2452 | ||
e038729f DB |
2453 | This is the mechanism that applies the lexical effects of the L<open> |
2454 | pragma, and the main program scope effects of the C<io> or C<D> options | |
2455 | for the L<-C command-line switch|perlrun/-C [I<numberE<sol>list>]> and | |
2456 | L<PERL_UNICODE environment variable|perlrun/PERL_UNICODE>. | |
2457 | ||
2458 | The functions C<accept()>, C<open()>, C<pipe()>, C<readpipe()> (as well | |
2459 | as the related C<qx> and C<`STRING`> operators), C<socket()>, | |
2460 | C<socketpair()>, and C<sysopen()> are affected by the lexical value of | |
2461 | this variable. The implicit L</ARGV> handle opened by C<readline()> (or | |
2462 | the related C<< <> >> and C<<< <<>> >>> operators) on passed filenames is | |
2463 | also affected (but not if it opens C<STDIN>). If this variable is not | |
2464 | set, these functions will set the default layers as described in | |
2465 | L<PerlIO/Defaults and how to override them>. | |
2466 | ||
2467 | C<open()> ignores this variable (and the default layers) when called with | |
2468 | 3 arguments and explicit layers are specified. Indirect calls to these | |
2469 | functions via modules like L<IO::Handle> are not affected as they occur | |
2470 | in a different lexical scope. Directory handles such as opened by | |
2471 | C<opendir()> are not currently affected. | |
2472 | ||
60cf4914 | 2473 | This variable was added in Perl v5.8.0. |
1fa81471 DR |
2474 | |
2475 | =item $PERLDB | |
2476 | ||
2477 | =item $^P | |
2478 | X<$^P> X<$PERLDB> | |
2479 | ||
241a59d9 | 2480 | The internal variable for debugging support. The meanings of the |
1fa81471 DR |
2481 | various bits are subject to change, but currently indicate: |
2482 | ||
2483 | =over 6 | |
2484 | ||
2485 | =item 0x01 | |
2486 | ||
2487 | Debug subroutine enter/exit. | |
2488 | ||
2489 | =item 0x02 | |
2490 | ||
241a59d9 FC |
2491 | Line-by-line debugging. Causes C<DB::DB()> subroutine to be called for |
2492 | each statement executed. Also causes saving source code lines (like | |
2493 | 0x400). | |
1fa81471 DR |
2494 | |
2495 | =item 0x04 | |
2496 | ||
2497 | Switch off optimizations. | |
2498 | ||
2499 | =item 0x08 | |
2500 | ||
2501 | Preserve more data for future interactive inspections. | |
2502 | ||
2503 | =item 0x10 | |
2504 | ||
2505 | Keep info about source lines on which a subroutine is defined. | |
2506 | ||
2507 | =item 0x20 | |
2508 | ||
2509 | Start with single-step on. | |
2510 | ||
2511 | =item 0x40 | |
2512 | ||
2513 | Use subroutine address instead of name when reporting. | |
2514 | ||
2515 | =item 0x80 | |
2516 | ||
2517 | Report C<goto &subroutine> as well. | |
2518 | ||
2519 | =item 0x100 | |
2520 | ||
2521 | Provide informative "file" names for evals based on the place they were compiled. | |
2522 | ||
2523 | =item 0x200 | |
2524 | ||
2525 | Provide informative names to anonymous subroutines based on the place they | |
2526 | were compiled. | |
2527 | ||
2528 | =item 0x400 | |
2529 | ||
2530 | Save source code lines into C<@{"_<$filename"}>. | |
2531 | ||
aab47982 RS |
2532 | =item 0x800 |
2533 | ||
2534 | When saving source, include evals that generate no subroutines. | |
2535 | ||
2536 | =item 0x1000 | |
2537 | ||
2538 | When saving source, include source that did not compile. | |
2539 | ||
1fa81471 DR |
2540 | =back |
2541 | ||
2542 | Some bits may be relevant at compile-time only, some at | |
241a59d9 | 2543 | run-time only. This is a new mechanism and the details may change. |
1fa81471 DR |
2544 | See also L<perldebguts>. |
2545 | ||
2546 | =item ${^TAINT} | |
2547 | X<${^TAINT}> | |
2548 | ||
241a59d9 | 2549 | Reflects if taint mode is on or off. 1 for on (the program was run with |
1fa81471 DR |
2550 | B<-T>), 0 for off, -1 when only taint warnings are enabled (i.e. with |
2551 | B<-t> or B<-TU>). | |
2552 | ||
7c90a946 NB |
2553 | Note: if your perl was built without taint support (see L<perlsec>), |
2554 | then C<${^TAINT}> will always be 0, even if the program was run with B<-T>). | |
2555 | ||
1fa81471 DR |
2556 | This variable is read-only. |
2557 | ||
60cf4914 | 2558 | This variable was added in Perl v5.8.0. |
1fa81471 | 2559 | |
f512d242 KW |
2560 | =item ${^SAFE_LOCALES} |
2561 | X<${^SAFE_LOCALES}> | |
2562 | ||
2563 | Reflects if safe locale operations are available to this perl (when the | |
2564 | value is 1) or not (the value is 0). This variable is always 1 if the | |
e9bc6d6b KW |
2565 | perl has been compiled without threads. It is also 1 if this perl is |
2566 | using thread-safe locale operations. Note that an individual thread may | |
2567 | choose to use the global locale (generally unsafe) by calling | |
58e641fb | 2568 | L<perlapi/switch_to_global_locale>. This variable currently is still |
e9bc6d6b | 2569 | set to 1 in such threads. |
f512d242 KW |
2570 | |
2571 | This variable is read-only. | |
2572 | ||
2573 | This variable was added in Perl v5.28.0. | |
2574 | ||
1fa81471 DR |
2575 | =item ${^UNICODE} |
2576 | X<${^UNICODE}> | |
2577 | ||
028611fa DB |
2578 | Reflects certain Unicode settings of Perl. See |
2579 | L<perlrun|perlrun/-C [numberE<sol>list]> documentation for the C<-C> | |
2580 | switch for more information about the possible values. | |
1fa81471 DR |
2581 | |
2582 | This variable is set during Perl startup and is thereafter read-only. | |
2583 | ||
60cf4914 | 2584 | This variable was added in Perl v5.8.2. |
1fa81471 DR |
2585 | |
2586 | =item ${^UTF8CACHE} | |
2587 | X<${^UTF8CACHE}> | |
2588 | ||
2589 | This variable controls the state of the internal UTF-8 offset caching code. | |
2590 | 1 for on (the default), 0 for off, -1 to debug the caching code by checking | |
2591 | all its results against linear scans, and panicking on any discrepancy. | |
2592 | ||
94df5432 KW |
2593 | This variable was added in Perl v5.8.9. It is subject to change or |
2594 | removal without notice, but is currently used to avoid recalculating the | |
2595 | boundaries of multi-byte UTF-8-encoded characters. | |
1fa81471 DR |
2596 | |
2597 | =item ${^UTF8LOCALE} | |
2598 | X<${^UTF8LOCALE}> | |
2599 | ||
2600 | This variable indicates whether a UTF-8 locale was detected by perl at | |
241a59d9 | 2601 | startup. This information is used by perl when it's in |
1fa81471 | 2602 | adjust-utf8ness-to-locale mode (as when run with the C<-CL> command-line |
028611fa DB |
2603 | switch); see L<perlrun|perlrun/-C [numberE<sol>list]> for more info on |
2604 | this. | |
1fa81471 | 2605 | |
60cf4914 | 2606 | This variable was added in Perl v5.8.8. |
1fa81471 DR |
2607 | |
2608 | =back | |
2609 | ||
b0c22438 | 2610 | =head2 Deprecated and removed variables |
7f315d2e | 2611 | |
0b9346e6 | 2612 | Deprecating a variable announces the intent of the perl maintainers to |
241a59d9 FC |
2613 | eventually remove the variable from the language. It may still be |
2614 | available despite its status. Using a deprecated variable triggers | |
b0c22438 | 2615 | a warning. |
7f315d2e | 2616 | |
84dabc03 | 2617 | Once a variable is removed, its use triggers an error telling you |
b0c22438 | 2618 | the variable is unsupported. |
7f315d2e | 2619 | |
84dabc03 | 2620 | See L<perldiag> for details about error messages. |
7f315d2e | 2621 | |
b0c22438 | 2622 | =over 8 |
7f315d2e | 2623 | |
84dabc03 | 2624 | =item $# |
b7a15f05 | 2625 | X<$#> |
84dabc03 | 2626 | |
38e5787b | 2627 | C<$#> was a variable that could be used to format printed numbers. |
60cf4914 | 2628 | After a deprecation cycle, its magic was removed in Perl v5.10.0 and |
84dabc03 | 2629 | using it now triggers a warning: C<$# is no longer supported>. |
2630 | ||
2631 | This is not the sigil you use in front of an array name to get the | |
241a59d9 FC |
2632 | last index, like C<$#array>. That's still how you get the last index |
2633 | of an array in Perl. The two have nothing to do with each other. | |
84dabc03 | 2634 | |
2635 | Deprecated in Perl 5. | |
2636 | ||
60cf4914 | 2637 | Removed in Perl v5.10.0. |
84dabc03 | 2638 | |
7f315d2e CO |
2639 | =item $* |
2640 | X<$*> | |
2641 | ||
84dabc03 | 2642 | C<$*> was a variable that you could use to enable multiline matching. |
60cf4914 | 2643 | After a deprecation cycle, its magic was removed in Perl v5.10.0. |
7f315d2e | 2644 | Using it now triggers a warning: C<$* is no longer supported>. |
84dabc03 | 2645 | You should use the C</s> and C</m> regexp modifiers instead. |
7f315d2e | 2646 | |
b0c22438 | 2647 | Deprecated in Perl 5. |
7f315d2e | 2648 | |
60cf4914 | 2649 | Removed in Perl v5.10.0. |
7f315d2e | 2650 | |
84dabc03 | 2651 | =item $[ |
b7a15f05 | 2652 | X<$[> |
84dabc03 | 2653 | |
b82b06b8 FC |
2654 | This variable stores the index of the first element in an array, and |
2655 | of the first character in a substring. The default is 0, but you could | |
2656 | theoretically set it to 1 to make Perl behave more like B<awk> (or Fortran) | |
2657 | when subscripting and when evaluating the index() and substr() functions. | |
84dabc03 | 2658 | |
b82b06b8 FC |
2659 | As of release 5 of Perl, assignment to C<$[> is treated as a compiler |
2660 | directive, and cannot influence the behavior of any other file. | |
2661 | (That's why you can only assign compile-time constants to it.) | |
2662 | Its use is highly discouraged. | |
2663 | ||
60cf4914 | 2664 | Prior to Perl v5.10.0, assignment to C<$[> could be seen from outer lexical |
b82b06b8 FC |
2665 | scopes in the same file, unlike other compile-time directives (such as |
2666 | L<strict>). Using local() on it would bind its value strictly to a lexical | |
2667 | block. Now it is always lexically scoped. | |
2668 | ||
c22e17d0 | 2669 | As of Perl v5.16.0, it is implemented by the L<arybase> module. |
84dabc03 | 2670 | |
c22e17d0 DIM |
2671 | As of Perl v5.30.0, or under C<use v5.16>, or C<no feature "array_base">, |
2672 | C<$[> no longer has any effect, and always contains 0. | |
2673 | Assigning 0 to it is permitted, but any other value will produce an error. | |
6b54f8ab | 2674 | |
b82b06b8 FC |
2675 | Mnemonic: [ begins subscripts. |
2676 | ||
60cf4914 | 2677 | Deprecated in Perl v5.12.0. |
e1dccc0d | 2678 | |
0763c253 | 2679 | =item ${^ENCODING} |
2680 | X<${^ENCODING}> | |
2681 | ||
2682 | This variable is no longer supported. | |
2683 | ||
2684 | It used to hold the I<object reference> to the C<Encode> object that was | |
2685 | used to convert the source code to Unicode. | |
2686 | ||
2687 | Its purpose was to allow your non-ASCII Perl | |
2688 | scripts not to have to be written in UTF-8; this was | |
2689 | useful before editors that worked on UTF-8 encoded text were common, but | |
2690 | that was long ago. It caused problems, such as affecting the operation | |
2691 | of other modules that weren't expecting it, causing general mayhem. | |
2692 | ||
2693 | If you need something like this functionality, it is recommended that use | |
2694 | you a simple source filter, such as L<Filter::Encoding>. | |
2695 | ||
2696 | If you are coming here because code of yours is being adversely affected | |
2697 | by someone's use of this variable, you can usually work around it by | |
2698 | doing this: | |
2699 | ||
2700 | local ${^ENCODING}; | |
2701 | ||
2702 | near the beginning of the functions that are getting broken. This | |
2703 | undefines the variable during the scope of execution of the including | |
2704 | function. | |
2705 | ||
2706 | This variable was added in Perl 5.8.2 and removed in 5.26.0. | |
2707 | Setting it to anything other than C<undef> was made fatal in Perl 5.28.0. | |
2708 | ||
2709 | =item ${^WIN32_SLOPPY_STAT} | |
2710 | X<${^WIN32_SLOPPY_STAT}> X<sitecustomize> X<sitecustomize.pl> | |
2711 | ||
2712 | This variable no longer has any function. | |
2713 | ||
2714 | This variable was added in Perl v5.10.0 and removed in Perl v5.34.0. | |
2715 | ||
b0c22438 | 2716 | =back |
2b92dfce | 2717 | |
0b9346e6 | 2718 | =cut |