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