Table 5-1. Alternatives to built-in variables
Variable | Purpose | Alternative |
---|
$1, $2, $3, etc. | Store substrings captured from the previous regex match | Assign captures directly using list context regex matching, or unpack them into lexical variables immediately after the match (see Chapter 12). Note that these variables are still acceptable in the replacement string of a substitution, because there is no alternative. For example:s{($DD)/($MMM)/($YYYY)}{$3-$2-$1}xms |
$& | Stores the complete substring most recently matched by a regex | Place an extra set of capturing parentheses around the entire regex, or use Regexp::MatchContext (see the "Match Variables" guideline later in this chapter). |
$' | Stores the substring that preceded the most recent successful regex match | Place a ((?s).*?) at the beginning of the regex to capture everything up to the start of the pattern you are actually interested in, or use Regexp::MatchContext. |
$' | Stores the substring that followed the most recent successful regex match | Place a ((?s).*) at the end of the regex to capture everything after the pattern you are actually interested in, or use Regexp::MatchContext. |
$* | Controls newline matching in regexes | Use the /m regex modifier. |
$. | Stores the current line number of the current input stream | Use $fh->input_line_number( ). |
$| | Controls autoflushing of the current output stream | Use $fh->autoflush( ). |
$" | Array element separator when interpolating into strings | Use an explicit join. |
$%, $=, $-, $~, $^, $:, $^L, $^A | Control various features of Perl's format mechanism | Use Perl6::Form::form instead (see Chapter 19). |
$[ | Determines the starting index of arrays and strings | Never change the starting index from zero. |
@F | Stores the result of autosplitting the current line | Don't use the -a command-line flag when invoking perl. |
$^W | Controls warnings | Under Perl 5.6.1 and later, specify use warnings instead. |