Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

can anyone tell me what $0, $| and $\ stand for?

Replies are listed 'Best First'.
Re: simple question
by btrott (Parson) on Mar 30, 2000 at 05:22 UTC
    (For further such inquiries, you may want to check out perlvar, which explains the special Perl variables.)

    From perlvar:

    $0 Contains the name of the file containing the Perl script being executed. On some operating systems assigning to "$0" modifies the argument area that the ps(1) program sees. This is more useful as a way of indicating the current program state than it is for hiding the program you're running. (Mnemonic: same as sh and ksh.) $| 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 actually buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). Note that 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, such as when you are running a Perl script under rsh and want to see the output as it's happening. This has no effect on input buffering. (Mnemonic: when you want your pipes to be piping hot.) $\ The output record separator for the print operator. Ordinarily the print operator simply prints out the comma-separated fields you specify, with no trailing newline or record separator assumed. To get behavior more like awk, set this variable as you would set awk's ORS variable to specify what is printed at the end of the print. (Mnemonic: you set "$\" instead of adding \n at the end of the print. Also, it's just like $/, but it's what you get "back" from Perl.)
Re: simple question
by btrott (Parson) on Mar 30, 2000 at 05:33 UTC
    By the way: I hate to assume that you may have mis-typed, but since (as turnstep wrote) $\ is so rarely used, perhaps you meant $/? $/ is used much more often than $\, because it's used when you want to alter the behavior of the <> operator, which by default reads a file in a line at a time. For example, say that you want to read in the entire file in one slurp:
    { local $/ = undef; $_ = <FH>; }
    The entire file is now in $_, which is nice.

    Here's the entry from perlvar:

    $/ The input record separator, newline by default. Works like awk's RS variable, including treating empty lines as delimiters if set to the null string. (Note: An empty line cannot contain any spaces or tabs.) You may set it to a multi-character string to match a multi-character delimiter, or to undef to read to end of file. Note that setting it to "\n\n" means something slightly different than setting it to "", if the file contains consecutive empty lines. Setting it to "" will treat two or more consecutive empty lines as a single empty line. Setting it to "\n\n" will blindly assume that the next input character belongs to the next paragraph, even if it's a newline. (Mnemonic: / is used to delimit line boundaries when quoting poetry.) undef $/; $_ = <FH>; # whole file now here s/\n[ \t]+/ /g; Remember: the value of $/ is a string, not a regexp. AWK has to be better for something :-)
RE: simple question
by turnstep (Parson) on Mar 30, 2000 at 05:25 UTC
    • $0 ($PROGRAM_NAME)
      The name of the file containing the running perl script.
    • $| ($OUTPUT_AUTOFLUSH)
      Causes a fflush after each write or print if true. Usually set in cgi scripts by $|++ or $|=1;
    • $\ ($OUTPUT_RECORD_SEPARATOR)
      Similar to awk's ORS. Rarely used.

    The parenthesis are the "English" names (you can use those if you say

    use English;
    at the top of your script).