home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / SLAKWARE / D13 / PERL2.TGZ / perl2.tar / usr / lib / perl5 / pod / perlvar.pod < prev    next >
Text File  |  1996-06-28  |  21KB  |  696 lines

  1. =head1 NAME
  2.  
  3. perlvar - Perl predefined variables
  4.  
  5. =head1 DESCRIPTION
  6.  
  7. =head2 Predefined Names
  8.  
  9. The following names have special meaning to Perl.  Most of the
  10. punctuational names have reasonable mnemonics, or analogues in one of
  11. the shells.  Nevertheless, if you wish to use the long variable names,
  12. you just need to say
  13.  
  14.     use English;
  15.  
  16. at the top of your program.  This will alias all the short names to the
  17. long names in the current package.  Some of them even have medium names,
  18. generally borrowed from B<awk>.
  19.  
  20. To go a step further, those variables that depend on the currently
  21. selected filehandle may instead be set by calling an object method on
  22. the FileHandle object.  (Summary lines below for this contain the word
  23. HANDLE.)  First you must say
  24.  
  25.     use FileHandle;
  26.  
  27. after which you may use either
  28.  
  29.     method HANDLE EXPR
  30.  
  31. or
  32.  
  33.     HANDLE->method(EXPR)
  34.  
  35. Each of the methods returns the old value of the FileHandle attribute.
  36. The methods each take an optional EXPR, which if supplied specifies the
  37. new value for the FileHandle attribute in question.  If not supplied,
  38. most of the methods do nothing to the current value, except for
  39. autoflush(), which will assume a 1 for you, just to be different.
  40.  
  41. A few of these variables are considered "read-only".  This means that if
  42. you try to assign to this variable, either directly or indirectly through
  43. a reference, you'll raise a run-time exception.
  44.  
  45. =over 8
  46.  
  47. =item $ARG
  48.  
  49. =item $_
  50.  
  51. The default input and pattern-searching space.  The following pairs are
  52. equivalent:
  53.  
  54.     while (<>) {...}    # only equivalent in while!
  55.     while ($_ = <>) {...}
  56.  
  57.     /^Subject:/
  58.     $_ =~ /^Subject:/
  59.  
  60.     tr/a-z/A-Z/
  61.     $_ =~ tr/a-z/A-Z/
  62.  
  63.     chop
  64.     chop($_)
  65.  
  66. Here are the places where Perl will assume $_ even if you 
  67. don't use it:
  68.  
  69. =over 3
  70.  
  71. =item *
  72.  
  73. Various unary functions, including functions like ord() and int(), as well
  74. as the all file tests (C<-f>, C<-d>) except for C<-t>, which defaults to
  75. STDIN.
  76.  
  77. =item *
  78.  
  79. Various list functions like print() and unlink().
  80.  
  81. =item *
  82.  
  83. The pattern matching operations C<m//>, C<s///>, and C<tr///> when used
  84. without an C<=~> operator.
  85.  
  86. =item * 
  87.  
  88. The default iterator variable in a C<foreach> loop if no other
  89. variable is supplied.
  90.  
  91. =item * 
  92.  
  93. The implicit iterator variable in the grep() and map() functions.
  94.  
  95. =item * 
  96.  
  97. The default place to put an input record when a C<E<lt>FHE<gt>>
  98. operation's result is tested by itself as the sole criterion of a C<while>
  99. test.  Note that outside of a C<while> test, this will not happen.
  100.  
  101. =back
  102.  
  103. (Mnemonic: underline is understood in certain operations.)
  104.  
  105. =item $<I<digit>>
  106.  
  107. Contains the subpattern from the corresponding set of parentheses in
  108. the last pattern matched, not counting patterns matched in nested
  109. blocks that have been exited already.  (Mnemonic: like \digit.)
  110. These variables are all read-only.
  111.  
  112. =item $MATCH
  113.  
  114. =item $&
  115.  
  116. The string matched by the last successful pattern match (not counting
  117. any matches hidden within a BLOCK or eval() enclosed by the current
  118. BLOCK).  (Mnemonic: like & in some editors.)  This variable is read-only.
  119.  
  120. =item $PREMATCH
  121.  
  122. =item $`
  123.  
  124. The string preceding whatever was matched by the last successful
  125. pattern match (not counting any matches hidden within a BLOCK or eval
  126. enclosed by the current BLOCK).  (Mnemonic: ` often precedes a quoted
  127. string.)  This variable is read-only.
  128.  
  129. =item $POSTMATCH
  130.  
  131. =item $'
  132.  
  133. The string following whatever was matched by the last successful
  134. pattern match (not counting any matches hidden within a BLOCK or eval()
  135. enclosed by the current BLOCK).  (Mnemonic: ' often follows a quoted
  136. string.)  Example:
  137.  
  138.     $_ = 'abcdefghi';
  139.     /def/;
  140.     print "$`:$&:$'\n";      # prints abc:def:ghi
  141.  
  142. This variable is read-only.
  143.  
  144. =item $LAST_PAREN_MATCH
  145.  
  146. =item $+
  147.  
  148. The last bracket matched by the last search pattern.  This is useful if
  149. you don't know which of a set of alternative patterns matched.  For
  150. example:
  151.  
  152.     /Version: (.*)|Revision: (.*)/ && ($rev = $+);
  153.  
  154. (Mnemonic: be positive and forward looking.)
  155. This variable is read-only.
  156.  
  157. =item $MULTILINE_MATCHING
  158.  
  159. =item $*
  160.  
  161. Set to 1 to do multiline matching within a string, 0 to tell Perl
  162. that it can assume that strings contain a single line, for the purpose
  163. of optimizing pattern matches.  Pattern matches on strings containing
  164. multiple newlines can produce confusing results when "C<$*>" is 0.  Default
  165. is 0.  (Mnemonic: * matches multiple things.)  Note that this variable
  166. only influences the interpretation of "C<^>" and "C<$>".  A literal newline can
  167. be searched for even when C<$* == 0>.
  168.  
  169. Use of "C<$*>" is deprecated in Perl 5.
  170.  
  171. =item input_line_number HANDLE EXPR
  172.  
  173. =item $INPUT_LINE_NUMBER
  174.  
  175. =item $NR
  176.  
  177. =item $.
  178.  
  179. The current input line number of the last filehandle that was read.  An
  180. explicit close on the filehandle resets the line number.  Since
  181. "C<E<lt>E<gt>>" never does an explicit close, line numbers increase
  182. across ARGV files (but see examples under eof()).  Localizing C<$.> has
  183. the effect of also localizing Perl's notion of "the last read
  184. filehandle".  (Mnemonic: many programs use "." to mean the current line
  185. number.)
  186.  
  187. =item input_record_separator HANDLE EXPR
  188.  
  189. =item $INPUT_RECORD_SEPARATOR
  190.  
  191. =item $RS
  192.  
  193. =item $/
  194.  
  195. The input record separator, newline by default.  Works like B<awk>'s RS
  196. variable, including treating blank lines as delimiters if set to the
  197. null string.  You may set it to a multicharacter string to match a
  198. multi-character delimiter.  Note that setting it to C<"\n\n"> means
  199. something slightly different than setting it to C<"">, if the file
  200. contains consecutive blank lines.  Setting it to C<""> will treat two or
  201. more consecutive blank lines as a single blank line.  Setting it to
  202. C<"\n\n"> will blindly assume that the next input character belongs to the
  203. next paragraph, even if it's a newline.  (Mnemonic: / is used to
  204. delimit line boundaries when quoting poetry.)
  205.  
  206.     undef $/;
  207.     $_ = <FH>;         # whole file now here
  208.     s/\n[ \t]+/ /g;
  209.  
  210. =item autoflush HANDLE EXPR
  211.  
  212. =item $OUTPUT_AUTOFLUSH
  213.  
  214. =item $|
  215.  
  216. If set to nonzero, forces a flush after every write or print on the
  217. currently selected output channel.  Default is 0.  Note that STDOUT
  218. will typically be line buffered if output is to the terminal and block
  219. buffered otherwise.  Setting this variable is useful primarily when you
  220. are outputting to a pipe, such as when you are running a Perl script
  221. under rsh and want to see the output as it's happening.  This has no
  222. effect on input buffering.  
  223. (Mnemonic: when you want your pipes to be piping hot.)
  224.  
  225. =item output_field_separator HANDLE EXPR
  226.  
  227. =item $OUTPUT_FIELD_SEPARATOR
  228.  
  229. =item $OFS
  230.  
  231. =item $,
  232.  
  233. The output field separator for the print operator.  Ordinarily the
  234. print operator simply prints out the comma separated fields you
  235. specify.  In order to get behavior more like B<awk>, set this variable
  236. as you would set B<awk>'s OFS variable to specify what is printed
  237. between fields.  (Mnemonic: what is printed when there is a , in your
  238. print statement.)
  239.  
  240. =item output_record_separator HANDLE EXPR
  241.  
  242. =item $OUTPUT_RECORD_SEPARATOR
  243.  
  244. =item $ORS
  245.  
  246. =item $\
  247.  
  248. The output record separator for the print operator.  Ordinarily the
  249. print operator simply prints out the comma separated fields you
  250. specify, with no trailing newline or record separator assumed.  In
  251. order to get behavior more like B<awk>, set this variable as you would
  252. set B<awk>'s ORS variable to specify what is printed at the end of the
  253. print.  (Mnemonic: you set "C<$\>" instead of adding \n at the end of the
  254. print.  Also, it's just like /, but it's what you get "back" from
  255. Perl.)
  256.  
  257. =item $LIST_SEPARATOR
  258.  
  259. =item $"
  260.  
  261. This is like "C<$,>" except that it applies to array values interpolated
  262. into a double-quoted string (or similar interpreted string).  Default
  263. is a space.  (Mnemonic: obvious, I think.)
  264.  
  265. =item $SUBSCRIPT_SEPARATOR
  266.  
  267. =item $SUBSEP
  268.  
  269. =item $;
  270.  
  271. The subscript separator for multi-dimensional array emulation.  If you
  272. refer to a hash element as
  273.  
  274.     $foo{$a,$b,$c}
  275.  
  276. it really means
  277.  
  278.     $foo{join($;, $a, $b, $c)}
  279.  
  280. But don't put
  281.  
  282.     @foo{$a,$b,$c}    # a slice--note the @
  283.  
  284. which means
  285.  
  286.     ($foo{$a},$foo{$b},$foo{$c})
  287.  
  288. Default is "\034", the same as SUBSEP in B<awk>.  Note that if your
  289. keys contain binary data there might not be any safe value for "C<$;>".
  290. (Mn