@ARGV

Short Name:          @ARGV
Scope:               always global

This variable is an array of the arguments passed to the script. Unlike the situation in the C language, the first element of this array is the first argument (not the program name). As the arguments are processed, the value of this variable can alter.

Example:

$Example46String = "There were $#ARGV arguments, first argument was @ARGV[0]\n";
print $Example46String;

@INC

Short Name:          @INC
Scope:               always global

This variable is an array of the directories to search for included files. These directories are normally specified either on the command line of the Perl invocation or in an environment variable.

Example:

print "The possible include script directories are: @INC\n";

%ENV,

Short Name:          %ENV{,}
Scope:               always global

This variable is an associative array that links the names of the environment variables to their values. This variable makes it easy to look up a value with the appropriate name.

Example:

 1. $tmp = $ENV{SHELL};
      print "The current SHELL is set to $tmp\n";
 
            2.foreach  $k (keys %ENV)  {
                      print “$k = $ENV{$k} \n”;
               }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       print “\n $k = $ENV{$k} “; 

$[

Short Name:          $[
Scope:               localize

This variable, which is usually set to a value of zero, represents the index of the first element in any array. Programmers who are used to using 1 as the index of the first element of an array could change the value of this variable to suit their preference.

Example:

$[ = 1;
$_ = "AlphaBetaGamma";
$tmp = index($_,"Beta");
print "Beta located at: $tmp\n";
$[ = 0;
$_ = "AlphaBetaGamma";
$tmp = index($_,"Beta");
print "Beta located at: $tmp\n";
 

$PERL_VERSION

Short Name:          $]
Scope:               localize

This variable represents the version string that identifies the Perl version that is being run. You can assign a value to the variable, if necessary. In a numeric context, the variable evaluates to a number made up of the version plus the (patch level/1000).

Example:

$ver = $]+0;
print "So every test has tested the version $] (numeric $ver).\n";

$PROCESS_ID

Short Name:          $$
Intermediate Name:   $PID
Scope:               localize

In systems that support multiple processes, Perl can identify the process number of the Perl script process itself via this variable.

Example:

print "The process ID (PID) is: $$\n";

$PROGRAM_NAME

Short Name:          $0
Scope:               localize

This variable contains the name of the Perl script that is being executed. You can alter this variable if you want the script to identify itself to the operating system as having a particular name.

Example:

print "The program name is: $0\n";
 
 

$OS_ERROR

Short Name:          $!
Intermediate Name:   $ERRNO
Scope:               localize

If an operating-system-error condition exists, this variable is set to the error number (and, if it is evaluated in a string context, to the equivalent error message). You can manually set the error number and then access the relevant error message in a string context.

Example:

 open (FILE,”myfile.txt”) || die “OS Error was $! \n”;

$OUTPUT_AUTOFLUSH

Short Name:          $|
Scope:               always global
File Handle Call:    autoflush FILEHANDLE EXPR

If this Boolean variable (which is associated with a file handle) has a nonzero value, that file is autoflushed (the output is written after each print or write operation) rather than being buffered.

Example:

select(STDERR);
$| = 1;
select(STDOUT);
print "Autoflush setting for STDOUT is $|\n";

$OUTPUT_FIELD_SEPARATOR

Short Name:          $,
Intermediate Name:   $OFS
Scope:               localize
File Handle Call:    output_field_separator FILEHANDLE EXPR

This variable can alter the behavior of the print() function. The default behavior of print(), when it is given a comma-separated list of arguments, is to print each argument with no output separator. You can use this variable to specify any string as a separator.

Example:

$, = "=";
print STDOUT a, b, c, "\n";
$, = "";

$OUTPUT_RECORD_SEPARATOR

Short Name:          $\
Intermediate Name:   $ORS
Scope:               localize
File Handle Call:    output_record_separator FILEHANDLE EXPR

This variable can alter the behavior of the print() function. The default behavior of print(), when it is given a comma-separated list of arguments, is to print each argument. If a new line is required at the end, you must add it explicitly. You can use this record-separator variable to specify any string as the end-of-record string, and you most commonly would set it to the new-line character to avert the need for explicit new lines.

Example:

$\ = "\n";
print "No need for an explicit newline now.";
$\ = "";

$MATCH

Short Name:          $&
Scope:               local (read-only)

This variable references the entire pattern that matched the most recent pattern-matching operation.

Example:

$_ = "AlphaBetaGamma";
/B[aet]*/;
print "Matched: $&\n";

$MULTILINE_MATCHING

Short Name:          $*
Scope:               localize

By default, Perl optimizes pattern matching on the assumption that each pattern does not contain embedded new lines-that is, it is optimized for single-line matching. If you are using a pattern that has embedded new lines, you should set this variable to a value of 1 so that this optimization is disabled and the correct result is obtained.

Example:

print("\nTest 26 Perl Version ($])\n");
$_ = "Alpha\nBeta\nGamma\n";
$* = 0; # Assume string comprises a single line
/^.*$/;
print "a) Assuming single line: $& (which is wrong - the assumption was wrong).\n";
$* = 1; # Do not assume string comprises a single line
/^.*$/;
print "a) Not assuming single line: $& (which is correct).\n";
$* = 0;

$OFMT

Short Name:          $#
Scope:               localize

This variable mimics the UNIX awk utility variable of the same name, which permits numeric formatting. The default value is:

%.2g

See the UNIX awk documentation for information about the possible values.

$# = "%.6g";

print 5467.4567, "\n";
$# = "%.8g";
print 5467.4567, "\n";


0 comments:

Write a Comment!