System variables used in Perl

Perl is a powerful, adaptable and dynamic programming language which is compiled each time before running. It was first developed by Larry Wall in late 1980s. Today we will discuss briefly the system variables used in Perl.

The following are system variables used in Perl:

1. $$
This variable returns the process ID of the process that is running the current Perl script. It is very handy where the operations are to be performed using the script’s process ID.

2. $<
This variable returns the user ID of the current process.

3. $>
This variable returns the effective user ID of the current process.

Note: The difference between current and effective user is that effective user is running with the privileges of that user id. Basically the real is the user id of the user and current is the process that started you.

4. $(
This variable holds the real group ID of the current process.

5. $)
This variable holds the effective group ID of the current process.

6. $0(zero)
This variable holds the name of the file the current Perl process is executing.

7. $^X
This variable stores the interpreter path or the binary of Perl that got executed.

8.  $]
This variable is very helpful to get the version number of the perl interpreter. This helps to trace the version compatibility issues that occur with various modules.

9.  $^O
The name of the operating system the Perl code was built on. This is similar to $Config{‘osname’}.

10.  $^T
This variable returns the time at which the script was executed. This time is in seconds since the beginning of 1970.

11.  $^W
Returns the value of the warning switch. It is either TRUE or FALSE. Warnings is a Perl program to control additional Perl warnings. The syntax to enable a warning is to “use warnings” and to disable them is “no warnings”.

12.  %ENV
This special hash variable maintains all the environment variables. Basically environment variables are used to store the information that is used to run the system/computer. One can change the values of these variables but these changes are available within the program. Once the program exits, these changes loose their respective scope. Every Perl process is provided with a copy of these environmental variables.
One can print out all these environment variables using:

foreach (keys(%ENV)) {
print “Key:$_ and its value: $ENV{$_}\n”;
}

13.  %SIG
This is also a special hash variable that can be used to set signal handlers of various signals. This makes signal handling quite easy as one can set a signal handler to a certain value and again reset its value to default.

Similar to %ENV, %SIG can also be printed using:

foreach (keys(%SIG)) {
print “$_”;
}

My system provides the following list of signals:

NUM63,TRAP,NUM42,URG,STOP,NUM39,NUM62,NUM43,USR2,NUM57,NUM56,RTMAX,NUM59,VTALRM,CONT,NUM45,
NUM61,TERM,NUM44,NUM36,NUM32,BUS,NUM40,NUM51,
IOT,STKFLT,NUM41,KILL,QUIT,NUM37,NUM50,
ABRT,NUM48,CLD,NUM35,NUM38,TTOU,IO,TSTP,
PROF,NUM53,NUM58,SEGV,RTMIN,POLL,PIPE,SYS,
NUM46,PWR,CHLD,HUP,FPE,NUM54,XCPU,TTIN,NUM52,
NUM55,XFSZ,NUM33,INT,NUM49,UNUSED,WINCH,USR1,ILL,ALRM,NUM47,NUM60

14.  @INC

This special array is similar to shell’s PATH variable. Although, PATH contains a list of directories that are to be searched for executables, @INC contains a list of directories from which Perl modules and libraries can be loaded.

When we code use()” or require() or do(), Perl searches the list of directories from @INC and searches the requested file in this array. If the file is not present then one has to define it separately.

15.  %INC
This is special hash that is used to store the names of the files and modules that were successfully loaded and compiled for a given Perl script. Before loading and compiling any file or module Perl initially checks for its availability in this special hash and thus if not found loads and compiles it accordingly.

This entry was posted in Perl and tagged , , , . Bookmark the permalink.

Leave a Reply