Perl Programming - SPLessons

Perl Special Variable

Home > Lesson > Chapter 7
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Perl Special Variable

Perl Special Variable

shape Introduction

This chapter demonstrates about the Perl Special Variables these re-contain some predefined meaning and denoted with their real name or punctuation symbols and following are the concepts covered in this chapter.
  • Special Variables

Special Variables

shape Description

Special variables in Perl are those which have some predefined which means. These variables denoted with either real Name or Punctuation symbols. we have the special variable for all the Perl supported Variables which are shown below
  • scalar special variables
  • Array special variables
  • hash special variables
When user needs to use the special variable with its name, then the user need to load a Perl module 'use English', to explicitly say Perl interpreter that we are going to use special variables using its Name following are the special variables with its description. scalar special variables The table below demonstrates the scalar special variables are shown below.
Variable Description
$_$ARG newline.
\t Which is the default variable used to store the current values.
$/ Which is the input record seperator and have the default value of '\n' which is new line charecter.
$0 or $PROGRAM_NAME Which stores the file name of the perl script.
$. Which holds the current reading line number.
$\ Which is the output record seperator and default values of these variable should be emepty and user can assign value to these variable. Which will be used by the print() statement while printing the output.
$, Which is the output field seperator and default values of these variable should be zero and user can change the value to these variable. Which will be used by the print() statement while printing the output.
$# Which is the variable used for the output format while pinting the numbers.
$=$FORMAT_LINES_PER_PAGE Which hold the current page length of the read file.
$%$FORMAT_PAGE_NUMBER Which hold the current page number of the read file.
$~$FORMAT_NAME Format Name: Which hold the current selected output by default the file handle name format.
$-$FORMAT_LINES_LEFT Which holds the number of lines left to print from the page.
$^$FORMAT_TOP_NAME Which is used to hold the heading format of the file handler by default which will be TOP followed by file handle name.
$|$OUTPUT_AUTOFLUSH Which is used to flush the output buffer after every write() or print() nd the default value is zero.
$? Status code : Pipe and system call. which Return status of the executed command.
$$ Will hold the running process number of Perl interpreter.
Array special variables The table below demonstrates the Array special variables are shown below.
Variable Description
@INC Holds a list of methods, wherever Perl library modules or scripts are often looked into whereas executing the present script. This @INC is used by use and require statements to appear into those methods for library modules.
@ARGV Which stores the passed command line arguments
@_ Which are used in subroutines when ever passing the parameters to subroutine.
@F Which is the array into which input lines are stored when auto split -a.
hash special variables The table below demonstrates the hash special variables as shown below.
Variable Description
%INC File name will be the keys which will pass the values to those values by using do, use and require.
%ENV System enveronment variables.
%SIG Signals handler.

shape Examples

The example below demonstrates the Special Variables are shown. [perl] my $str= "Hello World"; $str =~ /llo/; print "$&\n"; # this will hold string of the last successful pattern match print "$'\n"; #this will hold remaining string after patter match is done. print "$`\n"; #this will hold the string before the patter match. $\= "--"; #output record separator (Check output of print on output screen) my $a =5; my $b =6; print "$a"; print "$b"; $\="\n"; my @array = qw(a b c d); print ""; $"="-";# output record separator arrays (Check output of print on output screen) print "@array"; [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image.

Summary

shape Key Points

  • Special variable have some predefined mening.
  • Special Variable denoted by Punctutaion symbols.
  • Perl supports several special variables.