師匠の散歩

Perlで遊ぼう

perlvar一覧

出展元
PerlvarPerl predefined variables
Perl Predefined Variables
データの著作権は全てperldoc.perl.orgにあります。

特殊変数を記号から長い名称のエイリアスで呼び出すには、Englishモジュールを使う。バグを回避するために、次のように記述することが推奨されている。

use English '-no_match_vars';

動作が遅くなったり、バグが出るなどと書いてあるので、特に問題なければ、Englishモジュールは使わないほうがよさそうだ。

Engllish
Variable
ExplanationsSample
$ARG
$_
The default input and pattern-searching space.while (<>) {...} #equiv. only in while
while (defined($_ = <>)) {...}

$a $b
Special package variables when using sort(), see sort.@articles = sort {$a cmp $b} @files;

$1 $2 $3
Contains the subpattern from the corresponding set of capturing parentheses from the last successful pattern match.$1 is the same as substr($var, $-[1], $+[1] - $-[1])
$2 is the same as substr($var, $-[2], $+[2] - $-[2])
$3 is the same as substr($var, $-[3], $+[3] - $-[3])
$MATCH
$&
The string matched by the last successful pattern match .$& is the same as substr($var, $-[0], $+[0] - $-[0])
$PREMATCH
$`
The string preceding whatever was matched by the last successful pattern match $` is same as substr($var, 0, $-[0])
$POSTMATCH
$'
The string following whatever was matched by the last successful pattern match .$' is the same as substr($var, $+[0])
$LAST_PAREN_MATCH
$+
The text matched by the last bracket of the last successful search pattern. This is useful if you don't know which one of a set of alternative patterns matched. /Version: (.*)|Revision: (.*)/
&&
($rev = $+);
$LAST_SUBMATCH_RESULT
$^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.$v = "sep:2:match";
$v =~ /(?:(\d)(?{ $a = $^N }))/;
print $a; # prints 2
@LAST_MATCH_END
@+
This array holds the offsets of the ends of the last successful submatches in the currently active dynamic scope. $+[0] is the offset into the string of the end of the entire match.
$+[1] is the offset past where $1 ends.
You can use $#+ to determine how many subgroups were in the last successful match.
%LAST_PAREN_MATCH
%+
Similar to @+ , the %+ hash allows access to the named capture buffers, should they exist, in the last successful match in the currently active dynamic scope.

$*
Multiline matching was removed in Perl 5.10. Use of $* is deprecated in modern Perl, supplanted by the /s and /m modifiers on pattern matching.
$INPUT_LINE_NUMBER
$NR
$.
Current line number for the last filehandle accessed.
$INPUT_RECORD_SEPARATOR
$RS
$/
The input record separator, newline by default. local $/; # enable "slurp" mode
local $_ = ; # whole file now here
$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
$OUTPUT_FIELD_SEPARATOR
$OFS
$,
The output field separator for the print operator. Default is undef.\@arr = (1,2,3);
$, = “ - ”
print @arr; # prints 1 ? 2 - 3
$OUTPUT_RECORD_SEPARATOR
$ORS
$\
The output record separator for the print operator. Default is undef.\@arr = (1, 2, “baz”);
$\ = “\t”
foreach (@arr) { print }
# prints 1 [tab] 2 [tab] baz
$LIST_SEPARATOR
$"
This is like $, except that it applies to array and slice values interpolated into a double-quoted string. Default is a space.arr = (“foo”, “esr”, “rms”);
$” = “ - ”
print “@arr”; # prints foo ? esr ? rms
$SUBSCRIPT_SEPARATOR
$SUBSEP
$;
The subscript separator for multidimensional array emulation. Default is "\034"If you refer to a hash element as
$foo{$a,$b,$c} it really means
$foo{join($;, $a, $b, $c)}

$#
Format printed numbers. was removed in Perl 5.10
$FORMAT_PAGE_NUMBER
$%
The current page number of the currently selected output channel.
$FORMAT_LINES_PER_PAGE
$=
The current page length (printable lines) of the currently selected output channel. Default is 60. Used with formats.
$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.

%-
Similar to %+ , this variable allows access to the named capture buffers in the last successful match in the currently active dynamic scope.
$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. Default is " \n-", to break on whitespace or hyphens.
$FORMAT_FORMFEED
$^L
What formats output as a form feed. Default is \f.
$ACCUMULATOR
$^A
The current value of the write() accumulator for format() lines.
$CHILD_ERROR
$?
The status returned by the last pipe close, backtick (`` ) command, successful call to wait() or waitpid(), or from the system() operator. The exit value of the subprocess is really ($?>>8), and $? & 127 gives which signal, if any, the process died from, and $? & 128 reports whether there was a core dump.

${^ENCODING}
The object reference to the Encode object that is used to convert the source code to Unicode. Default is undef.
$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.if (open(FH, $filename)) {
# Here $! is meaningless.

} else {
# ONLY here is $! meaningful.
# Here $! might be meaningless.
}
%OS_ERROR
%ERRNO
%!
Each element of %! has a true value only if $! is set to that value. For example, $!{ENOENT} is true if and only if the current value of $! is ENOENT ; that is, if the most recent error was "No such file or directory"
$EXTENDED_OS_ERROR
$^E
Error information specific to the current operating system.
$EVAL_ERROR
$@
The Perl syntax error message from the last eval() operator.
$PROCESS_ID
$PID
$$
The process number of the Perl running this script.
$REAL_USER_ID
$UID
$<
The real uid of this process.
$EFFECTIVE_USER_ID
$EUID
$>
The effective uid of this process.$< = $>; # set real to effective uid
# swap real and effective uid
($<,$>) = ($>,$<);
$REAL_GROUP_ID
$GID
$(
The real gid of this process.The first number is the one returned by getgid(), and the subsequent ones by getgroups(), one of which may be the same as the first number.
$EFFECTIVE_GROUP_ID
$EGID
$)
The effective gid of this process.$) = "5 5"
$PROGRAM_NAME
$0
Contains the name of the program being executed.

$[
The index of the first element in an array, and of the first character in a substring. Default is 0,
$PERL_VERSION
$]
The version + patchlevel / 1000 of the Perl interpreter.
$COMPILING
$^C
The current value of the flag associated with the -c switch.
$DEBUGGING
$^D
The current value of the debugging flags.
$SYSTEM_FD_MAX
$^F
The maximum system file descriptor, ordinarily 2.

$^H
This variable contains compile-time hints for the Perl interpreter.WARNING: This variable is strictly for internal use only. Its availability, behavior, and contents are subject to change without notice.

%^H
The %^H hash provides the same scoping semantic as $^H.WARNING: This variable is strictly for internal use only. Its availability, behavior, and contents are subject to change without notice.
$INPLACE_EDIT
$^I
The current value of the inplace-edit extension. Use undef to disable inplace editing.

$^M
Perl can use the contents of $^M as an emergency memory pool after die()ing.# allocate a 64K buffer for use in
# an emergency if Perl was compiled
# with -DPERL_EMERGENCY_SBRK
$^M = 'a' x (1 << 16);
$OSNAME
$^O
The name of the operating system under which this copy of Perl was built, as determined during the configuration process.
$PERLDB
$^P
The internal variable for debugging support.
$LAST_REGEXP_CODE_RESULT
$^R
The result of evaluation of the last successful (?{ code }) regular expression assertion (see perlre).
$EXCEPTIONS_BEING_CAUGHT
$^S
Current state of the interpreter.$^S State
--------- -------------------
undef Parsing module/eval
true (1) Executing an eval
false (0) Otherwise
$BASETIME
$^T
The time at which the program began running, in seconds since the epoch (beginning of 1970).
$PERL_VERSION
$^V
The revision, version, and subversion of the Perl interpreter, represented as a version object.This variable first appeared in perl 5.6.0
$WARNING
$^W
The current value of the warning switch, initially true if -w was used, false otherwise, but directly modifiable.
$EXECUTABLE_NAME
$^X
The name used to execute the current copy of Perl, from C's argv[0] or (where supported) /proc/self/exe.

ARGV
The special filehandle that iterates over command-line filenames in @ARGV . Usually written as the null filehandle in the angle operator <>

$ARGV
contains the name of the current file when reading from <>.

@ARGV
The array @ARGV contains the command-line arguments intended for the script.

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.

@INC
The array @INC contains the list of places that the do EXPR , require, or use constructs look for their library files.
@ARG
@_
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
The hash %ENV contains your current environment.

%SIG
The hash %SIG contains signal handlers for signals.sub handler {
# 1st argument is signal name
my($sig) = @_;
print "Caught a SIG$sig\n";
close(LOG);
exit(0);
}
$SIG{'INT'} = \&handler;
$SIG{'QUIT'} = \&handler;
# restore default action
$SIG{'INT'} = 'DEFAULT';
# ignore SIGQUIT
$SIG{'QUIT'} = 'IGNORE';

戻る
2010 grandmaster all rights reserved. 最終更新:2010/12/28