1. Perl
  2. here

Perl predefined variable

A list of Perl predefined variable. Predefined variable are global variable that are predefined in Perl. The variable name identifies its use. I will introduce only this from the predefined variable that I definitely want to remember.

@ARGV - command line arguments

command line argument is assigned to "@ARGV".

@ARGV

@_ - Subroutine arguments

argument of subroutine is assigned to "@_".

@_

$1, $2, $3, ... - Regex captured string

For "$1", "$2" and "$3", use regular expression to capture The specified string is assigned.

$1;
$2;
$3;

$_ - Default variable

"$_" Is called default variable. It's best to refrain from using default arguments yourself for readability, but some builtin functions use default variable, so there are a few things to keep in mind.

At a minimum, map function, grep function, Postscript for often uses" $_ ", so keep in mind.

map function

map {$_ => 1} @elements

grep function

grep {$_ eq 'foo'} @elements

Postfix for

$_ for @elements;

%ENV - Getting and setting environment variable

You can access environment variable with a predefined variable called %ENV .

%ENV

The environment variable name is the key and you can refer to the value with $ENV{'env_name'}. Environment variable are like variable that the OS has. Environment variable are copied into the memory of the process space when the OS starts the process. OS environment variable cannot be changed from the process.

Environment variable can be set freely, but they are valid only in that process. When a process launches a child process, the environment variable are copied, as if the OS launched the process.

$0 - Get program name

Use the predefined variable "$0" to get the program name.

$0;

Gets the name when the program was started. In other words, if you specify a program with an absolute path, you can get the program name with the absolute path name from the current directory, and if you specify it with a relative path, you can get the program name with the relative path name.

@INC - List of module search paths

List of module search paths is assigned to a predefined variable called @INC .

@INC

The module search path is the directory that Perl searches to load the module. Modules placed in directories not included in @INC cannot be loaded.

Use the lib module to add a module search path

use lib './lib';

You can add the search path to @INC at compile time by using lib module.

Add the module search path directly to the beginning of @INC

unshift @INC, './lib2';

Add it directly to the beginning with unshift function. It is better to use use lib than this way. The advantage of using use lib is that it adds a search path at compile time, makes it more readable, and adds an architectural path as well.

%INC - Check loaded modules

To find out loaded modules, look at the variable %INC .

%INC

The%INC hash is assigned a combination of module name and module file name. You can see the module name and which file you actually imported.

It has nothing to do with @INC, which stores the module's search path. @INC and%INC are completely different variable. If you want to know only the module name, use keys function to retrieve only the keys.

$^O - Get OS name

To get OS name, use the predefined variable $^O .

$^O

$^O value and OS type

The value of "$^O" in each OS is summarized.

| Windows in general |

$^O value Description and link
MacOS MacOS
MSWin32
os2 OS/2
VMS VMS
epoc EPOC OS (probably)
NetWare NetWare
symbian Symbian OS
dos MS-DOS
cygwin cygwin
linux Linux
freebsd FreeBSD
solaris Solaris
Unix (maybe different) Unix
darwin Mac OS X
CentOS Linux

Other predefined variable

$$ - Get process ID

Use the predefined variable " $$" to get the process ID.

$$

The process ID is the unique knowledge of the process by the OS.To separate it, it is an identifier assigned when a process is started.

$^T - Get the process start time

To get the process start time, use the predefined variable " $^T ".

$^T

The time is obtained in epoch seconds (seconds from 00:00:00 on January 1, 1970).

$? - Child process exit status

Use "$?" To get exit status of the child process.

$?

If wait waits for the child process to terminate, $? Contains multiple values, including the exit status of the child process. $? Is also set when a child process is executed using the system function.

List of all predefined variable

Below is a list of predefined variable. See official document "perlvar" for more information.

|

|

* Predefined variable *meaning
$_ Default argument
@_ Subroutine arguments
$" Separator when the array is interpolated with double quotes
$$ Process ID
$0 The name of the running program
$( Process real group ID
$) Process execution group ID
$< The real user ID of the process
$> Process execution user ID
$; Subscript delimiter for emulation of multidimensional arrays
$a, $b Variable used in the sort function
%ENV environmental variable
$^F Maximum file descriptor
@F Includes fields in each row when automatic split mode is enabled
@INC List of library load paths
%INC List of included file names
$^I Replacement edit extension
$^M Do you want to trap if you run out of memory?
$^O Operating system name
%SIG Signal handler
$^T Time when the program started
$^V Perl version information
$^X Name when executed
$1, $2, $3 String captured with regular expression
$& & The string that matched in the last successful pattern match
$` The part before the string matched by pattern matching
$' The part after the string matched by pattern matching
$+ The string captured at the end of the last pattern match
$^N Last used group in pattern matching
@+ Offset to the end of the last successful match
% + Named capture value with last successful match
@- Last matched first offset
%- Last successful match allows for named capture values, list of values
$^R Result of regular expression assertion
${^RE_DEBUG_FLAGS} Regular expression debug flag
$ARGV File name when reading from & lt; & gt;
@ARGV Command line arguments
ARGV File handle for filename in @ARGV with & lt; ARGV & gt;
ARGVOUT Currently open filehandle when using -i
$, Output field delimiter for print
$. Current line number of filehandle
$/ Input record separator
$\ Output record separator
$| Avoid buffering
${^LAST_FH} Reference to the last loaded file handle
$^A Format predefined variable
$^L Format predefined variable
$% Format predefined variable
$- Format predefined variable
$: Format predefined variable
$= Format predefined variable
$^ Format predefined variable
$~ Format predefined variable
${^CHILD_ERROR_NATIVE} Native status on system calls
$^E Current OS error information
$^S Current interpreter status
$^W warning
$! System call error
%! System call error number and content
$? Child process error
$@ If you catch an exception with eval, its contents will be saved.
$^C Compile switch
$^D Debug flag
${^ENCODING} Perl source code character code
${^GLOBAL_PHASE} Current phase of Perl interpreter
$^H Compile time tips
%^H Has the same scope as $^H
${^OPEN} Internal variable for PerlIO
$^P Internal variable for debugging
${^TAINT} Whether in tein mode
${^UNICODE} Unicode settings
${^UTF8CACHE} Internal UTF-8 offset cache code state
${^UTF8LOCALE} Whether UTF-8 locale was detected

We recommend that you do not use predefined variable until you really need them, as they significantly reduce the readability of your source code.

Related Informatrion