Section 5.1.  Lexical Variables

Table of Contents

5.1. Lexical Variables

Avoid using non-lexical variables.

Stick to using only lexical variables (my), unless you genuinely need the functionality that only a package or punctuation variable can provide.

Using non-lexical variables increases the "coupling" of your code. If two otherwise unrelated sections of code both use a package variable, those two pieces of code can interact with each other in very subtle ways, just by the way they each interact with that shared variable. In other words, without full knowledge of every other piece of code that is called from a particular statement, it is impossible to know whether the value of a given non-lexical variable will somehow be changed by executing that statement.

Some of Perl's built-in non-lexical variables, such as $_, @ARGV, $AUTOLOAD, or $a and $b, are impossible to avoid. But most of the rest are not required in general programming, and there are usually better alternatives. Table 5-1 lists the commonly used Perl built-in variables and what you should use instead. Note that prior to Perl 5.8, you may need to specify use IO::Handle explicitly before using the suggestions that involve method calls on filehandles.

Table 5-1. Alternatives to built-in variables




$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:



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.


Stores the result of autosplitting the current line

Don't use the -a command-line flag when invoking perl.


Controls warnings

Under Perl 5.6.1 and later, specify use warnings instead.

    Table of Contents
    © 2000- NIV