2024/02/13 22:13:39

Perlの特殊変数

Perl predefined variables

Perl の特殊変数は検索しにくくて解説が見つけにくかったりします。

しかし、そんなときこそ perldoc コマンド。
以下のように -v で特殊変数の個別解説が読めます。

$ perldoc -v '$|'
    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.  (Mnemonic: when you want your pipes to
            be piping hot.)

特殊変数の全体をまとめて読むには perlvar が最適です。

$ perldoc perlvar

Perl 特殊変数 一行解説

Perl 特殊変数
特殊変数名 別名 説明
$_ $ARG The default input and pattern-searching space.
$a / $b Special package variables when using sort()
$& $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).
$` $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).
$' $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).
$+ $LAST_PAREN_MATCH The text matched by the last bracket of the last successful search pattern.
$^N The text matched by the used group most-recently closed (i.e. the group with the rightmost closing parenthesis) of the last successful search pattern.
@+ @LAST_MATCH_END This array holds the offsets of the ends of the last successful submatches in the currently active dynamic scope.
$* 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.
$. $NR
$INPUT_LINE_NUMBER
Current line number for the last filehandle accessed
$/ $RS
$INPUT_RECORD_SEPARATOR
The input record separator, newline by default.
$, $OFS
$OUTPUT_FIELD_SEPARATOR
The output field separator for the print operator.
$\ $ORS
$OUTPUT_RECORD_SEPARATOR
The output record separator for the print operator.
$" $LIST_SEPARATOR This is like $, except that it applies to array and slice values interpolated into a double-quoted string (or similar interpreted string).
$; $SUBSEP
$SUBSCRIPT_SEPARATOR
The subscript separator for multidimensional array emulation.
$% $FORMAT_PAGE_NUMBER The current page number of the currently selected output channel. Used with formats.
$= $FORMAT_LINES_PER_PAGE The current page length (printable lines) of the currently selected output channel. Default is 60.
$- $FORMAT_LINES_LEFT The number of lines left on the page of the currently selected output channel.
@- @LAST_MATCH_START $-[0] is the offset of the start of the last successful match. "$-["n"]" is the offset of the start of the substring matched by n-th subpattern, or undef if the subpattern did not match.
$~ $FORMAT_NAME The name of the current report format for the currently selected output channel. default is the name of the filehandle.
$^ $FORMAT_TOP_NAME The name of the current top-of-page format for the currently selected output channel. Default is the name of the filehandle with _TOP appended.
$: $FORMAT_LINE_BREAK_CHARACTERS The current set of characters after which a string may be broken to fill continuation fields (starting with ^) in a format.
$^L $FORMAT_FORMFEED What formats output as a form feed. Default is \f.
$^A $ACCUMULATOR The current value of the write() accumulator for format() lines. A format contains formline() calls that put their result into $^A.
$? $CHILD_ERROR The status returned by the last pipe close, backtick (``) command, successful call to wait() or waitpid(), or from the system() operator.
$! $OS_ERROR
$ERRNO
If used numerically, yields the current value of the C "errno" variable, or in other words, if a system or library call fails, it sets this variable.
$@ $EVAL_ERROR The Perl syntax error message from the last eval() operator.
$$ $PROCESS_ID
$PID
The process number of the Perl running this script. You should consider this variable read-only, although it will be altered across fork() calls.
$< $REAL_USER_ID
$UID
The real uid of this process.
$> $EFFECTIVE_USER_ID
$EUID
The effective uid of this process.
$( $REAL_GROUP_ID
$GID
The real gid of this process. If you are on a machine that supports membership in multiple groups simultaneously, gives a space separated list of groups you are in.
$) $EFFECTIVE_GROUP_ID
$EGID
The effective gid of this process.
$0 $PROGRAM_NAME Contains the name of the program being executed.
$^C $COMPILING The current value of the flag associated with the -c switch.
$^D $DEBUGGING The current value of the debugging flags.
$^F $SYSTEM_FD_MAX The maximum system file descriptor, ordinarily 2.
$^I $INPLACE_EDIT The current value of the inplace-edit extension.
$^O $OSNAME The name of the operating system under which this copy of Perl was built, as determined during the configuration process.
$^P $PERLDB The internal variable for debugging support.
$^R $LAST_REGEXP_CODE_RESULT The result of evaluation of the last successful "(?{ code })" regular expression assertion (see perlre).
$^S $EXCEPTIONS_BEING_CAUGHT Current state of the interpreter.
$^T $BASETIME The time at which the program began running, in seconds since the epoch (beginning of 1970).
$^V $PERL_VERSION The revision, version, and subversion of the Perl interpreter, represented as a string composed of characters with those ordinals.
$^W $WARNING The current value of the warning switch, initially true if -w was used, false otherwise, but directly modifiable.
$^X $EXECUTABLE_NAME The name used to execute the current copy of Perl, from C's "argv[0]".
$ARGV $ARGV contains the name of the current file when reading from <>.
@ARGV @ARGV The array @ARGV contains the command-line arguments intended for the script. $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program's command name itself. See $0 for the command name.
ARGVOUT The special filehandle that points to the currently open output file when doing edit-in-place processing with -i.
@F The array @F contains the fields of each line read in when autosplit mode is turned on. See perlrun for the -a switch.
@INC The array @INC contains the list of places that the "do EXPR", "require", or "use" constructs look for their library files.
@_ Within a subroutine the array @_ contains the parameters passed to that subroutine.
%INC The hash %INC contains entries for each filename included via the "do", "require", or "use" operators.
%ENV $ENV{expr} The hash %ENV contains your current environment. Setting a value in "ENV" changes the environment for any child processes you subsequently fork() off.
%SIG $SIG{expr} The hash %SIG contains signal handlers for signals.
サイト内検索