Saturday 31 December 2016

Some common special variables in perl

Perl offers a large number of available special variable which we can use for different purposes in our scripts. This article will discus some of those variables which I've encountered & used thus far.

The $_ and @_ variable: 
The $_ and @_ variables are commonly refereed to as the default scaler & default arrary variables in perl and aptly so. When we use a loop or conditional statement or write a function, if we do not explicitly specify an input variable to the body of the loop or the conditional statement or the function then the content within the $_ or the @_ is taken as the input. These are perhaps the most commonly used special variables.

The $^O variable:
This variables tells us the OS type like linux or SunOS etc. This would help to make the scripts more portable like we can use a certain set of statements depending on the OS.

The $^V variable:
This variable contains the value of the version of perl running on the current system.

The $0 variable:
This variable contains the name of the perl script being executed.

The $ARGV/@ARGV variable:
The @ARGV variable contains the list of all the command line arguments supplied to the perl program. The scaler version would contain the individual command line argument values. The default value of ARGV scaler variable is -1 so it is advisable to change it's value to 0 when using it in scripts.

The $^I variable:
This variable is used when we want to perform some inplace changes to a file. I've used it in the script to demonstrate inplace file editing the article inplace-file-editing-in-perl.

The $| variable:
This is the output autoflush variable. It's helpful in scripts involving input being given by the user. Sometimes in programs requiring user input, the output gets buffered i.e the program waits for the user input but we do not see the prompt for it. Once we feed the input the entire program including the user input prompt gets executed in one go. Setting the value to $| to 1 helps prevent this situation.

The $. variable:
This variable contains the line number when working with files/lists.


Given below is a sample script to demonstrate the usage of the above mentioned variables. There are three more variables which I'll discus after this.

[root@alive ~]# cat 123.pl
#!/usr/bin/perl -w
#
use strict ;

hey ('one', 'two') ;

sub hey {
        print "@_ \n" ;
        }


my ($a, $b) = (10, 15) ;

print "$a and $b \n" ;

print "$^O \n" ;

print "$^V \n";


$#ARGV +=1 ;

print "$ARGV[0] \n";

print "$0 \n" ;


open (FH1, "testfile") || die "error $!" ;

while (<FH1>) { print "$. $_" ; }

$| = 1 ;

print "enter your name:" ;
my $name = <STDIN> ;

print "you entered $name \n" ;


The execution of this script yields the following output:

[root@alive ~]# ./123.pl TEST_in
one two
10 and 15
linux
v5.16.3
TEST_in
./123.pl
1 this is line 1
2 this is line 2
3 this is line 3
4 this is line 4


The last three variables I'll be discussing pertain to formatting our output.

The $\ variable:
This variable comprises of the value of the default output record separator.

The $, variable:
This variable comprises the value of the default output field separator.

The $" variable:
This variable comprises of the value of the default output field separator when we are working with double quoted string content.

Here is a sample script demonstrating the use of these three special variable:

#!/usr/bin/perl -w
#

$\="\n\n" ;
$, = "===";

open (FH, "testfile") || die ;

while (<FH>) {
my @art2 = split ;
         print "$art2[0]", "$art2[1]", "$art2[3]"  ;
 }

$" = "^-^" ;

my @ar = qw (sahil suri) ;

print "@ar" ;


The execution of the above script yields the following output:

[root@alive ~]# ./a.pl
this===is===1

this===is===2

this===is===3

this===is===4

sahil^-^suri


No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...