Basics Of Perl Special Variables

Perl is an open source, server side, high level dynamic programming language. Originally developed by Larry Wall in 1987. The basic rule of Perl is that a given task can be done in many ways. This is a very important feature perl provides for quick and efficient coding.

It can be used for the system administration as well as to prepare the web interface of that system. Also the cpan library for Perl modules is the largest in the world. No other language has such rich set of modules. Moreover like C, Java etc. Perl is also a cross platform programming language. It is by default installed on almost all the operating systems.

The reason for Perl’s popularity is because it is very powerful for text processing. Moreover it is a user friendly language as it allows users to play with their own programming patterns.

The following are the categories these variable’s are categorized into:

A. Regular Expression variables (These are read only)
B. Default variables and parameters
C. System Variables
D. File-handle or format variables
E. Error Variables
F. Input/Output Variables
G. Others

Regular Expression Variables (These are read only)

1. $’
This is a regular expression variable that holds the trailing part of the pattern match for any given pattern. If the pattern does not match then this variable holds nothing. It is a great feature as it reduces the effort to split the string to get the required sub-string. Thus, Perl does all the hard work to fetch the required substring that is made readily available in this variable.

Example:

my $string = “Incredible”  ; if($string =~ /incr/i) { print $' ; } 

Thus this will print “edible” as the output.

2. $`
This is a variable that hold the preceding part of the pattern match for any given string. This is the same as the above one with a very small difference.

Example:

my $string = “Incredible”  ; if($string =~ /edible/i) { print $` ; } 

Thus this will print “Incr” as the output.

3. $+
This variable holds the string that was last matched. It is useful when we are not sure for which of the sets of the pattern it matched.

Example:

my $string = 'IndiaAsia' ; $string =~ /(Ind) | (Aus)/ ; 

Thus $+ will hold Ind.

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

Leave a Reply