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 | ||
9 | Variable names in Perl can have several formats. Usually, they | |
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 | |
13 | C<::> or C<'>. In this case, the part before the last C<::> or | |
14 | C<'> is taken to be a I<package qualifier>; see L<perlmod>. | |
15 | ||
16 | Perl variable names may also be a sequence of digits or a single | |
17 | punctuation or control character. These names are all reserved for | |
18 | special uses by Perl; for example, the all-digits names are used | |
19 | to hold data captured by backreferences after a regular expression | |
20 | match. Perl has a special syntax for the single-control-character | |
21 | names: It understands C<^X> (caret C<X>) to mean the control-C<X> | |
22 | character. For example, the notation C<$^W> (dollar-sign caret | |
23 | C<W>) is the scalar variable whose name is the single character | |
24 | control-C<W>. This is better than typing a literal control-C<W> | |
25 | into your program. | |
26 | ||
27 | Since Perl 5.6, Perl variable names may be alphanumeric | |
28 | strings that begin with control characters (or better yet, a caret). | |
29 | These variables must be written in the form C<${^Foo}>; the braces | |
30 | are not optional. C<${^Foo}> denotes the scalar variable whose | |
31 | name is a control-C<F> followed by two C<o>'s. These variables are | |
32 | reserved for future special uses by Perl, except for the ones that | |
33 | begin with C<^_> (control-underscore or caret-underscore). No | |
34 | control-character name that begins with C<^_> will acquire a special | |
35 | meaning in any future version of Perl; such names may therefore be | |
36 | used safely in programs. C<$^_> itself, however, I<is> reserved. | |
37 | ||
38 | Perl identifiers that begin with digits, control characters, or | |
39 | punctuation characters are exempt from the effects of the C<package> | |
40 | declaration and are always forced to be in package C<main>; they are | |
41 | also exempt from C<strict 'vars'> errors. A few other names are also | |
42 | exempt in these ways: | |
43 | ||
44 | ENV STDIN | |
45 | INC STDOUT | |
46 | ARGV STDERR | |
47 | ARGVOUT _ | |
48 | SIG | |
49 | ||
69520822 | 50 | In particular, the special C<${^_XYZ}> variables are always taken |
b0c22438 | 51 | to be in package C<main>, regardless of any C<package> declarations |
52 | presently in scope. | |
53 | ||
54 | =head1 SPECIAL VARIABLES | |
a0d0e21e | 55 | |
84dabc03 | 56 | The following names have special meaning to Perl. Most punctuation |
57 | names have reasonable mnemonics, or analogs in the shells. | |
58 | Nevertheless, if you wish to use long variable names, you need only | |
59 | say | |
a0d0e21e LW |
60 | |
61 | use English; | |
62 | ||
a1ce9542 JF |
63 | at the top of your program. This aliases all the short names to the long |
64 | names in the current package. Some even have medium names, generally | |
84dabc03 | 65 | borrowed from B<awk>. To avoid a performance hit, if you don't need the |
66 | C<$PREMATCH>, C<$MATCH>, or C<$POSTMATCH> it's best to use the C<English> | |
67 | module without them: | |
a0d0e21e | 68 | |
a1ce9542 JF |
69 | use English '-no_match_vars'; |
70 | ||
84dabc03 | 71 | Before you continue, note the sort order for variables. In general, we first list |
72 | the variables in order of their type, scalars first, then arrays and hashes, followed | |
73 | by the odd barewords (i.e. filehandles). Within each variable type, we sort the | |
74 | names in lexicographical order. However, C<$_> gets pride of place since its | |
75 | extra special. | |
a1ce9542 | 76 | |
b0c22438 | 77 | =head2 General Variables |
a0d0e21e | 78 | |
84dabc03 | 79 | =over 8 |
80 | ||
a0d0e21e LW |
81 | =item $ARG |
82 | ||
83 | =item $_ | |
a054c801 | 84 | X<$_> X<$ARG> |
a0d0e21e | 85 | |
b0c22438 | 86 | The default input and pattern-searching space. The following pairs are |
a0d0e21e LW |
87 | equivalent: |
88 | ||
19799a22 | 89 | while (<>) {...} # equivalent only in while! |
54310121 | 90 | while (defined($_ = <>)) {...} |
a0d0e21e LW |
91 | |
92 | /^Subject:/ | |
93 | $_ =~ /^Subject:/ | |
94 | ||
95 | tr/a-z/A-Z/ | |
96 | $_ =~ tr/a-z/A-Z/ | |
97 | ||
19799a22 GS |
98 | chomp |
99 | chomp($_) | |
a0d0e21e | 100 | |
b0c22438 | 101 | Here are the places where Perl will assume C<$_> even if you |
cb1a09d0 AD |
102 | don't use it: |
103 | ||
104 | =over 3 | |
105 | ||
106 | =item * | |
107 | ||
84dabc03 | 108 | The following functions use C<$_> as a default argument: |
db1511c8 | 109 | |
b0169937 GS |
110 | abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, exp, glob, |
111 | hex, int, lc, lcfirst, length, log, lstat, mkdir, oct, ord, pos, print, | |
112 | quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only), | |
b0c18621 | 113 | rmdir, sin, split (on its second argument), sqrt, stat, study, uc, ucfirst, |
b0169937 | 114 | unlink, unpack. |
cb1a09d0 AD |
115 | |
116 | =item * | |
117 | ||
db1511c8 GS |
118 | All file tests (C<-f>, C<-d>) except for C<-t>, which defaults to STDIN. |
119 | See L<perlfunc/-X> | |
120 | ||
cb1a09d0 AD |
121 | |
122 | =item * | |
123 | ||
b0169937 GS |
124 | The pattern matching operations C<m//>, C<s///> and C<tr///> (aka C<y///>) |
125 | when used without an C<=~> operator. | |
cb1a09d0 | 126 | |
54310121 | 127 | =item * |
cb1a09d0 AD |
128 | |
129 | The default iterator variable in a C<foreach> loop if no other | |
130 | variable is supplied. | |
131 | ||
54310121 | 132 | =item * |
cb1a09d0 | 133 | |
b0c22438 | 134 | The implicit iterator variable in the C<grep()> and C<map()> functions. |
cb1a09d0 | 135 | |
54310121 | 136 | =item * |
cb1a09d0 | 137 | |
b0c22438 | 138 | The implicit variable of C<given()>. |
db1511c8 GS |
139 | |
140 | =item * | |
141 | ||
c47ff5f1 | 142 | The default place to put an input record when a C<< <FH> >> |
cb1a09d0 | 143 | operation's result is tested by itself as the sole criterion of a C<while> |
b0c22438 | 144 | test. Outside a C<while> test, this will not happen. |
cb1a09d0 AD |
145 | |
146 | =back | |
147 | ||
59f00321 | 148 | As C<$_> is a global variable, this may lead in some cases to unwanted |
b0c22438 | 149 | side-effects. As of perl 5.9.1, you can now use a lexical version of |
150 | C<$_> by declaring it in a file or in a block with C<my>. Moreover, | |
4fd88bf8 | 151 | declaring C<our $_> restores the global C<$_> in the current scope. |
59f00321 | 152 | |
b0c22438 | 153 | Mnemonic: underline is understood in certain operations. |
a0d0e21e | 154 | |
a1db74c9 JH |
155 | =item $a |
156 | ||
157 | =item $b | |
a054c801 | 158 | X<$a> X<$b> |
a1db74c9 | 159 | |
b0c22438 | 160 | Special package variables when using C<sort()>, see L<perlfunc/sort>. |
161 | Because of this specialness C<$a> and C<$b> don't need to be declared | |
162 | (using C<use vars>, or C<our()>) even when using the C<strict 'vars'> | |
163 | pragma. Don't lexicalize them with C<my $a> or C<my $b> if you want to | |
164 | be able to use them in the C<sort()> comparison block or function. | |
a1db74c9 | 165 | |
b0c22438 | 166 | =item $SUBSCRIPT_SEPARATOR |
a1db74c9 | 167 | |
b0c22438 | 168 | =item $SUBSEP |
a1db74c9 | 169 | |
b0c22438 | 170 | =item $; |
171 | X<$;> X<$SUBSEP> X<SUBSCRIPT_SEPARATOR> | |
a0d0e21e | 172 | |
b0c22438 | 173 | The subscript separator for multidimensional array emulation. If you |
174 | refer to a hash element as | |
a0d0e21e | 175 | |
b0c22438 | 176 | $foo{$a,$b,$c} |
a0d0e21e | 177 | |
b0c22438 | 178 | it really means |
a0d0e21e | 179 | |
b0c22438 | 180 | $foo{join($;, $a, $b, $c)} |
a0d0e21e | 181 | |
b0c22438 | 182 | But don't put |
19ddd453 | 183 | |
b0c22438 | 184 | @foo{$a,$b,$c} # a slice--note the @ |
a054c801 | 185 | |
b0c22438 | 186 | which means |
cde0cee5 | 187 | |
b0c22438 | 188 | ($foo{$a},$foo{$b},$foo{$c}) |
cde0cee5 | 189 | |
b0c22438 | 190 | Default is "\034", the same as SUBSEP in B<awk>. If your |
191 | keys contain binary data there might not be any safe value for C<$;>. | |
a0d0e21e | 192 | |
b0c22438 | 193 | Consider using "real" multidimensional arrays as described |
194 | in L<perllol>. | |
a0d0e21e | 195 | |
b0c22438 | 196 | Mnemonic: comma (the syntactic subscript separator) is a semi-semicolon. |
a0d0e21e | 197 | |
1311257d | 198 | =item $LIST_SEPARATOR |
199 | ||
200 | =item $" | |
201 | X<$"> X<$LIST_SEPARATOR> | |
202 | ||
69520822 | 203 | When an array or an array slice is interpolated into a double-quoted |
204 | string or a similar context such as C</.../>, its elements are | |
205 | separated by this value. Default is a space. For example, this: | |
206 | ||
207 | print "The array is: @array\n"; | |
208 | ||
209 | is equivalent to this: | |
210 | ||
211 | print "The array is: " . join($", @array) . "\n"; | |
212 | ||
213 | Mnemonic: works in double-quoted context. | |
1311257d | 214 | |
b0c22438 | 215 | =item ${^ENCODING} |
216 | X<$^ENCODING> | |
19ddd453 | 217 | |
b0c22438 | 218 | The I<object reference> to the C<Encode> object that is used to convert |
219 | the source code to Unicode. Thanks to this variable your Perl script | |
220 | does not have to be written in UTF-8. Default is I<undef>. The direct | |
221 | manipulation of this variable is highly discouraged. | |
a054c801 | 222 | |
b0c22438 | 223 | This variable was added in Perl 5.8.2. |
cde0cee5 | 224 | |
b0c22438 | 225 | =item $PROCESS_ID |
cde0cee5 | 226 | |
b0c22438 | 227 | =item $PID |
a0d0e21e | 228 | |
b0c22438 | 229 | =item $$ |
230 | X<$$> X<$PID> X<$PROCESS_ID> | |
a0d0e21e | 231 | |
b0c22438 | 232 | The process number of the Perl running this script. You should |
233 | consider this variable read-only, although it will be altered | |
234 | across C<fork()> calls. | |
a0d0e21e | 235 | |
b0c22438 | 236 | Note for Linux users: on Linux, the C functions C<getpid()> and |
237 | C<getppid()> return different values from different threads. In order to | |
238 | be portable, this behavior is not reflected by C<$$>, whose value remains | |
239 | consistent across threads. If you want to call the underlying C<getpid()>, | |
240 | you may use the CPAN module C<Linux::Pid>. | |
a0d0e21e | 241 | |
b0c22438 | 242 | Mnemonic: same as shells. |
243 | ||
244 | =item $REAL_USER_ID | |
a0d0e21e | 245 | |
b0c22438 | 246 | =item $UID |
19ddd453 | 247 | |
b0c22438 | 248 | =item $< |
249 | X<< $< >> X<$UID> X<$REAL_USER_ID> | |
a054c801 | 250 | |
b0c22438 | 251 | The real uid of this process. You can change both the real uid and the |
252 | effective uid at the same time by using C<POSIX::setuid()>. Since | |
253 | changes to C<< $< >> require a system call, check C<$!> after a change | |
254 | attempt to detect any possible errors. | |
cde0cee5 | 255 | |
b0c22438 | 256 | Mnemonic: it's the uid you came I<from>, if you're running setuid. |
cde0cee5 | 257 | |
b0c22438 | 258 | =item $EFFECTIVE_USER_ID |
a0d0e21e | 259 | |
b0c22438 | 260 | =item $EUID |
a0d0e21e | 261 | |
b0c22438 | 262 | =item $> |
263 | X<< $> >> X<$EUID> X<$EFFECTIVE_USER_ID> | |
a0d0e21e | 264 | |
b0c22438 | 265 | The effective uid of this process. For example: |
a0d0e21e | 266 | |
69520822 | 267 | $< = $>; # set real to effective uid |
268 | ($<,$>) = ($>,$<); # swap real and effective uids | |
a0d0e21e | 269 | |
b0c22438 | 270 | You can change both the effective uid and the real uid at the same |
271 | time by using C<POSIX::setuid()>. Changes to C<< $> >> require a check | |
272 | to C<$!> to detect any possible errors after an attempted change. | |
daaddde1 | 273 | |
b0c22438 | 274 | C<< $< >> and C<< $> >> can be swapped only on machines |
275 | supporting C<setreuid()>. | |
a01268b5 | 276 | |
b0c22438 | 277 | Mnemonic: it's the uid you went I<to>, if you're running setuid. |
ad83b128 | 278 | |
b0c22438 | 279 | =item $REAL_GROUP_ID |
a01268b5 | 280 | |
b0c22438 | 281 | =item $GID |
a01268b5 | 282 | |
b0c22438 | 283 | =item $( |
284 | X<$(> X<$GID> X<$REAL_GROUP_ID> | |
a01268b5 | 285 | |
b0c22438 | 286 | The real gid of this process. If you are on a machine that supports |
287 | membership in multiple groups simultaneously, gives a space separated | |
288 | list of groups you are in. The first number is the one returned by | |
289 | C<getgid()>, and the subsequent ones by C<getgroups()>, one of which may be | |
290 | the same as the first number. | |
a01268b5 | 291 | |
b0c22438 | 292 | However, a value assigned to C<$(> must be a single number used to |
293 | set the real gid. So the value given by C<$(> should I<not> be assigned | |
294 | back to C<$(> without being forced numeric, such as by adding zero. Note | |
295 | that this is different to the effective gid (C<$)>) which does take a | |
296 | list. | |
fe307981 | 297 | |
b0c22438 | 298 | You can change both the real gid and the effective gid at the same |
299 | time by using C<POSIX::setgid()>. Changes to C<$(> require a check to C<$!> | |
300 | to detect any possible errors after an attempted change. | |
6cef1e77 | 301 | |
b0c22438 | 302 | Mnemonic: parentheses are used to I<group> things. The real gid is the |
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 | |
b0c22438 | 312 | The effective gid of this process. If you are on a machine that |
313 | supports membership in multiple groups simultaneously, gives a space | |
314 | separated list of groups you are in. The first number is the one | |
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 |
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 | |
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 |
331 | machines that support the corresponding I<set[re][ug]id()> routine. C<$(> | |
332 | and C<$)> can be swapped only on machines supporting C<setregid()>. | |
3195cf34 | 333 | |
b0c22438 | 334 | Mnemonic: parentheses are used to I<group> things. The effective gid |
335 | is the group that's I<right> for you, if you're running setgid. | |
44a2ac75 | 336 | |
b0c22438 | 337 | =item $PROGRAM_NAME |
a0d0e21e | 338 | |
b0c22438 | 339 | =item $0 |
340 | X<$0> X<$PROGRAM_NAME> | |
a0d0e21e | 341 | |
b0c22438 | 342 | Contains the name of the program being executed. |
a0d0e21e | 343 | |
69520822 | 344 | On some (but not all) operating systems assigning to C<$0> modifies |
b0c22438 | 345 | the argument area that the C<ps> program sees. On some platforms you |
346 | may have to use special C<ps> options or a different C<ps> to see the | |
347 | changes. Modifying the C<$0> is more useful as a way of indicating the | |
348 | current program state than it is for hiding the program you're | |
349 | running. | |
a0d0e21e | 350 | |
69520822 | 351 | Note that there are platform-specific limitations on the maximum |
b0c22438 | 352 | length of C<$0>. In the most extreme case it may be limited to the |
353 | space occupied by the original C<$0>. | |
fcc7d916 | 354 | |
b0c22438 | 355 | In some platforms there may be arbitrary amount of padding, for |
356 | example space characters, after the modified name as shown by C<ps>. | |
357 | In some platforms this padding may extend all the way to the original | |
358 | length of the argument area, no matter what you do (this is the case | |
359 | for example with Linux 2.2). | |
fcc7d916 | 360 | |
b0c22438 | 361 | Note for BSD users: setting C<$0> does not completely remove "perl" |
362 | from the ps(1) output. For example, setting C<$0> to C<"foobar"> may | |
363 | result in C<"perl: foobar (perl)"> (whether both the C<"perl: "> prefix | |
364 | and the " (perl)" suffix are shown depends on your exact BSD variant | |
365 | and version). This is an operating system feature, Perl cannot help it. | |
fcc7d916 | 366 | |
b0c22438 | 367 | In multithreaded scripts Perl coordinates the threads so that any |
368 | thread may modify its copy of the C<$0> and the change becomes visible | |
369 | to ps(1) (assuming the operating system plays along). Note that | |
370 | the view of C<$0> the other threads have will not change since they | |
371 | have their own copies of it. | |
fcc7d916 | 372 | |
b0c22438 | 373 | If the program has been given to perl via the switches C<-e> or C<-E>, |
374 | C<$0> will contain the string C<"-e">. | |
fcc7d916 | 375 | |
b0c22438 | 376 | On Linux as of perl 5.14 the legacy process name will be set with |
377 | L<prctl(2)>, in addition to altering the POSIX name via C<argv[0]> as | |
378 | perl has done since version 4.000. Now system utilities that read the | |
379 | legacy process name such as ps, top and killall will recognize the | |
380 | name you set when assigning to C<$0>. The string you supply will be | |
381 | cut off at 16 bytes, this is a limitation imposed by Linux. | |
fcc7d916 | 382 | |
b0c22438 | 383 | Mnemonic: same as B<sh> and B<ksh>. |
384 | ||
385 | =item $COMPILING | |
a0d0e21e | 386 | |
b0c22438 | 387 | =item $^C |
388 | X<$^C> X<$COMPILING> | |
a0d0e21e | 389 | |
b0c22438 | 390 | The current value of the flag associated with the B<-c> switch. |
391 | Mainly of use with B<-MO=...> to allow code to alter its behavior | |
392 | when being compiled, such as for example to C<AUTOLOAD> at compile | |
393 | time rather than normal, deferred loading. Setting | |
394 | C<$^C = 1> is similar to calling C<B::minus_c>. | |
a0d0e21e | 395 | |
b0c22438 | 396 | This variable was added in Perl 5.6. |
a0d0e21e | 397 | |
b0c22438 | 398 | =item $DEBUGGING |
a0d0e21e | 399 | |
b0c22438 | 400 | =item $^D |
401 | X<$^D> X<$DEBUGGING> | |
a0d0e21e | 402 | |
b0c22438 | 403 | The current value of the debugging flags. May be read or set. Like its |
404 | command-line equivalent, you can use numeric or symbolic values, eg | |
405 | C<$^D = 10> or C<$^D = "st">. | |
68dc0745 | 406 | |
b0c22438 | 407 | Mnemonic: value of B<-D> switch. |
5b2b9c68 | 408 | |
b0c22438 | 409 | =item $SYSTEM_FD_MAX |
5b2b9c68 | 410 | |
b0c22438 | 411 | =item $^F |
412 | X<$^F> X<$SYSTEM_FD_MAX> | |
5b2b9c68 | 413 | |
b0c22438 | 414 | The maximum system file descriptor, ordinarily 2. System file |
415 | descriptors are passed to C<exec()>ed processes, while higher file | |
416 | descriptors are not. Also, during an C<open()>, system file descriptors are | |
417 | preserved even if the C<open()> fails (ordinary file descriptors are | |
418 | closed before the C<open()> is attempted). The close-on-exec | |
419 | status of a file descriptor will be decided according to the value of | |
420 | C<$^F> when the corresponding file, pipe, or socket was opened, not the | |
421 | time of the C<exec()>. | |
5b2b9c68 | 422 | |
b0c22438 | 423 | =item $^H |
883faa13 | 424 | |
b0c22438 | 425 | WARNING: This variable is strictly for internal use only. Its availability, |
426 | behavior, and contents are subject to change without notice. | |
a0d0e21e | 427 | |
b0c22438 | 428 | This variable contains compile-time hints for the Perl interpreter. At the |
429 | end of compilation of a BLOCK the value of this variable is restored to the | |
430 | value when the interpreter started to compile the BLOCK. | |
a0d0e21e | 431 | |
b0c22438 | 432 | When perl begins to parse any block construct that provides a lexical scope |
433 | (e.g., eval body, required file, subroutine body, loop body, or conditional | |
434 | block), the existing value of C<$^H> is saved, but its value is left unchanged. | |
435 | When the compilation of the block is completed, it regains the saved value. | |
436 | Between the points where its value is saved and restored, code that | |
437 | executes within BEGIN blocks is free to change the value of C<$^H>. | |
a0d0e21e | 438 | |
b0c22438 | 439 | This behavior provides the semantic of lexical scoping, and is used in, |
440 | for instance, the C<use strict> pragma. | |
a0d0e21e | 441 | |
b0c22438 | 442 | The contents should be an integer; different bits of it are used for |
443 | different pragmatic flags. Here's an example: | |
a0d0e21e | 444 | |
b0c22438 | 445 | sub add_100 { $^H |= 0x100 } |
a0d0e21e | 446 | |
b0c22438 | 447 | sub foo { |
448 | BEGIN { add_100() } | |
449 | bar->baz($boon); | |
450 | } | |
a0d0e21e | 451 | |
b0c22438 | 452 | Consider what happens during execution of the BEGIN block. At this point |
453 | the BEGIN block has already been compiled, but the body of C<foo()> is still | |
454 | being compiled. The new value of C<$^H> will therefore be visible only while | |
455 | the body of C<foo()> is being compiled. | |
a0d0e21e | 456 | |
b0c22438 | 457 | Substitution of the above BEGIN block with: |
a0d0e21e | 458 | |
b0c22438 | 459 | BEGIN { require strict; strict->import('vars') } |
a0d0e21e | 460 | |
b0c22438 | 461 | demonstrates how C<use strict 'vars'> is implemented. Here's a conditional |
462 | version of the same lexical pragma: | |
a0d0e21e | 463 | |
b0c22438 | 464 | BEGIN { require strict; strict->import('vars') if $condition } |
a0d0e21e | 465 | |
b0c22438 | 466 | This variable was added in Perl 5.003. |
a0d0e21e | 467 | |
b0c22438 | 468 | =item %^H |
a0d0e21e | 469 | |
b0c22438 | 470 | The C<%^H> hash provides the same scoping semantic as C<$^H>. This makes it |
471 | useful for implementation of lexically scoped pragmas. See L<perlpragma>. | |
a0d0e21e | 472 | |
b0c22438 | 473 | This variable was added in Perl 5.6. |
a0d0e21e | 474 | |
b0c22438 | 475 | =item $INPLACE_EDIT |
a0d0e21e | 476 | |
b0c22438 | 477 | =item $^I |
478 | X<$^I> X<$INPLACE_EDIT> | |
a0d0e21e | 479 | |
b0c22438 | 480 | The current value of the inplace-edit extension. Use C<undef> to disable |
481 | inplace editing. | |
a0d0e21e | 482 | |
b0c22438 | 483 | Mnemonic: value of B<-i> switch. |
a0d0e21e | 484 | |
b0c22438 | 485 | =item $^M |
486 | X<$^M> | |
a0d0e21e | 487 | |
b0c22438 | 488 | By default, running out of memory is an untrappable, fatal error. |
489 | However, if suitably built, Perl can use the contents of C<$^M> | |
490 | as an emergency memory pool after C<die()>ing. Suppose that your Perl | |
491 | were compiled with C<-DPERL_EMERGENCY_SBRK> and used Perl's malloc. | |
492 | Then | |
a0d0e21e | 493 | |
b0c22438 | 494 | $^M = 'a' x (1 << 16); |
a0d0e21e | 495 | |
b0c22438 | 496 | would allocate a 64K buffer for use in an emergency. See the |
497 | F<INSTALL> file in the Perl distribution for information on how to | |
498 | add custom C compilation flags when compiling perl. To discourage casual | |
499 | use of this advanced feature, there is no L<English|English> long name for | |
500 | this variable. | |
a0d0e21e | 501 | |
b0c22438 | 502 | This variable was added in Perl 5.004. |
a0d0e21e | 503 | |
b0c22438 | 504 | =item $OSNAME |
a0d0e21e | 505 | |
b0c22438 | 506 | =item $^O |
507 | X<$^O> X<$OSNAME> | |
a0d0e21e | 508 | |
b0c22438 | 509 | The name of the operating system under which this copy of Perl was |
510 | built, as determined during the configuration process. For examples | |
511 | see L<perlport/PLATFORMS>. | |
a0d0e21e | 512 | |
b0c22438 | 513 | The value is identical to C<$Config{'osname'}>. See also L<Config> |
514 | and the B<-V> command-line switch documented in L<perlrun>. | |
a0d0e21e | 515 | |
b0c22438 | 516 | In Windows platforms, C<$^O> is not very helpful: since it is always |
517 | C<MSWin32>, it doesn't tell the difference between | |
518 | 95/98/ME/NT/2000/XP/CE/.NET. Use C<Win32::GetOSName()> or | |
519 | Win32::GetOSVersion() (see L<Win32> and L<perlport>) to distinguish | |
520 | between the variants. | |
a0d0e21e | 521 | |
b0c22438 | 522 | This variable was added in Perl 5.003. |
a0d0e21e | 523 | |
b0c22438 | 524 | =item ${^OPEN} |
a0d0e21e | 525 | |
b0c22438 | 526 | An internal variable used by PerlIO. A string in two parts, separated |
527 | by a C<\0> byte, the first part describes the input layers, the second | |
528 | part describes the output layers. | |
a0d0e21e | 529 | |
b0c22438 | 530 | This variable was added in Perl 5.8.2. |
a0d0e21e | 531 | |
b0c22438 | 532 | =item $PERLDB |
a0d0e21e | 533 | |
b0c22438 | 534 | =item $^P |
535 | X<$^P> X<$PERLDB> | |
a0d0e21e | 536 | |
b0c22438 | 537 | The internal variable for debugging support. The meanings of the |
538 | various bits are subject to change, but currently indicate: | |
a0d0e21e | 539 | |
b0c22438 | 540 | =over 6 |
a0d0e21e | 541 | |
b0c22438 | 542 | =item 0x01 |
a0d0e21e | 543 | |
b0c22438 | 544 | Debug subroutine enter/exit. |
a0d0e21e | 545 | |
b0c22438 | 546 | =item 0x02 |
a0d0e21e | 547 | |
b0c22438 | 548 | Line-by-line debugging. Causes C<DB::DB()> subroutine to be called for each |
549 | statement executed. Also causes saving source code lines (like 0x400). | |
a0d0e21e | 550 | |
b0c22438 | 551 | =item 0x04 |
fe307981 | 552 | |
b0c22438 | 553 | Switch off optimizations. |
6cef1e77 | 554 | |
b0c22438 | 555 | =item 0x08 |
6cef1e77 | 556 | |
b0c22438 | 557 | Preserve more data for future interactive inspections. |
6cef1e77 | 558 | |
b0c22438 | 559 | =item 0x10 |
4ba05bdc | 560 | |
b0c22438 | 561 | Keep info about source lines on which a subroutine is defined. |
4ba05bdc | 562 | |
b0c22438 | 563 | =item 0x20 |
4ba05bdc | 564 | |
b0c22438 | 565 | Start with single-step on. |
4ba05bdc | 566 | |
b0c22438 | 567 | =item 0x40 |
4ba05bdc | 568 | |
b0c22438 | 569 | Use subroutine address instead of name when reporting. |
4ba05bdc | 570 | |
b0c22438 | 571 | =item 0x80 |
4ba05bdc | 572 | |
b0c22438 | 573 | Report C<goto &subroutine> as well. |
4ba05bdc | 574 | |
b0c22438 | 575 | =item 0x100 |
4ba05bdc | 576 | |
b0c22438 | 577 | Provide informative "file" names for evals based on the place they were compiled. |
4ba05bdc | 578 | |
b0c22438 | 579 | =item 0x200 |
44a2ac75 | 580 | |
b0c22438 | 581 | Provide informative names to anonymous subroutines based on the place they |
582 | were compiled. | |
44a2ac75 | 583 | |
b0c22438 | 584 | =item 0x400 |
44a2ac75 | 585 | |
b0c22438 | 586 | Save source code lines into C<@{"_<$filename"}>. |
44a2ac75 | 587 | |
b0c22438 | 588 | =back |
44a2ac75 | 589 | |
b0c22438 | 590 | Some bits may be relevant at compile-time only, some at |
591 | run-time only. This is a new mechanism and the details may change. | |
592 | See also L<perldebguts>. | |
3195cf34 | 593 | |
b0c22438 | 594 | =item @F |
595 | X<@F> | |
44a2ac75 | 596 | |
b0c22438 | 597 | The array C<@F> contains the fields of each line read in when autosplit |
598 | mode is turned on. See L<perlrun> for the B<-a> switch. This array | |
599 | is package-specific, and must be declared or given a full package name | |
600 | if not in package main when running under C<strict 'vars'>. | |
44a2ac75 | 601 | |
b0c22438 | 602 | =item @INC |
603 | X<@INC> | |
a0d0e21e | 604 | |
b0c22438 | 605 | The array C<@INC> contains the list of places that the C<do EXPR>, |
606 | C<require>, or C<use> constructs look for their library files. It | |
607 | initially consists of the arguments to any B<-I> command-line | |
608 | switches, followed by the default Perl library, probably | |
609 | F</usr/local/lib/perl>, followed by ".", to represent the current | |
610 | directory. ("." will not be appended if taint checks are enabled, | |
611 | either by C<-T> or by C<-t>.) If you need to modify this at runtime, | |
612 | you should use the C<use lib> pragma to get the machine-dependent | |
613 | library properly loaded also: | |
a0d0e21e | 614 | |
b0c22438 | 615 | use lib '/mypath/libdir/'; |
616 | use SomeMod; | |
a0d0e21e | 617 | |
b0c22438 | 618 | You can also insert hooks into the file inclusion system by putting Perl |
619 | code directly into C<@INC>. Those hooks may be subroutine references, array | |
620 | references or blessed objects. See L<perlfunc/require> for details. | |
a0d0e21e | 621 | |
b0c22438 | 622 | =item @ARG |
a0d0e21e | 623 | |
b0c22438 | 624 | =item @_ |
625 | X<@_> X<@ARG> | |
a0d0e21e | 626 | |
b0c22438 | 627 | Within a subroutine the array C<@_> contains the parameters passed to that |
628 | subroutine. See L<perlsub>. | |
a0d0e21e | 629 | |
b0c22438 | 630 | =item %INC |
631 | X<%INC> | |
a0d0e21e | 632 | |
b0c22438 | 633 | The hash C<%INC> contains entries for each filename included via the |
634 | C<do>, C<require>, or C<use> operators. The key is the filename | |
635 | you specified (with module names converted to pathnames), and the | |
636 | value is the location of the file found. The C<require> | |
637 | operator uses this hash to determine whether a particular file has | |
638 | already been included. | |
a0d0e21e | 639 | |
b0c22438 | 640 | If the file was loaded via a hook (e.g. a subroutine reference, see |
641 | L<perlfunc/require> for a description of these hooks), this hook is | |
642 | by default inserted into C<%INC> in place of a filename. Note, however, | |
643 | that the hook may have set the C<%INC> entry by itself to provide some more | |
644 | specific info. | |
a0d0e21e | 645 | |
b0c22438 | 646 | =item %ENV |
a0d0e21e | 647 | |
b0c22438 | 648 | =item $ENV{expr} |
649 | X<%ENV> | |
a0d0e21e | 650 | |
b0c22438 | 651 | The hash C<%ENV> contains your current environment. Setting a |
652 | value in C<ENV> changes the environment for any child processes | |
653 | you subsequently C<fork()> off. | |
a0d0e21e | 654 | |
b0c22438 | 655 | =item %SIG |
a0d0e21e | 656 | |
b0c22438 | 657 | =item $SIG{expr} |
658 | X<%SIG> | |
a0d0e21e | 659 | |
b0c22438 | 660 | The hash C<%SIG> contains signal handlers for signals. For example: |
a0d0e21e | 661 | |
b0c22438 | 662 | sub handler { # 1st argument is signal name |
663 | my($sig) = @_; | |
664 | print "Caught a SIG$sig--shutting down\n"; | |
665 | close(LOG); | |
666 | exit(0); | |
667 | } | |
a0d0e21e | 668 | |
b0c22438 | 669 | $SIG{'INT'} = \&handler; |
670 | $SIG{'QUIT'} = \&handler; | |
671 | ... | |
672 | $SIG{'INT'} = 'DEFAULT'; # restore default action | |
673 | $SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT | |
a0d0e21e | 674 | |
b0c22438 | 675 | Using a value of C<'IGNORE'> usually has the effect of ignoring the |
676 | signal, except for the C<CHLD> signal. See L<perlipc> for more about | |
677 | this special case. | |
a0d0e21e | 678 | |
b0c22438 | 679 | Here are some other examples: |
a0d0e21e | 680 | |
b0c22438 | 681 | $SIG{"PIPE"} = "Plumber"; # assumes main::Plumber (not recommended) |
682 | $SIG{"PIPE"} = \&Plumber; # just fine; assume current Plumber | |
683 | $SIG{"PIPE"} = *Plumber; # somewhat esoteric | |
684 | $SIG{"PIPE"} = Plumber(); # oops, what did Plumber() return?? | |
a0d0e21e | 685 | |
b0c22438 | 686 | Be sure not to use a bareword as the name of a signal handler, |
687 | lest you inadvertently call it. | |
a0d0e21e | 688 | |
b0c22438 | 689 | If your system has the C<sigaction()> function then signal handlers |
690 | are installed using it. This means you get reliable signal handling. | |
7b8d334a | 691 | |
b0c22438 | 692 | The default delivery policy of signals changed in Perl 5.8.0 from |
693 | immediate (also known as "unsafe") to deferred, also known as "safe | |
694 | signals". See L<perlipc> for more information. | |
aa689395 | 695 | |
b0c22438 | 696 | Certain internal hooks can be also set using the C<%SIG> hash. The |
697 | routine indicated by C<$SIG{__WARN__}> is called when a warning | |
698 | message is about to be printed. The warning message is passed as the | |
699 | first argument. The presence of a C<__WARN__> hook causes the | |
700 | ordinary printing of warnings to C<STDERR> to be suppressed. You can | |
701 | use this to save warnings in a variable, or turn warnings into fatal | |
702 | errors, like this: | |
19799a22 | 703 | |
b0c22438 | 704 | local $SIG{__WARN__} = sub { die $_[0] }; |
705 | eval $proggie; | |
a8f8344d | 706 | |
b0c22438 | 707 | As the C<'IGNORE'> hook is not supported by C<__WARN__>, you can |
708 | disable warnings using the empty subroutine: | |
f86702cc | 709 | |
b0c22438 | 710 | local $SIG{__WARN__} = sub {}; |
55602bd2 | 711 | |
b0c22438 | 712 | The routine indicated by C<$SIG{__DIE__}> is called when a fatal |
713 | exception is about to be thrown. The error message is passed as the | |
714 | first argument. When a C<__DIE__> hook routine returns, the exception | |
715 | processing continues as it would have in the absence of the hook, | |
716 | unless the hook routine itself exits via a C<goto>, a loop exit, or a | |
717 | C<die()>. The C<__DIE__> handler is explicitly disabled during the | |
718 | call, so that you can die from a C<__DIE__> handler. Similarly for | |
719 | C<__WARN__>. | |
e5218da5 | 720 | |
b0c22438 | 721 | Due to an implementation glitch, the C<$SIG{__DIE__}> hook is called |
722 | even inside an C<eval()>. Do not use this to rewrite a pending | |
723 | exception in C<$@>, or as a bizarre substitute for overriding | |
724 | C<CORE::GLOBAL::die()>. This strange action at a distance may be fixed | |
725 | in a future release so that C<$SIG{__DIE__}> is only called if your | |
726 | program is about to exit, as was the original intent. Any other use is | |
727 | deprecated. | |
728 | ||
729 | C<__DIE__>/C<__WARN__> handlers are very special in one respect: they | |
730 | may be called to report (probable) errors found by the parser. In such | |
731 | a case the parser may be in inconsistent state, so any attempt to | |
732 | evaluate Perl code from such a handler will probably result in a | |
733 | segfault. This means that warnings or errors that result from parsing | |
734 | Perl should be used with extreme caution, like this: | |
e5218da5 | 735 | |
b0c22438 | 736 | require Carp if defined $^S; |
737 | Carp::confess("Something wrong") if defined &Carp::confess; | |
738 | die "Something wrong, but could not load Carp to give backtrace... | |
739 | To see backtrace try starting Perl with -MCarp switch"; | |
e5218da5 | 740 | |
b0c22438 | 741 | Here the first line will load C<Carp> I<unless> it is the parser who |
742 | called the handler. The second line will print backtrace and die if | |
743 | C<Carp> was available. The third line will be executed only if C<Carp> was | |
744 | not available. | |
0a378802 | 745 | |
b0c22438 | 746 | See L<perlfunc/die>, L<perlfunc/warn>, L<perlfunc/eval>, and |
747 | L<warnings> for additional information. | |
0a378802 | 748 | |
b0c22438 | 749 | =back |
a0d0e21e | 750 | |
b0c22438 | 751 | =head2 Names that are no longer special |
a0d0e21e | 752 | |
b0c22438 | 753 | These variables had special meaning in prior versions of Perl but now |
754 | have no effect and will cause warnings if used. They are included | |
755 | here for historical reference. | |
a0d0e21e | 756 | |
b0c22438 | 757 | =over 8 |
6ab308ee | 758 | |
b0c22438 | 759 | =item $BASETIME |
6ab308ee | 760 | |
b0c22438 | 761 | =item $^T |
762 | X<$^T> X<$BASETIME> | |
6ab308ee | 763 | |
b0c22438 | 764 | The time at which the program began running, in seconds since the |
765 | epoch (beginning of 1970). The values returned by the B<-M>, B<-A>, | |
766 | and B<-C> filetests are based on this value. | |
a0d0e21e | 767 | |
b0c22438 | 768 | =item ${^TAINT} |
55602bd2 | 769 | |
b0c22438 | 770 | Reflects if taint mode is on or off. 1 for on (the program was run with |
771 | B<-T>), 0 for off, -1 when only taint warnings are enabled (i.e. with | |
772 | B<-t> or B<-TU>). | |
daaddde1 | 773 | |
b0c22438 | 774 | This variable is read-only. |
daaddde1 | 775 | |
b0c22438 | 776 | This variable was added in Perl 5.8. |
4c5cef9b | 777 | |
b0c22438 | 778 | =item ${^UNICODE} |
4c5cef9b | 779 | |
b0c22438 | 780 | Reflects certain Unicode settings of Perl. See L<perlrun> |
781 | documentation for the C<-C> switch for more information about | |
782 | the possible values. | |
5c055ba3 | 783 | |
b0c22438 | 784 | This variable is set during Perl startup and is thereafter read-only. |
5c055ba3 | 785 | |
b0c22438 | 786 | This variable was added in Perl 5.8.2. |
22fae026 | 787 | |
b0c22438 | 788 | =item ${^UTF8CACHE} |
22fae026 | 789 | |
b0c22438 | 790 | This variable controls the state of the internal UTF-8 offset caching code. |
791 | 1 for on (the default), 0 for off, -1 to debug the caching code by checking | |
792 | all its results against linear scans, and panicking on any discrepancy. | |
22fae026 | 793 | |
b0c22438 | 794 | This variable was added in Perl 5.8.9. |
22fae026 | 795 | |
b0c22438 | 796 | =item ${^UTF8LOCALE} |
5c055ba3 | 797 | |
b0c22438 | 798 | This variable indicates whether a UTF-8 locale was detected by perl at |
799 | startup. This information is used by perl when it's in | |
800 | adjust-utf8ness-to-locale mode (as when run with the C<-CL> command-line | |
801 | switch); see L<perlrun> for more info on this. | |
55602bd2 | 802 | |
b0c22438 | 803 | This variable was added in Perl 5.8.8. |
a0d0e21e | 804 | |
b0c22438 | 805 | =item $PERL_VERSION |
a0d0e21e | 806 | |
b0c22438 | 807 | =item $^V |
808 | X<$^V> X<$PERL_VERSION> | |
a0d0e21e | 809 | |
b0c22438 | 810 | The revision, version, and subversion of the Perl interpreter, |
811 | represented as a C<version> object. | |
748a9306 | 812 | |
b0c22438 | 813 | This variable first appeared in perl 5.6.0; earlier versions of perl |
814 | will see an undefined value. Before perl 5.10.0 C<$^V> was represented | |
815 | as a v-string. | |
55602bd2 | 816 | |
b0c22438 | 817 | C<$^V> can be used to determine whether the Perl interpreter executing |
818 | a script is in the right range of versions. For example: | |
a0d0e21e | 819 | |
b0c22438 | 820 | warn "Hashes not randomized!\n" if !$^V or $^V lt v5.8.1 |
a0d0e21e | 821 | |
b0c22438 | 822 | To convert C<$^V> into its string representation use C<sprintf()>'s |
823 | C<"%vd"> conversion: | |
a0d0e21e | 824 | |
b0c22438 | 825 | printf "version is v%vd\n", $^V; # Perl's version |
a0d0e21e | 826 | |
b0c22438 | 827 | See the documentation of C<use VERSION> and C<require VERSION> |
828 | for a convenient way to fail if the running Perl interpreter is too old. | |
4d76a344 | 829 | |
b0c22438 | 830 | See also C<$]> for an older representation of the Perl version. |
a0d0e21e | 831 | |
b0c22438 | 832 | This variable was added in Perl 5.6. |
a0d0e21e | 833 | |
b0c22438 | 834 | Mnemonic: use ^V for Version Control. |
a0d0e21e | 835 | |
b0c22438 | 836 | =item ${^WIN32_SLOPPY_STAT} |
837 | X<sitecustomize> X<sitecustomize.pl> | |
a0d0e21e | 838 | |
b0c22438 | 839 | If this variable is set to a true value, then C<stat()> on Windows will |
840 | not try to open the file. This means that the link count cannot be | |
841 | determined and file attributes may be out of date if additional | |
842 | hardlinks to the file exist. On the other hand, not opening the file | |
843 | is considerably faster, especially for files on network drives. | |
a0d0e21e | 844 | |
b0c22438 | 845 | This variable could be set in the F<sitecustomize.pl> file to |
846 | configure the local Perl installation to use "sloppy" C<stat()> by | |
847 | default. See the documentation for B<-f> in | |
848 | L<perlrun|perlrun/"Command Switches"> for more information about site | |
849 | customization. | |
a0d0e21e | 850 | |
b0c22438 | 851 | This variable was added in Perl 5.10. |
a0d0e21e | 852 | |
b0c22438 | 853 | =item $EXECUTABLE_NAME |
a0d0e21e | 854 | |
b0c22438 | 855 | =item $^X |
856 | X<$^X> X<$EXECUTABLE_NAME> | |
a0d0e21e | 857 | |
b0c22438 | 858 | The name used to execute the current copy of Perl, from C's |
859 | C<argv[0]> or (where supported) F</proc/self/exe>. | |
a043a685 | 860 | |
b0c22438 | 861 | Depending on the host operating system, the value of C<$^X> may be |
862 | a relative or absolute pathname of the perl program file, or may | |
863 | be the string used to invoke perl but not the pathname of the | |
864 | perl program file. Also, most operating systems permit invoking | |
865 | programs that are not in the PATH environment variable, so there | |
866 | is no guarantee that the value of C<$^X> is in PATH. For VMS, the | |
867 | value may or may not include a version number. | |
a0d0e21e | 868 | |
b0c22438 | 869 | You usually can use the value of C<$^X> to re-invoke an independent |
870 | copy of the same perl that is currently running, e.g., | |
a0d0e21e | 871 | |
b0c22438 | 872 | @first_run = `$^X -le "print int rand 100 for 1..100"`; |
a0d0e21e | 873 | |
b0c22438 | 874 | But recall that not all operating systems support forking or |
875 | capturing of the output of commands, so this complex statement | |
876 | may not be portable. | |
a0d0e21e | 877 | |
b0c22438 | 878 | It is not safe to use the value of C<$^X> as a path name of a file, |
879 | as some operating systems that have a mandatory suffix on | |
880 | executable files do not require use of the suffix when invoking | |
881 | a command. To convert the value of C<$^X> to a path name, use the | |
882 | following statements: | |
8cc95fdb | 883 | |
b0c22438 | 884 | # Build up a set of file names (not command names). |
885 | use Config; | |
886 | $this_perl = $^X; | |
887 | if ($^O ne 'VMS') | |
888 | {$this_perl .= $Config{_exe} | |
889 | unless $this_perl =~ m/$Config{_exe}$/i;} | |
8cc95fdb | 890 | |
b0c22438 | 891 | Because many operating systems permit anyone with read access to |
892 | the Perl program file to make a copy of it, patch the copy, and | |
893 | then execute the copy, the security-conscious Perl programmer | |
894 | should take care to invoke the installed copy of perl, not the | |
895 | copy referenced by C<$^X>. The following statements accomplish | |
896 | this goal, and produce a pathname that can be invoked as a | |
897 | command or referenced as a file. | |
a043a685 | 898 | |
b0c22438 | 899 | use Config; |
900 | $secure_perl_path = $Config{perlpath}; | |
901 | if ($^O ne 'VMS') | |
902 | {$secure_perl_path .= $Config{_exe} | |
903 | unless $secure_perl_path =~ m/$Config{_exe}$/i;} | |
a0d0e21e | 904 | |
b0c22438 | 905 | =back |
a0d0e21e | 906 | |
b0c22438 | 907 | =head2 Variables related to regular expressions |
908 | ||
909 | Most of the special variables related to regular expressions are side | |
910 | effects. Perl sets these variables when it has a successful match, so | |
911 | you should check the match result before using them. For instance: | |
912 | ||
913 | if( /P(A)TT(ER)N/ ) { | |
914 | print "I found $1 and $2\n"; | |
915 | } | |
916 | ||
917 | These variables are read-only and dynamically-scoped, unless we note | |
918 | otherwise. | |
919 | ||
920 | The dynamic nature of the regular expression variables means that their value | |
921 | is limited to the block that they are in, as demonstrated by this bit of code: | |
922 | ||
923 | my $outer = 'Wallace and Grommit'; | |
924 | my $inner = 'Mutt and Jeff'; | |
925 | ||
926 | my $pattern = qr/(\S+) and (\S+)/; | |
927 | ||
928 | sub show_n { print "\$1 is $1; \$2 is $2\n" } | |
929 | ||
930 | { | |
931 | OUTER: | |
932 | show_n() if $outer =~ m/$pattern/; | |
933 | ||
934 | INNER: { | |
935 | show_n() if $inner =~ m/$pattern/; | |
936 | } | |
937 | ||
938 | show_n(); | |
939 | } | |
940 | ||
941 | The output shows that while in the C<OUTER> block, the values of C<$1> and C<$2> | |
942 | are from the match against C<$outer>. Inside the C<INNER> block, the values of | |
943 | C<$1> and C<$2> are from the match against C<$inner>, but only until the end of the | |
944 | block (i.e. the dynamic scope). After the C<INNER> block completes, the values of | |
945 | C<$1> and C<$2> return to the values for the match against C<$outer> even though | |
946 | we have not made another match: | |
947 | ||
948 | $1 is Wallace; $2 is Grommit | |
949 | $1 is Mutt; $2 is Jeff | |
950 | $1 is Wallace; $2 is Grommit | |
a0d0e21e | 951 | |
b0c22438 | 952 | =over 8 |
a0d0e21e | 953 | |
b0c22438 | 954 | =item $<I<digits>> ($1, $2, ...) |
955 | X<$1> X<$2> X<$3> | |
8cc95fdb | 956 | |
b0c22438 | 957 | Contains the subpattern from the corresponding set of capturing |
958 | parentheses from the last successful pattern match, not counting patterns | |
959 | matched in nested blocks that have been exited already. | |
8cc95fdb | 960 | |
b0c22438 | 961 | These variables are read-only and dynamically-scoped. |
a043a685 | 962 | |
b0c22438 | 963 | Mnemonic: like \digits. |
a0d0e21e | 964 | |
b0c22438 | 965 | =item $MATCH |
a0d0e21e | 966 | |
b0c22438 | 967 | =item $& |
968 | X<$&> X<$MATCH> | |
a0d0e21e | 969 | |
b0c22438 | 970 | The string matched by the last successful pattern match (not counting |
971 | any matches hidden within a BLOCK or C<eval()> enclosed by the current | |
972 | BLOCK). | |
a0d0e21e | 973 | |
b0c22438 | 974 | The use of this variable anywhere in a program imposes a considerable |
975 | performance penalty on all regular expression matches. See L</BUGS>. | |
976 | To avoid this penatly, you can extract the same substring by | |
977 | using L</@->. Starting with Perl 5.10, you can use the </p> match flag | |
978 | and the C<${^MATCH}> variable to do the same thing for particular | |
979 | match operations. | |
80bca1b4 | 980 | |
b0c22438 | 981 | This variable is read-only and dynamically-scoped. |
f9cbb277 | 982 | |
b0c22438 | 983 | Mnemonic: like C<&> in some editors. |
984 | ||
985 | =item ${^MATCH} | |
986 | X<${^MATCH}> | |
a0d0e21e | 987 | |
b0c22438 | 988 | This is similar to C<$&> (C<$MATCH>) except that it does not incur the |
989 | performance penalty associated with that variable, and is only guaranteed | |
990 | to return a defined value when the pattern was compiled or executed with | |
991 | the C</p> modifier. | |
80bca1b4 | 992 | |
b0c22438 | 993 | This variable was added in Perl 5.10. |
4bc88a62 | 994 | |
b0c22438 | 995 | This variable is read-only and dynamically-scoped. |
e2975953 | 996 | |
b0c22438 | 997 | =item $PREMATCH |
52c447a8 | 998 | |
b0c22438 | 999 | =item $` |
1000 | X<$`> X<$PREMATCH> | |
7636ea95 | 1001 | |
b0c22438 | 1002 | The string preceding whatever was matched by the last successful |
1003 | pattern match, not counting any matches hidden within a BLOCK or C<eval> | |
1004 | enclosed by the current BLOCK. | |
a0d0e21e | 1005 | |
b0c22438 | 1006 | The use of this variable anywhere in a program imposes a considerable |
1007 | performance penalty on all regular expression matches. See L</BUGS>. | |
1008 | To avoid this penatly, you can extract the same substring by | |
1009 | using L</@->. Starting with Perl 5.10, you can use the </p> match flag | |
1010 | and the C<${^PREMATCH}> variable to do the same thing for particular | |
1011 | match operations. | |
a0d0e21e | 1012 | |
b0c22438 | 1013 | This variable is read-only and dynamically-scoped. |
a0d0e21e | 1014 | |
b0c22438 | 1015 | Mnemonic: C<`> often precedes a quoted string. |
f83ed198 | 1016 | |
b0c22438 | 1017 | =item ${^PREMATCH} |
1018 | X<${^PREMATCH}> | |
a0d0e21e | 1019 | |
b0c22438 | 1020 | This is similar to C<$`> ($PREMATCH) except that it does not incur the |
1021 | performance penalty associated with that variable, and is only guaranteed | |
1022 | to return a defined value when the pattern was compiled or executed with | |
1023 | the C</p> modifier. | |
a0d0e21e | 1024 | |
b0c22438 | 1025 | This variable was added in Perl 5.10 |
a0d0e21e | 1026 | |
b0c22438 | 1027 | This variable is read-only and dynamically-scoped. |
a0d0e21e | 1028 | |
b0c22438 | 1029 | =item $POSTMATCH |
16070b82 | 1030 | |
b0c22438 | 1031 | =item $' |
1032 | X<$'> X<$POSTMATCH> | |
305aace0 | 1033 | |
b0c22438 | 1034 | The string following whatever was matched by the last successful |
1035 | pattern match (not counting any matches hidden within a BLOCK or C<eval()> | |
1036 | enclosed by the current BLOCK). Example: | |
305aace0 | 1037 | |
b0c22438 | 1038 | local $_ = 'abcdefghi'; |
1039 | /def/; | |
1040 | print "$`:$&:$'\n"; # prints abc:def:ghi | |
305aace0 | 1041 | |
b0c22438 | 1042 | The use of this variable anywhere in a program imposes a considerable |
1043 | performance penalty on all regular expression matches. See L</BUGS>. | |
1044 | To avoid this penatly, you can extract the same substring by | |
1045 | using L</@->. Starting with Perl 5.10, you can use the </p> match flag | |
1046 | and the C<${^POSTMATCH}> variable to do the same thing for particular | |
1047 | match operations. | |
a0d0e21e | 1048 | |
b0c22438 | 1049 | This variable is read-only and dynamically-scoped. |
1050 | ||
1051 | Mnemonic: C<'> often follows a quoted string. | |
1052 | ||
1053 | =item ${^POSTMATCH} | |
1054 | X<${^POSTMATCH}> | |
1055 | ||
1056 | This is similar to C<$'> (C<$POSTMATCH>) except that it does not incur the | |
1057 | performance penalty associated with that variable, and is only guaranteed | |
1058 | to return a defined value when the pattern was compiled or executed with | |
1059 | the C</p> modifier. | |
1060 | ||
1061 | This variable was added in Perl 5.10. | |
1062 | ||
1063 | This variable is read-only and dynamically-scoped. | |
1064 | ||
1065 | =item $LAST_PAREN_MATCH | |
1066 | ||
1067 | =item $+ | |
1068 | X<$+> X<$LAST_PAREN_MATCH> | |
1069 | ||
1070 | The text matched by the last bracket of the last successful search pattern. | |
1071 | This is useful if you don't know which one of a set of alternative patterns | |
1072 | matched. For example: | |
1073 | ||
1074 | /Version: (.*)|Revision: (.*)/ && ($rev = $+); | |
1075 | ||
1076 | This variable is read-only and dynamically-scoped. | |
1077 | ||
1078 | Mnemonic: be positive and forward looking. | |
1079 | ||
1080 | =item $LAST_SUBMATCH_RESULT | |
1081 | ||
1082 | =item $^N | |
1083 | X<$^N> | |
1084 | ||
1085 | The text matched by the used group most-recently closed (i.e. the group | |
1086 | with the rightmost closing parenthesis) of the last successful search | |
1087 | pattern. | |
1088 | ||
1089 | This is primarily used inside C<(?{...})> blocks for examining text | |
1090 | recently matched. For example, to effectively capture text to a variable | |
1091 | (in addition to C<$1>, C<$2>, etc.), replace C<(...)> with | |
1092 | ||
1093 | (?:(...)(?{ $var = $^N })) | |
1094 | ||
1095 | By setting and then using C<$var> in this way relieves you from having to | |
1096 | worry about exactly which numbered set of parentheses they are. | |
1097 | ||
1098 | This variable was added in Perl 5.8. | |
1099 | ||
1100 | Mnemonic: the (possibly) Nested parenthesis that most recently closed. | |
1101 | ||
1102 | =item @LAST_MATCH_END | |
1103 | ||
1104 | =item @+ | |
1105 | X<@+> X<@LAST_MATCH_END> | |
1106 | ||
1107 | This array holds the offsets of the ends of the last successful | |
1108 | submatches in the currently active dynamic scope. C<$+[0]> is | |
1109 | the offset into the string of the end of the entire match. This | |
1110 | is the same value as what the C<pos> function returns when called | |
1111 | on the variable that was matched against. The I<n>th element | |
1112 | of this array holds the offset of the I<n>th submatch, so | |
1113 | C<$+[1]> is the offset past where C<$1> ends, C<$+[2]> the offset | |
1114 | past where C<$2> ends, and so on. You can use C<$#+> to determine | |
1115 | how many subgroups were in the last successful match. See the | |
1116 | examples given for the C<@-> variable. | |
1117 | ||
1118 | This variable was added in Perl 5.6. | |
1119 | ||
1120 | =item %LAST_PAREN_MATCH | |
1121 | ||
1122 | =item %+ | |
1123 | X<%+> | |
1124 | ||
1125 | Similar to C<@+>, the C<%+> hash allows access to the named capture | |
1126 | buffers, should they exist, in the last successful match in the | |
1127 | currently active dynamic scope. | |
1128 | ||
1129 | For example, C<$+{foo}> is equivalent to C<$1> after the following match: | |
1130 | ||
1131 | 'foo' =~ /(?<foo>foo)/; | |
1132 | ||
1133 | The keys of the C<%+> hash list only the names of buffers that have | |
1134 | captured (and that are thus associated to defined values). | |
1135 | ||
1136 | The underlying behaviour of C<%+> is provided by the | |
1137 | L<Tie::Hash::NamedCapture> module. | |
1138 | ||
1139 | B<Note:> C<%-> and C<%+> are tied views into a common internal hash | |
1140 | associated with the last successful regular expression. Therefore mixing | |
1141 | iterative access to them via C<each> may have unpredictable results. | |
1142 | Likewise, if the last successful match changes, then the results may be | |
1143 | surprising. | |
1144 | ||
1145 | This variable was added in Perl 5.10. | |
a0d0e21e | 1146 | |
b0c22438 | 1147 | This variable is read-only and dynamically-scoped. |
1148 | ||
1149 | =item @LAST_MATCH_START | |
1150 | ||
1151 | =item @- | |
1152 | X<@-> X<@LAST_MATCH_START> | |
1153 | ||
1154 | C<$-[0]> is the offset of the start of the last successful match. | |
1155 | C<$-[>I<n>C<]> is the offset of the start of the substring matched by | |
1156 | I<n>-th subpattern, or undef if the subpattern did not match. | |
1157 | ||
1158 | Thus, after a match against C<$_>, C<$&> coincides with C<substr $_, $-[0], | |
1159 | $+[0] - $-[0]>. Similarly, $I<n> coincides with C<substr $_, $-[n], | |
1160 | $+[n] - $-[n]> if C<$-[n]> is defined, and $+ coincides with | |
1161 | C<substr $_, $-[$#-], $+[$#-] - $-[$#-]>. One can use C<$#-> to find the last | |
1162 | matched subgroup in the last successful match. Contrast with | |
1163 | C<$#+>, the number of subgroups in the regular expression. Compare | |
1164 | with C<@+>. | |
1165 | ||
1166 | This array holds the offsets of the beginnings of the last | |
1167 | successful submatches in the currently active dynamic scope. | |
1168 | C<$-[0]> is the offset into the string of the beginning of the | |
1169 | entire match. The I<n>th element of this array holds the offset | |
1170 | of the I<n>th submatch, so C<$-[1]> is the offset where C<$1> | |
1171 | begins, C<$-[2]> the offset where C<$2> begins, and so on. | |
1172 | ||
1173 | After a match against some variable C<$var>: | |
1174 | ||
1175 | =over 5 | |
1176 | ||
1177 | =item C<$`> is the same as C<substr($var, 0, $-[0])> | |
1178 | ||
1179 | =item C<$&> is the same as C<substr($var, $-[0], $+[0] - $-[0])> | |
1180 | ||
1181 | =item C<$'> is the same as C<substr($var, $+[0])> | |
1182 | ||
1183 | =item C<$1> is the same as C<substr($var, $-[1], $+[1] - $-[1])> | |
1184 | ||
1185 | =item C<$2> is the same as C<substr($var, $-[2], $+[2] - $-[2])> | |
1186 | ||
1187 | =item C<$3> is the same as C<substr($var, $-[3], $+[3] - $-[3])> | |
1188 | ||
1189 | =back | |
1190 | ||
1191 | This variable was added in Perl 5.6. | |
1192 | ||
1193 | =item %- | |
1194 | X<%-> | |
1195 | ||
1196 | Similar to C<%+>, this variable allows access to the named capture groups | |
1197 | in the last successful match in the currently active dynamic scope. To | |
1198 | each capture group name found in the regular expression, it associates a | |
1199 | reference to an array containing the list of values captured by all | |
1200 | buffers with that name (should there be several of them), in the order | |
1201 | where they appear. | |
1202 | ||
1203 | Here's an example: | |
1204 | ||
1205 | if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) { | |
1206 | foreach my $bufname (sort keys %-) { | |
1207 | my $ary = $-{$bufname}; | |
1208 | foreach my $idx (0..$#$ary) { | |
1209 | print "\$-{$bufname}[$idx] : ", | |
1210 | (defined($ary->[$idx]) ? "'$ary->[$idx]'" : "undef"), | |
1211 | "\n"; | |
1212 | } | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | would print out: | |
1217 | ||
1218 | $-{A}[0] : '1' | |
1219 | $-{A}[1] : '3' | |
1220 | $-{B}[0] : '2' | |
1221 | $-{B}[1] : '4' | |
1222 | ||
1223 | The keys of the C<%-> hash correspond to all buffer names found in | |
1224 | the regular expression. | |
1225 | ||
1226 | The behaviour of C<%-> is implemented via the | |
1227 | L<Tie::Hash::NamedCapture> module. | |
1228 | ||
1229 | B<Note:> C<%-> and C<%+> are tied views into a common internal hash | |
1230 | associated with the last successful regular expression. Therefore mixing | |
1231 | iterative access to them via C<each> may have unpredictable results. | |
1232 | Likewise, if the last successful match changes, then the results may be | |
1233 | surprising. | |
1234 | ||
1235 | This variable was added in Perl 5.10 | |
1236 | ||
1237 | This variable is read-only and dynamically-scoped. | |
1238 | ||
1239 | =item $LAST_REGEXP_CODE_RESULT | |
1240 | ||
1241 | =item $^R | |
1242 | X<$^R> X<$LAST_REGEXP_CODE_RESULT> | |
1243 | ||
1244 | The result of evaluation of the last successful C<(?{ code })> | |
1245 | regular expression assertion (see L<perlre>). May be written to. | |
1246 | ||
1247 | This variable was added in Perl 5.005. | |
a0d0e21e | 1248 | |
a3621e74 YO |
1249 | =item ${^RE_DEBUG_FLAGS} |
1250 | ||
1251 | The current value of the regex debugging flags. Set to 0 for no debug output | |
b0c22438 | 1252 | even when the C<re 'debug'> module is loaded. See L<re> for details. |
1253 | ||
1254 | This variable was added in Perl 5.10. | |
a3621e74 | 1255 | |
0111c4fd | 1256 | =item ${^RE_TRIE_MAXBUF} |
a3621e74 YO |
1257 | |
1258 | Controls how certain regex optimisations are applied and how much memory they | |
1259 | utilize. This value by default is 65536 which corresponds to a 512kB temporary | |
1260 | cache. Set this to a higher value to trade memory for speed when matching | |
1261 | large alternations. Set it to a lower value if you want the optimisations to | |
1262 | be as conservative of memory as possible but still occur, and set it to a | |
1263 | negative value to prevent the optimisation and conserve the most memory. | |
1264 | Under normal situations this variable should be of no interest to you. | |
1265 | ||
b0c22438 | 1266 | This variable was added in Perl 5.10. |
a0d0e21e | 1267 | |
b0c22438 | 1268 | =back |
a0d0e21e | 1269 | |
b0c22438 | 1270 | =head2 Variables related to filehandles |
a0d0e21e | 1271 | |
b0c22438 | 1272 | Variables that depend on the currently selected filehandle may be set |
1273 | by calling an appropriate object method on the C<IO::Handle> object, | |
1274 | although this is less efficient than using the regular built-in | |
1275 | variables. (Summary lines below for this contain the word HANDLE.) | |
1276 | First you must say | |
6e2995f4 | 1277 | |
b0c22438 | 1278 | use IO::Handle; |
0462a1ab | 1279 | |
b0c22438 | 1280 | after which you may use either |
0462a1ab | 1281 | |
b0c22438 | 1282 | method HANDLE EXPR |
0462a1ab | 1283 | |
b0c22438 | 1284 | or more safely, |
0462a1ab | 1285 | |
b0c22438 | 1286 | HANDLE->method(EXPR) |
0462a1ab | 1287 | |
b0c22438 | 1288 | Each method returns the old value of the C<IO::Handle> attribute. The |
1289 | methods each take an optional EXPR, which, if supplied, specifies the | |
1290 | new value for the C<IO::Handle> attribute in question. If not | |
1291 | supplied, most methods do nothing to the current value--except for | |
1292 | C<autoflush()>, which will assume a 1 for you, just to be different. | |
0462a1ab | 1293 | |
b0c22438 | 1294 | Because loading in the C<IO::Handle> class is an expensive operation, |
1295 | you should learn how to use the regular built-in variables. | |
1296 | ||
1297 | A few of these variables are considered "read-only". This means that | |
1298 | if you try to assign to this variable, either directly or indirectly | |
1299 | through a reference, you'll raise a run-time exception. | |
1300 | ||
1301 | You should be very careful when modifying the default values of most | |
1302 | special variables described in this document. In most cases you want | |
1303 | to localize these variables before changing them, since if you don't, | |
1304 | the change may affect other modules which rely on the default values | |
1305 | of the special variables that you have changed. This is one of the | |
1306 | correct ways to read the whole file at once: | |
1307 | ||
1308 | open my $fh, "<", "foo" or die $!; | |
1309 | local $/; # enable localized slurp mode | |
1310 | my $content = <$fh>; | |
1311 | close $fh; | |
1312 | ||
1313 | But the following code is quite bad: | |
1314 | ||
1315 | open my $fh, "<", "foo" or die $!; | |
1316 | undef $/; # enable slurp mode | |
1317 | my $content = <$fh>; | |
1318 | close $fh; | |
1319 | ||
1320 | since some other module, may want to read data from some file in the | |
1321 | default "line mode", so if the code we have just presented has been | |
1322 | executed, the global value of C<$/> is now changed for any other code | |
1323 | running inside the same Perl interpreter. | |
1324 | ||
1325 | Usually when a variable is localized you want to make sure that this | |
1326 | change affects the shortest scope possible. So unless you are already | |
1327 | inside some short C<{}> block, you should create one yourself. For | |
1328 | example: | |
1329 | ||
1330 | my $content = ''; | |
1331 | open my $fh, "<", "foo" or die $!; | |
1332 | { | |
1333 | local $/; | |
1334 | $content = <$fh>; | |
0462a1ab | 1335 | } |
b0c22438 | 1336 | close $fh; |
0462a1ab | 1337 | |
b0c22438 | 1338 | Here is an example of how your own code can go broken: |
0462a1ab | 1339 | |
b0c22438 | 1340 | for (1..5){ |
1341 | nasty_break(); | |
1342 | print "$_ "; | |
1343 | } | |
1344 | sub nasty_break { | |
1345 | $_ = 5; | |
1346 | # do something with $_ | |
1347 | } | |
0462a1ab | 1348 | |
b0c22438 | 1349 | You probably expect this code to print: |
0462a1ab | 1350 | |
b0c22438 | 1351 | 1 2 3 4 5 |
0462a1ab | 1352 | |
b0c22438 | 1353 | but instead you get: |
0462a1ab | 1354 | |
b0c22438 | 1355 | 5 5 5 5 5 |
0462a1ab | 1356 | |
b0c22438 | 1357 | Why? Because C<nasty_break()> modifies C<$_> without localizing it |
1358 | first. The fix is to add C<local()>: | |
6e2995f4 | 1359 | |
b0c22438 | 1360 | local $_ = 5; |
a0d0e21e | 1361 | |
b0c22438 | 1362 | It's easy to notice the problem in such a short example, but in more |
1363 | complicated code you are looking for trouble if you don't localize | |
1364 | changes to the special variables. | |
a0d0e21e | 1365 | |
b0c22438 | 1366 | =over 8 |
a0d0e21e | 1367 | |
b0c22438 | 1368 | =item $ARGV |
1369 | X<$ARGV> | |
fb73857a | 1370 | |
b0c22438 | 1371 | contains the name of the current file when reading from <>. |
1372 | ||
1373 | =item @ARGV | |
1374 | X<@ARGV> | |
1375 | ||
1376 | The array @ARGV contains the command-line arguments intended for | |
1377 | the script. C<$#ARGV> is generally the number of arguments minus | |
1378 | one, because C<$ARGV[0]> is the first argument, I<not> the program's | |
1379 | command name itself. See C<$0> for the command name. | |
1380 | ||
84dabc03 | 1381 | =item ARGV |
1382 | X<ARGV> | |
1383 | ||
1384 | The special filehandle that iterates over command-line filenames in | |
1385 | C<@ARGV>. Usually written as the null filehandle in the angle operator | |
1386 | C<< <> >>. Note that currently C<ARGV> only has its magical effect | |
1387 | within the C<< <> >> operator; elsewhere it is just a plain filehandle | |
1388 | corresponding to the last file opened by C<< <> >>. In particular, | |
1389 | passing C<\*ARGV> as a parameter to a function that expects a filehandle | |
1390 | may not cause your function to automatically read the contents of all the | |
1391 | files in C<@ARGV>. | |
1392 | ||
b0c22438 | 1393 | =item ARGVOUT |
1394 | X<ARGVOUT> | |
1395 | ||
1396 | The special filehandle that points to the currently open output file | |
1397 | when doing edit-in-place processing with B<-i>. Useful when you have | |
1398 | to do a lot of inserting and don't want to keep modifying C<$_>. See | |
1399 | L<perlrun> for the B<-i> switch. | |
1400 | ||
84dabc03 | 1401 | =item Handle->output_field_separator EXPR |
1402 | ||
1403 | =item $OUTPUT_FIELD_SEPARATOR | |
1404 | ||
1405 | =item $OFS | |
1406 | ||
1407 | =item $, | |
1408 | X<$,> X<$OFS> X<$OUTPUT_FIELD_SEPARATOR> | |
1409 | ||
1410 | The output field separator for the print operator. If defined, this | |
1411 | value is printed between each of print's arguments. Default is C<undef>. | |
1412 | ||
1413 | Mnemonic: what is printed when there is a "," in your print statement. | |
1414 | ||
b0c22438 | 1415 | =item HANDLE->input_line_number(EXPR) |
1416 | ||
1417 | =item $INPUT_LINE_NUMBER | |
1418 | ||
1419 | =item $NR | |
1420 | ||
1421 | =item $. | |
1422 | X<$.> X<$NR> X<$INPUT_LINE_NUMBER> X<line number> | |
1423 | ||
1424 | Current line number for the last filehandle accessed. | |
1425 | ||
1426 | Each filehandle in Perl counts the number of lines that have been read | |
1427 | from it. (Depending on the value of C<$/>, Perl's idea of what | |
1428 | constitutes a line may not match yours.) When a line is read from a | |
1429 | filehandle (via C<readline()> or C<< <> >>), or when C<tell()> or | |
1430 | C<seek()> is called on it, C<$.> becomes an alias to the line counter | |
1431 | for that filehandle. | |
1432 | ||
1433 | You can adjust the counter by assigning to C<$.>, but this will not | |
1434 | actually move the seek pointer. I<Localizing C<$.> will not localize | |
1435 | the filehandle's line count>. Instead, it will localize perl's notion | |
1436 | of which filehandle C<$.> is currently aliased to. | |
1437 | ||
1438 | C<$.> is reset when the filehandle is closed, but B<not> when an open | |
1439 | filehandle is reopened without an intervening C<close()>. For more | |
1440 | details, see L<perlop/"IE<sol>O Operators">. Because C<< <> >> never does | |
1441 | an explicit close, line numbers increase across C<ARGV> files (but see | |
1442 | examples in L<perlfunc/eof>). | |
1443 | ||
1444 | You can also use C<< HANDLE->input_line_number(EXPR) >> to access the | |
1445 | line counter for a given filehandle without having to worry about | |
1446 | which handle you last accessed. | |
1447 | ||
1448 | Mnemonic: many programs use "." to mean the current line number. | |
1449 | ||
84dabc03 | 1450 | =item HANDLE->input_record_separator(EXPR) |
b0c22438 | 1451 | |
1452 | =item $INPUT_RECORD_SEPARATOR | |
1453 | ||
1454 | =item $RS | |
1455 | ||
1456 | =item $/ | |
1457 | X<$/> X<$RS> X<$INPUT_RECORD_SEPARATOR> | |
1458 | ||
84dabc03 | 1459 | The input record separator, newline by default. This influences Perl's |
1460 | idea of what a "line" is. Works like B<awk>'s RS variable, including | |
1461 | treating empty lines as a terminator if set to the null string (an | |
1462 | empty line cannot contain any spaces or tabs). You may set it to a | |
1463 | multi-character string to match a multi-character terminator, or to | |
1464 | C<undef> to read through the end of file. Setting it to C<"\n\n"> | |
1465 | means something slightly different than setting to C<"">, if the file | |
1466 | contains consecutive empty lines. Setting to C<""> will treat two or | |
1467 | more consecutive empty lines as a single empty line. Setting to | |
1468 | C<"\n\n"> will blindly assume that the next input character belongs to | |
1469 | the next paragraph, even if it's a newline. | |
b0c22438 | 1470 | |
1471 | local $/; # enable "slurp" mode | |
1472 | local $_ = <FH>; # whole file now here | |
1473 | s/\n[ \t]+/ /g; | |
1474 | ||
1475 | Remember: the value of C<$/> is a string, not a regex. B<awk> has to | |
1476 | be better for something. :-) | |
1477 | ||
1478 | Setting C<$/> to a reference to an integer, scalar containing an | |
1479 | integer, or scalar that's convertible to an integer will attempt to | |
1480 | read records instead of lines, with the maximum record size being the | |
1481 | referenced integer. So this: | |
1482 | ||
1483 | local $/ = \32768; # or \"32768", or \$var_containing_32768 | |
1484 | open my $fh, "<", $myfile or die $!; | |
1485 | local $_ = <$fh>; | |
fb73857a | 1486 | |
b0c22438 | 1487 | will read a record of no more than 32768 bytes from FILE. If you're |
1488 | not reading from a record-oriented file (or your OS doesn't have | |
1489 | record-oriented files), then you'll likely get a full chunk of data | |
1490 | with every read. If a record is larger than the record size you've | |
1491 | set, you'll get the record back in pieces. Trying to set the record | |
1492 | size to zero or less will cause reading in the (rest of the) whole file. | |
6e2995f4 | 1493 | |
b0c22438 | 1494 | On VMS, record reads are done with the equivalent of C<sysread>, |
1495 | so it's best not to mix record and non-record reads on the same | |
1496 | file. (This is unlikely to be a problem, because any file you'd | |
1497 | want to read in record mode is probably unusable in line mode.) | |
1498 | Non-VMS systems do normal I/O, so it's safe to mix record and | |
1499 | non-record reads of a file. | |
5c055ba3 | 1500 | |
b0c22438 | 1501 | See also L<perlport/"Newlines">. Also see C<$.>. |
9bf22702 | 1502 | |
b0c22438 | 1503 | Mnemonic: / delimits line boundaries when quoting poetry. |
5c055ba3 | 1504 | |
b0c22438 | 1505 | =item HANDLE->autoflush(EXPR) |
916d64a3 | 1506 | |
b0c22438 | 1507 | =item $OUTPUT_AUTOFLUSH |
e2e27056 | 1508 | |
84dabc03 | 1509 | =item Handle->output_record_separator EXPR |
84902520 | 1510 | |
b0c22438 | 1511 | =item $OUTPUT_RECORD_SEPARATOR |
84902520 | 1512 | |
b0c22438 | 1513 | =item $ORS |
84902520 | 1514 | |
b0c22438 | 1515 | =item $\ |
1516 | X<$\> X<$ORS> X<$OUTPUT_RECORD_SEPARATOR> | |
84902520 | 1517 | |
b0c22438 | 1518 | The output record separator for the print operator. If defined, this |
1519 | value is printed after the last of print's arguments. Default is C<undef>. | |
84902520 | 1520 | |
b0c22438 | 1521 | Mnemonic: you set C<$\> instead of adding "\n" at the end of the print. |
1522 | Also, it's just like C<$/>, but it's what you get "back" from Perl. | |
84902520 | 1523 | |
84dabc03 | 1524 | =item $| |
1525 | X<$|> X<autoflush> X<flush> X<$OUTPUT_AUTOFLUSH> | |
84902520 | 1526 | |
84dabc03 | 1527 | If set to nonzero, forces a flush right away and after every write or |
1528 | print on the currently selected output channel. Default is 0 | |
1529 | (regardless of whether the channel is really buffered by the system or | |
1530 | not; C<$|> tells you only whether you've asked Perl explicitly to | |
1531 | flush after each write). STDOUT will typically be line buffered if | |
1532 | output is to the terminal and block buffered otherwise. Setting this | |
1533 | variable is useful primarily when you are outputting to a pipe or | |
1534 | socket, such as when you are running a Perl program under B<rsh> and | |
1535 | want to see the output as it's happening. This has no effect on input | |
1536 | buffering. See L<perlfunc/getc> for that. See L<perldoc/select> on | |
1537 | how to select the output channel. See also L<IO::Handle>. | |
1538 | ||
1539 | Mnemonic: when you want your pipes to be piping hot. | |
1540 | ||
1541 | =back | |
84902520 | 1542 | |
b0c22438 | 1543 | =head3 Variables related to formats |
83ee9e09 | 1544 | |
b0c22438 | 1545 | The special variables for formats are a subset of those for |
1546 | filehandles so they have | |
83ee9e09 | 1547 | |
b0c22438 | 1548 | See L<perlform> for more information about Perl's formats. |
83ee9e09 | 1549 | |
b0c22438 | 1550 | =over 8 |
83ee9e09 | 1551 | |
84dabc03 | 1552 | =item HANDLE->format_formfeed(EXPR) |
1553 | ||
1554 | =item $FORMAT_FORMFEED | |
1555 | ||
1556 | =item $ACCUMULATOR | |
1557 | ||
1558 | =item $^A | |
1559 | X<$^A> X<$ACCUMULATOR> | |
1560 | ||
1561 | The current value of the C<write()> accumulator for C<format()> lines. | |
1562 | A format contains C<formline()> calls that put their result into | |
1563 | C<$^A>. After calling its format, C<write()> prints out the contents | |
1564 | of C<$^A> and empties. So you never really see the contents of C<$^A> | |
1565 | unless you call C<formline()> yourself and then look at it. See | |
1566 | L<perlform> and L<perlfunc/formline()>. | |
1567 | ||
1568 | =item $^L | |
1569 | X<$^L> X<$FORMAT_FORMFEED> | |
1570 | ||
1571 | What formats output as a form feed. The default is C<\f>. | |
1572 | ||
b0c22438 | 1573 | =item HANDLE->format_page_number(EXPR) |
83ee9e09 | 1574 | |
b0c22438 | 1575 | =item $FORMAT_PAGE_NUMBER |
83ee9e09 | 1576 | |
b0c22438 | 1577 | =item $% |
1578 | X<$%> X<$FORMAT_PAGE_NUMBER> | |
83ee9e09 | 1579 | |
b0c22438 | 1580 | The current page number of the currently selected output channel. |
83ee9e09 | 1581 | |
b0c22438 | 1582 | Mnemonic: C<%> is page number in B<nroff>. |
7619c85e | 1583 | |
b0c22438 | 1584 | =item HANDLE->format_lines_left(EXPR) |
b9ac3b5b | 1585 | |
b0c22438 | 1586 | =item $FORMAT_LINES_LEFT |
66558a10 | 1587 | |
b0c22438 | 1588 | =item $- |
1589 | X<$-> X<$FORMAT_LINES_LEFT> | |
fb73857a | 1590 | |
b0c22438 | 1591 | The number of lines left on the page of the currently selected output |
1592 | channel. | |
fa05a9fd | 1593 | |
b0c22438 | 1594 | Mnemonic: lines_on_page - lines_printed. |
fa05a9fd | 1595 | |
84dabc03 | 1596 | =item Handle->format_line_break_characters EXPR |
fb73857a | 1597 | |
84dabc03 | 1598 | =item $FORMAT_LINE_BREAK_CHARACTERS |
a0d0e21e | 1599 | |
84dabc03 | 1600 | =item $: |
1601 | X<$:> X<FORMAT_LINE_BREAK_CHARACTERS> | |
a0d0e21e | 1602 | |
84dabc03 | 1603 | The current set of characters after which a string may be broken to |
1604 | fill continuation fields (starting with C<^>) in a format. The default is | |
1605 | S<" \n-">, to break on a space, newline, or a hyphen. | |
a0d0e21e | 1606 | |
84dabc03 | 1607 | Mnemonic: a "colon" in poetry is a part of a line. |
1608 | ||
1609 | =item HANDLE->format_lines_per_page(EXPR) | |
1610 | ||
1611 | =item $FORMAT_LINES_PER_PAGE | |
1612 | ||
1613 | =item $= | |
1614 | X<$=> X<$FORMAT_LINES_PER_PAGE> | |
1615 | ||
1616 | The current page length (printable lines) of the currently selected | |
1617 | output channel. The default is 60. | |
1618 | ||
1619 | Mnemonic: = has horizontal lines. | |
7c36658b | 1620 | |
b0c22438 | 1621 | =item HANDLE->format_top_name(EXPR) |
7c36658b | 1622 | |
b0c22438 | 1623 | =item $FORMAT_TOP_NAME |
a05d7ebb | 1624 | |
b0c22438 | 1625 | =item $^ |
1626 | X<$^> X<$FORMAT_TOP_NAME> | |
fde18df1 | 1627 | |
b0c22438 | 1628 | The name of the current top-of-page format for the currently selected |
1629 | output channel. The default is the name of the filehandle with C<_TOP> | |
1630 | appended. For example, the default format top name for the C<STDOUT> | |
1631 | filehanlde is C<STDOUT_TOP>. | |
e07ea26a | 1632 | |
b0c22438 | 1633 | Mnemonic: points to top of page. |
e07ea26a | 1634 | |
84dabc03 | 1635 | =item HANDLE->format_name(EXPR) |
16070b82 | 1636 | |
84dabc03 | 1637 | =item $FORMAT_NAME |
aa2f2a36 | 1638 | |
84dabc03 | 1639 | =item $~ |
1640 | X<$~> X<$FORMAT_NAME> | |
aa2f2a36 | 1641 | |
84dabc03 | 1642 | The name of the current report format for the currently selected |
1643 | output channel. The default format name is the same as the filehandle | |
1644 | name. For example, the default format name for the C<STDOUT> | |
1645 | filehandle is just C<STDOUT>. | |
16070b82 | 1646 | |
84dabc03 | 1647 | Mnemonic: brother to C<$^>. |
16070b82 | 1648 | |
b0c22438 | 1649 | =back |
a0d0e21e | 1650 | |
84dabc03 | 1651 | =head2 Error Variables |
b0c22438 | 1652 | X<error> X<exception> |
a0d0e21e | 1653 | |
b0c22438 | 1654 | The variables C<$@>, C<$!>, C<$^E>, and C<$?> contain information |
1655 | about different types of error conditions that may appear during | |
1656 | execution of a Perl program. The variables are shown ordered by | |
1657 | the "distance" between the subsystem which reported the error and | |
1658 | the Perl process. They correspond to errors detected by the Perl | |
1659 | interpreter, C library, operating system, or an external program, | |
1660 | respectively. | |
4438c4b7 | 1661 | |
b0c22438 | 1662 | To illustrate the differences between these variables, consider the |
1663 | following Perl expression, which uses a single-quoted string: | |
4438c4b7 | 1664 | |
b0c22438 | 1665 | eval q{ |
1666 | open my $pipe, "/cdrom/install |" or die $!; | |
1667 | my @res = <$pipe>; | |
1668 | close $pipe or die "bad pipe: $?, $!"; | |
1669 | }; | |
a0d0e21e | 1670 | |
b0c22438 | 1671 | After execution of this statement all 4 variables may have been set. |
2a8c8378 | 1672 | |
84dabc03 | 1673 | C<$@> is set if the string to be C<eval>-ed did not compile (this may |
1674 | happen if C<open> or C<close> were imported with bad prototypes), or | |
1675 | if Perl code executed during evaluation die()d . In these cases the | |
1676 | value of $@ is the compile error, or the argument to C<die> (which | |
1677 | will interpolate C<$!> and C<$?>). (See also L<Fatal>, though.) | |
2a8c8378 | 1678 | |
84dabc03 | 1679 | When the C<eval()> expression above is executed, C<open()>, C<< <PIPE> |
1680 | >>, and C<close> are translated to calls in the C run-time library and | |
b0c22438 | 1681 | thence to the operating system kernel. C<$!> is set to the C library's |
1682 | C<errno> if one of these calls fails. | |
2a8c8378 | 1683 | |
84dabc03 | 1684 | Under a few operating systems, C<$^E> may contain a more verbose error |
1685 | indicator, such as in this case, "CDROM tray not closed." Systems that | |
1686 | do not support extended error messages leave C<$^E> the same as C<$!>. | |
a0d0e21e | 1687 | |
b0c22438 | 1688 | Finally, C<$?> may be set to non-0 value if the external program |
84dabc03 | 1689 | F</cdrom/install> fails. The upper eight bits reflect specific error |
1690 | conditions encountered by the program (the program's C<exit()> value). | |
1691 | The lower eight bits reflect mode of failure, like signal death and | |
1692 | core dump information. See C<wait(2)> for details. In contrast to | |
1693 | C<$!> and C<$^E>, which are set only if error condition is detected, | |
1694 | the variable C<$?> is set on each C<wait> or pipe C<close>, | |
1695 | overwriting the old value. This is more like C<$@>, which on every | |
1696 | C<eval()> is always set on failure and cleared on success. | |
a0d0e21e | 1697 | |
b0c22438 | 1698 | For more details, see the individual descriptions at C<$@>, C<$!>, |
1699 | C<$^E>, and C<$?>. | |
38e4f4ae | 1700 | |
84dabc03 | 1701 | =item $EXTENDED_OS_ERROR |
2d84a16a | 1702 | |
b0c22438 | 1703 | =item ${^CHILD_ERROR_NATIVE} |
1704 | X<$^CHILD_ERROR_NATIVE> | |
a0d0e21e | 1705 | |
b0c22438 | 1706 | The native status returned by the last pipe close, backtick (C<``>) |
1707 | command, successful call to C<wait()> or C<waitpid()>, or from the | |
1708 | C<system()> operator. On POSIX-like systems this value can be decoded | |
1709 | with the WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WIFSTOPPED, | |
1710 | WSTOPSIG and WIFCONTINUED functions provided by the L<POSIX> module. | |
a0d0e21e | 1711 | |
b0c22438 | 1712 | Under VMS this reflects the actual VMS exit status; i.e. it is the |
1713 | same as C<$?> when the pragma C<use vmsish 'status'> is in effect. | |
a0d0e21e | 1714 | |
b0c22438 | 1715 | This variable was added in Perl 5.8.9. |
a0d0e21e | 1716 | |
84dabc03 | 1717 | =item $^E |
1718 | X<$^E> X<$EXTENDED_OS_ERROR> | |
1719 | ||
1720 | Error information specific to the current operating system. At the | |
1721 | moment, this differs from C<$!> under only VMS, OS/2, and Win32 (and | |
1722 | for MacPerl). On all other platforms, C<$^E> is always just the same | |
1723 | as C<$!>. | |
1724 | ||
1725 | Under VMS, C<$^E> provides the VMS status value from the last system | |
1726 | error. This is more specific information about the last system error | |
1727 | than that provided by C<$!>. This is particularly important when C<$!> | |
1728 | is set to B<EVMSERR>. | |
1729 | ||
1730 | Under OS/2, C<$^E> is set to the error code of the last call to OS/2 | |
1731 | API either via CRT, or directly from perl. | |
1732 | ||
1733 | Under Win32, C<$^E> always returns the last error information reported | |
1734 | by the Win32 call C<GetLastError()> which describes the last error | |
1735 | from within the Win32 API. Most Win32-specific code will report errors | |
1736 | via C<$^E>. ANSI C and Unix-like calls set C<errno> and so most | |
1737 | portable Perl code will report errors via C<$!>. | |
1738 | ||
1739 | Caveats mentioned in the description of C<$!> generally apply to | |
1740 | C<$^E>, also. | |
1741 | ||
1742 | This variable was added in Perl 5.003. | |
1743 | ||
1744 | Mnemonic: Extra error explanation. | |
1745 | ||
1746 | =item $EXCEPTIONS_BEING_CAUGHT | |
1747 | ||
1748 | =item $^S | |
1749 | X<$^S> X<$EXCEPTIONS_BEING_CAUGHT> | |
1750 | ||
1751 | Current state of the interpreter. | |
1752 | ||
1753 | $^S State | |
1754 | --------- ------------------- | |
1755 | undef Parsing module/eval | |
1756 | true (1) Executing an eval | |
1757 | false (0) Otherwise | |
1758 | ||
1759 | The first state may happen in C<$SIG{__DIE__}> and C<$SIG{__WARN__}> | |
1760 | handlers. | |
1761 | ||
1762 | This variable was added in Perl 5.004. | |
1763 | ||
1764 | =item $WARNING | |
1765 | ||
1766 | =item $^W | |
1767 | X<$^W> X<$WARNING> | |
1768 | ||
1769 | The current value of the warning switch, initially true if B<-w> was | |
1770 | used, false otherwise, but directly modifiable. | |
1771 | ||
1772 | See also L<warnings>. | |
1773 | ||
1774 | Mnemonic: related to the B<-w> switch. | |
1775 | ||
1776 | =item ${^WARNING_BITS} | |
1777 | ||
1778 | The current set of warning checks enabled by the C<use warnings> pragma. | |
1779 | See the documentation of C<warnings> for more details. | |
1780 | ||
1781 | This variable was added in Perl 5.10. | |
1782 | ||
b0c22438 | 1783 | =item $OS_ERROR |
5ccee41e | 1784 | |
b0c22438 | 1785 | =item $ERRNO |
5ccee41e | 1786 | |
b0c22438 | 1787 | =item $! |
1788 | X<$!> X<$ERRNO> X<$OS_ERROR> | |
9b0e6e7a | 1789 | |
b0c22438 | 1790 | If used numerically, yields the current value of the C C<errno> |
1791 | variable, or in other words, if a system or library call fails, it | |
1792 | sets this variable. This means that the value of C<$!> is meaningful | |
1793 | only I<immediately> after a B<failure>: | |
9b0e6e7a | 1794 | |
b0c22438 | 1795 | if (open my $fh, "<", $filename) { |
1796 | # Here $! is meaningless. | |
1797 | ... | |
1798 | } else { | |
1799 | # ONLY here is $! meaningful. | |
1800 | ... | |
1801 | # Already here $! might be meaningless. | |
1802 | } | |
1803 | # Since here we might have either success or failure, | |
1804 | # here $! is meaningless. | |
a0d0e21e | 1805 | |
b0c22438 | 1806 | In the above I<meaningless> stands for anything: zero, non-zero, |
84dabc03 | 1807 | C<undef>. A successful system or library call does B<not> set the |
1808 | variable to zero. | |
a0d0e21e | 1809 | |
84dabc03 | 1810 | If used as a string, yields the corresponding system error string. You |
1811 | can assign a number to C<$!> to set I<errno> if, for instance, you | |
1812 | want C<"$!"> to return the string for error I<n>, or you want to set | |
1813 | the exit value for the C<die()> operator. | |
d54b56d5 | 1814 | |
b0c22438 | 1815 | Mnemonic: What just went bang? |
314d39ce | 1816 | |
b0c22438 | 1817 | =item %OS_ERROR |
fb73857a | 1818 | |
b0c22438 | 1819 | =item %ERRNO |
fb73857a | 1820 | |
b0c22438 | 1821 | =item %! |
1822 | X<%!> | |
a0d0e21e | 1823 | |
b0c22438 | 1824 | Each element of C<%!> has a true value only if C<$!> is set to that |
1825 | value. For example, C<$!{ENOENT}> is true if and only if the current | |
84dabc03 | 1826 | value of C<$!> is C<ENOENT>; that is, if the most recent error was "No |
1827 | such file or directory" (or its moral equivalent: not all operating | |
1828 | systems give that exact error, and certainly not all languages). To | |
1829 | check if a particular key is meaningful on your system, use C<exists | |
1830 | $!{the_key}>; for a list of legal keys, use C<keys %!>. See L<Errno> | |
1831 | for more information, and also see above for the validity of C<$!>. | |
a0d0e21e | 1832 | |
b0c22438 | 1833 | This variable was added in Perl 5.005. |
44f0be63 | 1834 | |
84dabc03 | 1835 | =item $CHILD_ERROR |
b687b08b | 1836 | |
84dabc03 | 1837 | =item $? |
1838 | X<$?> X<$CHILD_ERROR> | |
a0d0e21e | 1839 | |
84dabc03 | 1840 | The status returned by the last pipe close, backtick (C<``>) command, |
1841 | successful call to C<wait()> or C<waitpid()>, or from the C<system()> | |
1842 | operator. This is just the 16-bit status word returned by the | |
1843 | traditional Unix C<wait()> system call (or else is made up to look | |
1844 | like it). Thus, the exit value of the subprocess is really (C<<< $? >> | |
1845 | 8 >>>), and C<$? & 127> gives which signal, if any, the process died | |
1846 | from, and C<$? & 128> reports whether there was a core dump. | |
a0d0e21e | 1847 | |
84dabc03 | 1848 | Additionally, if the C<h_errno> variable is supported in C, its value |
1849 | is returned via C<$?> if any C<gethost*()> function fails. | |
b687b08b | 1850 | |
84dabc03 | 1851 | If you have installed a signal handler for C<SIGCHLD>, the |
1852 | value of C<$?> will usually be wrong outside that handler. | |
a0d0e21e | 1853 | |
84dabc03 | 1854 | Inside an C<END> subroutine C<$?> contains the value that is going to be |
1855 | given to C<exit()>. You can modify C<$?> in an C<END> subroutine to | |
1856 | change the exit status of your program. For example: | |
a0d0e21e | 1857 | |
84dabc03 | 1858 | END { |
1859 | $? = 1 if $? == 255; # die would make it 255 | |
1860 | } | |
a0d0e21e | 1861 | |
84dabc03 | 1862 | Under VMS, the pragma C<use vmsish 'status'> makes C<$?> reflect the |
1863 | actual VMS exit status, instead of the default emulation of POSIX | |
1864 | status; see L<perlvms/$?> for details. | |
1865 | ||
1866 | Mnemonic: similar to B<sh> and B<ksh>. | |
a0d0e21e | 1867 | |
b0c22438 | 1868 | =item $EVAL_ERROR |
f648820c | 1869 | |
b0c22438 | 1870 | =item $@ |
1871 | X<$@> X<$EVAL_ERROR> | |
a0d0e21e | 1872 | |
b0c22438 | 1873 | The Perl syntax error message from the last eval() operator. If $@ is |
1874 | the null string, the last eval() parsed and executed correctly | |
1875 | (although the operations you invoked may have failed in the normal | |
1876 | fashion). | |
a0d0e21e | 1877 | |
b0c22438 | 1878 | Warning messages are not collected in this variable. You can, however, |
1879 | set up a routine to process warnings by setting C<$SIG{__WARN__}> as | |
1880 | described below. | |
748a9306 | 1881 | |
b0c22438 | 1882 | Mnemonic: Where was the syntax error "at"? |
7f315d2e | 1883 | |
b0c22438 | 1884 | =back |
7f315d2e | 1885 | |
b0c22438 | 1886 | =head2 Deprecated and removed variables |
7f315d2e | 1887 | |
84dabc03 | 1888 | Deprecating a variable announces the intent of the perl maintainers to |
1889 | eventually remove the variable from the langauge. It may still be | |
b0c22438 | 1890 | available despite its status. Using a deprecated variable triggers |
1891 | a warning. | |
7f315d2e | 1892 | |
84dabc03 | 1893 | Once a variable is removed, its use triggers an error telling you |
b0c22438 | 1894 | the variable is unsupported. |
7f315d2e | 1895 | |
84dabc03 | 1896 | See L<perldiag> for details about error messages. |
7f315d2e | 1897 | |
b0c22438 | 1898 | =over 8 |
7f315d2e | 1899 | |
84dabc03 | 1900 | =item $# |
1901 | X<$#> | |
1902 | ||
1903 | C<$#> was a variable that you could be use to format printed numbers. | |
1904 | After a deprecation cycle, its magic was removed in Perl 5.10 and | |
1905 | using it now triggers a warning: C<$# is no longer supported>. | |
1906 | ||
1907 | This is not the sigil you use in front of an array name to get the | |
1908 | last index, like C<$#array>. That's still how you get the last index | |
1909 | of an array in Perl. The two have nothing to do with each other. | |
1910 | ||
1911 | Deprecated in Perl 5. | |
1912 | ||
1913 | Removed in Perl 5.10. | |
1914 | ||
7f315d2e CO |
1915 | =item $* |
1916 | X<$*> | |
1917 | ||
84dabc03 | 1918 | C<$*> was a variable that you could use to enable multiline matching. |
7f315d2e CO |
1919 | After a deprecation cycle, its magic was removed in Perl 5.10. |
1920 | Using it now triggers a warning: C<$* is no longer supported>. | |
84dabc03 | 1921 | You should use the C</s> and C</m> regexp modifiers instead. |
7f315d2e | 1922 | |
b0c22438 | 1923 | Deprecated in Perl 5. |
7f315d2e | 1924 | |
b0c22438 | 1925 | Removed in Perl 5.10. |
7f315d2e | 1926 | |
84dabc03 | 1927 | =item $[ |
1928 | X<$[> | |
1929 | ||
1930 | This variable stores the index of the first element in an array, and | |
1931 | of the first character in a substring. You use to be able to assign to | |
1932 | this variable, but you can't do that anymore. It's now always 0, like | |
1933 | God intended. | |
1934 | ||
1935 | Mnemonic: [ begins subscripts. | |
1936 | ||
1937 | This variable is read-only. | |
1938 | ||
1939 | Deprecated in Perl 5.12. | |
1940 | ||
b0c22438 | 1941 | =item $] |
1942 | X<$]> | |
55602bd2 | 1943 | |
b0c22438 | 1944 | The version + patchlevel / 1000 of the Perl interpreter. This variable |
1945 | can be used to determine whether the Perl interpreter executing a | |
1946 | script is in the right range of versions: | |
55602bd2 | 1947 | |
b0c22438 | 1948 | warn "No checksumming!\n" if $] < 3.019; |
55602bd2 | 1949 | |
b0c22438 | 1950 | See also the documentation of C<use VERSION> and C<require VERSION> |
1951 | for a convenient way to fail if the running Perl interpreter is too old. | |
55602bd2 | 1952 | |
b0c22438 | 1953 | The floating point representation can sometimes lead to inaccurate |
1954 | numeric comparisons. See C<$^V> for a more modern representation of | |
1955 | the Perl version that allows accurate string comparisons. | |
55602bd2 | 1956 | |
b0c22438 | 1957 | Mnemonic: Is this version of perl in the right bracket? |
19799a22 | 1958 | |
b0c22438 | 1959 | Deprecated in Perl 5.6. |
19799a22 | 1960 | |
b0c22438 | 1961 | =back |
2b92dfce | 1962 | |
19799a22 GS |
1963 | =head1 BUGS |
1964 | ||
1965 | Due to an unfortunate accident of Perl's implementation, C<use | |
1966 | English> imposes a considerable performance penalty on all regular | |
1967 | expression matches in a program, regardless of whether they occur | |
b0c22438 | 1968 | in the scope of C<use English>. For that reason, saying C<use |
1969 | English> in libraries is strongly discouraged. See the | |
19799a22 | 1970 | Devel::SawAmpersand module documentation from CPAN |
1577cd80 | 1971 | ( http://www.cpan.org/modules/by-module/Devel/ ) |
a054c801 GS |
1972 | for more information. Writing C<use English '-no_match_vars';> |
1973 | avoids the performance penalty. | |
2b92dfce | 1974 | |
19799a22 GS |
1975 | Having to even think about the C<$^S> variable in your exception |
1976 | handlers is simply wrong. C<$SIG{__DIE__}> as currently implemented | |
b0c22438 | 1977 | invites grievous and difficult to track down errors. Avoid it |
19799a22 | 1978 | and use an C<END{}> or CORE::GLOBAL::die override instead. |