Query: perlvar
OS: x11r4
Section: 1
Links: x11r4 man pages all man pages
Forums: forum home forum categories
Format: Original Unix Latex Style Formatted with HTML and a Horizontal Scroll Bar
PERLVAR(1) Perl Programmers Reference Guide PERLVAR(1)NAMEperlvar - Perl predefined variablesDESCRIPTIONPredefined Names The following names have special meaning to Perl. Most punctuation names have reasonable mnemonics, or analogs in the shells. Neverthe- less, if you wish to use long variable names, you need only say use English; at the top of your program. This aliases all the short names to the long names in the current package. Some even have medium names, gener- ally borrowed from awk. In general, it's best to use the use English '-no_match_vars'; invocation if you don't need $PREMATCH, $MATCH, or $POSTMATCH, as it avoids a certain performance hit with the use of regular expressions. See English. Variables that depend on the currently selected filehandle may be set by calling an appropriate object method on the IO::Handle object, although this is less efficient than using the regular built-in variables. (Summary lines below for this contain the word HANDLE.) First you must say use IO::Handle; after which you may use either method HANDLE EXPR or more safely, HANDLE->method(EXPR) Each method returns the old value of the IO::Handle attribute. The methods each take an optional EXPR, which, if supplied, specifies the new value for the IO::Handle attribute in question. If not supplied, most methods do nothing to the current value--except for autoflush(), which will assume a 1 for you, just to be different. Because loading in the IO::Handle class is an expensive operation, you should learn how to use the regular built-in variables. A few of these variables are considered "read-only". This means that if you try to assign to this variable, either directly or indirectly through a reference, you'll raise a run-time exception. You should be very careful when modifying the default values of most special variables described in this document. In most cases you want to localize these variables before changing them, since if you don't, the change may affect other modules which rely on the default values of the special variables that you have changed. This is one of the correct ways to read the whole file at once: open my $fh, "<", "foo" or die $!; local $/; # enable localized slurp mode my $content = <$fh>; close $fh; But the following code is quite bad: open my $fh, "<", "foo" or die $!; undef $/; # enable slurp mode my $content = <$fh>; close $fh; since some other module, may want to read data from some file in the default "line mode", so if the code we have just presented has been executed, the global value of $/ is now changed for any other code running inside the same Perl interpreter. Usually when a variable is localized you want to make sure that this change affects the shortest scope possible. So unless you are already inside some short "{}" block, you should create one yourself. For example: my $content = ''; open my $fh, "<", "foo" or die $!; { local $/; $content = <$fh>; } close $fh; Here is an example of how your own code can go broken: for (1..5){ nasty_break(); print "$_ "; } sub nasty_break { $_ = 5; # do something with $_ } You probably expect this code to print: 1 2 3 4 5 but instead you get: 5 5 5 5 5 Why? Because nasty_break() modifies $_ without localizing it first. The fix is to add local(): local $_ = 5; It's easy to notice the problem in such a short example, but in more complicated code you are looking for trouble if you don't localize changes to the special variables. The following list is ordered by scalar variables first, then the arrays, then the hashes. $ARG $_ The default input and pattern-searching space. The following pairs are equivalent: while (<>) {...} # equivalent only in while! while (defined($_ = <>)) {...} /^Subject:/ $_ =~ /^Subject:/ tr/a-z/A-Z/ $_ =~ tr/a-z/A-Z/ chomp chomp($_) Here are the places where Perl will assume $_ even if you don't use it: * The following functions: abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, exp, glob, hex, int, lc, lcfirst, length, log, lstat, mkdir, oct, ord, pos, print, quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only), rmdir, sin, split (on its second argument), sqrt, stat, study, uc, ucfirst, unlink, unpack. * All file tests ("-f", "-d") except for "-t", which defaults to STDIN. See "-X" in perlfunc * The pattern matching operations "m//", "s///" and "tr///" (aka "y///") when used without an "=~" operator. * The default iterator variable in a "foreach" loop if no other variable is supplied. * The implicit iterator variable in the grep() and map() functions. * The implicit variable of given(). * The default place to put an input record when a "<FH>" operation's result is tested by itself as the sole criterion of a "while" test. Outside a "while" test, this will not happen. (Mnemonic: underline is understood in certain operations.) $a $b Special package variables when using sort(), see "sort" in perlfunc. Because of this specialness $a and $b don't need to be declared (using use vars, or our()) even when using the "strict 'vars'" pragma. Don't lexicalize them with "my $a" or "my $b" if you want to be able to use them in the sort() comparison block or function. $<digits> Contains the subpattern from the corresponding set of capturing parentheses from the last pattern match, not counting patterns matched in nested blocks that have been exited already. (Mnemonic: like digits.) These variables are all read-only and dynami- cally scoped to the current BLOCK. $MATCH $& The string matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK). (Mnemonic: like & in some editors.) This variable is read-only and dynamically scoped to the current BLOCK. The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. See "BUGS". See "@-" for a replacement. $PREMATCH $` The string preceding whatever was matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval enclosed by the current BLOCK). (Mnemonic: "`" often precedes a quoted string.) This variable is read-only. The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. See "BUGS". See "@-" for a replacement. $POSTMATCH $' The string following whatever was matched by the last successful pattern match (not counting any matches hidden within a BLOCK or eval() enclosed by the current BLOCK). (Mnemonic: "'" often follows a quoted string.) Example: local $_ = 'abcdefghi'; /def/; print "$`:$&:$' "; # prints abc:def:ghi This variable is read-only and dynamically scoped to the current BLOCK. The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. See "BUGS". See "@-" for a replacement. $LAST_PAREN_MATCH $+ The text matched by the last bracket of the last successful search pattern. This is useful if you don't know which one of a set of alternative patterns matched. For example: /Version: (.*)|Revision: (.*)/ && ($rev = $+); (Mnemonic: be positive and forward looking.) This variable is read-only and dynamically scoped to the current BLOCK. $LAST_SUBMATCH_RESULT $^N The text matched by the used group most-recently closed (i.e. the group with the rightmost closing parenthesis) of the last suc- cessful search pattern. (Mnemonic: the (possibly) Nested parenthesis that most recently closed.) This is primarily used inside "(?{...})" blocks for examining text recently matched. For example, to effectively capture text to a variable (in addition to $1, $2, etc.), replace "(...)" with (?:(...)(?{ $var = $^N })) By setting and then using $var in this way relieves you from having to worry about exactly which numbered set of parentheses they are. This variable is dynamically scoped to the current BLOCK. @LAST_MATCH_END @+ This array holds the offsets of the ends of the last successful submatches in the currently active dynamic scope. $+[0] is the offset into the string of the end of the entire match. This is the same value as what the "pos" function returns when called on the variable that was matched against. The nth element of this array holds the offset of the nth submatch, so $+[1] is the offset past where $1 ends, $+[2] the offset past where $2 ends, and so on. You can use $#+ to determine how many subgroups were in the last successful match. See the examples given for the "@-" variable. $* Set to a non-zero integer value to do multi-line matching within a string, 0 (or undefined) to tell Perl that it can assume that strings contain a single line, for the purpose of optimizing pattern matches. Pattern matches on strings containing multiple new- lines can produce confusing results when $* is 0 or undefined. Default is undefined. (Mnemonic: * matches multiple things.) This variable influences the interpretation of only "^" and "$". A literal newline can be searched for even when "$* == 0". Use of $* is deprecated in modern Perl, supplanted by the "/s" and "/m" modifiers on pattern matching. Assigning a non-numerical value to $* triggers a warning (and makes $* act if "$* == 0"), while assigning a numerical value to $* makes that an implicit "int" is applied on the value. HANDLE->input_line_number(EXPR) $INPUT_LINE_NUMBER $NR $. Current line number for the last filehandle accessed. Each filehandle in Perl counts the number of lines that have been read from it. (Depending on the value of $/, Perl's idea of what constitutes a line may not match yours.) When a line is read from a filehandle (via readline() or "<>"), or when tell() or seek() is called on it, $. becomes an alias to the line counter for that filehandle. You can adjust the counter by assigning to $., but this will not actually move the seek pointer. Localizing $. will not localize the filehandle's line count. Instead, it will localize perl's notion of which filehandle $. is currently aliased to. $. is reset when the filehandle is closed, but not when an open filehandle is reopened without an intervening close(). For more details, see "I/O Operators" in perlop. Because "<>" never does an explicit close, line numbers increase across ARGV files (but see examples in "eof" in perlfunc). You can also use "HANDLE->input_line_number(EXPR)" to access the line counter for a given filehandle without having to worry about which handle you last accessed. (Mnemonic: many programs use "." to mean the current line number.) IO::Handle->input_record_separator(EXPR) $INPUT_RECORD_SEPARATOR $RS $/ The input record separator, newline by default. This influences Perl's idea of what a "line" is. Works like awk's RS variable, including treating empty lines as a terminator if set to the null string. (An empty line cannot contain any spaces or tabs.) You may set it to a multi-character string to match a multi-character terminator, or to "undef" to read through the end of file. Set- ting it to " " means something slightly different than setting to "", if the file contains consecutive empty lines. Setting to "" will treat two or more consecutive empty lines as a single empty line. Setting to " " will blindly assume that the next input character belongs to the next paragraph, even if it's a newline. (Mnemonic: / delimits line boundaries when quoting poetry.) local $/; # enable "slurp" mode local $_ = <FH>; # whole file now here s/ [ ]+/ /g; Remember: the value of $/ is a string, not a regex. awk has to be better for something. :-) Setting $/ to a reference to an integer, scalar containing an integer, or scalar that's convertible to an integer will attempt to read records instead of lines, with the maximum record size being the referenced integer. So this: local $/ = 32768; # or "32768", or $var_containing_32768 open my $fh, "<", $myfile or die $!; local $_ = <$fh>; will read a record of no more than 32768 bytes from FILE. If you're not reading from a record-oriented file (or your OS doesn't have record-oriented files), then you'll likely get a full chunk of data with every read. If a record is larger than the record size you've set, you'll get the record back in pieces. Trying to set the record size to zero or less will cause reading in the (rest of the) whole file. On VMS, record reads are done with the equivalent of "sysread", so it's best not to mix record and non-record reads on the same file. (This is unlikely to be a problem, because any file you'd want to read in record mode is probably unusable in line mode.) Non-VMS systems do normal I/O, so it's safe to mix record and non-record reads of a file. See also "Newlines" in perlport. Also see $.. HANDLE->autoflush(EXPR) $OUTPUT_AUTOFLUSH $| If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it's happening. This has no effect on input buffering. See "getc" in perlfunc for that. See "select" in perldoc on how to select the output channel. See also IO::Handle. (Mnemonic: when you want your pipes to be piping hot.) IO::Handle->output_field_separator EXPR $OUTPUT_FIELD_SEPARATOR $OFS $, The output field separator for the print operator. If defined, this value is printed between each of print's arguments. Default is "undef". (Mnemonic: what is printed when there is a "," in your print statement.) IO::Handle->output_record_separator EXPR $OUTPUT_RECORD_SEPARATOR $ORS $ The output record separator for the print operator. If defined, this value is printed after the last of print's arguments. Default is "undef". (Mnemonic: you set "$" instead of adding " " at the end of the print. Also, it's just like $/, but it's what you get "back" from Perl.) $LIST_SEPARATOR $" This is like $, except that it applies to array and slice values interpolated into a double-quoted string (or similar interpreted string). Default is a space. (Mnemonic: obvious, I think.) $SUBSCRIPT_SEPARATOR $SUBSEP $; The subscript separator for multidimensional array emulation. If you refer to a hash element as $foo{$a,$b,$c} it really means $foo{join($;, $a, $b, $c)} But don't put @foo{$a,$b,$c} # a slice--note the @ which means ($foo{$a},$foo{$b},$foo{$c}) Default is "