A Guide to the Common Perl Commands and Syntax

A Guide to the Common Perl Commands and Syntax
Page content

Introduction

Perl happens to be quite a huge language so it is beyond the scope of this tutorial to go into every little detail about Perl. Here I will just go through the basic common Perl commands you are bound to encounter in most Perl scripts but for further information on Perl you can find out what Perl Programming is and read more on the uses of Perl here. For purposes of this guide I will mention about Scalar variables and operators. Perl support geometry like assigning of values to variables which are normally prefixed with a $ sign. Eg.

$a = 10

$b = 20

$c = “Hello”;

$d = “World”;

Operators are like the Math equivalent +, -, *, / which represent Addition, Subtraction, Multiplication and Division.

Call Perl Command

To be able to use Perl on your operating system you must begin with a line at the top of your scripts to tell the operating system where the Perl Interpreter has been installed. The syntax is as follows and it depends on the location and version of the Perl installation.

#!/usr/bin/perl

or

#!/usr/bin/perl5

print

Probably the most common Perl command is Print. This command output data to the stdout which could be either monitor, a printer, a file, a pipe or even the Internet.

print “Some text to show on screen\n”;

Here is an example where print is used to count from 0 to 10 assuming $i is initially equal to zero.

print $i++ while $i <= 10;

This second example will produce this output:

1 2 3 4 5 6 7 8 9 10

die

In some instances, if you are running a script and some kind of error is encountered, you can use die to gracefully raise a friendly exception on the particular error. In the example below, suppose we are running a script called “my_script” and an error occurs that we anticipated we could use a perl command like this:

die “/etc/program has stopped running”;

The output would be:

/etc/program has stopped running at my_script line 150

split

This can be used to split a scalar variable based on a delimiter and return all the elements as an array.

For example, if I wished to split the scalar $a using the delimiter “:”, then in this example below the would show the syntax and the results.

$a = “John Doe:200 East Boulevard:Winter Park, Florida”; @array = split ( /:/, $a);

which would make array above have the following values:

$array[0] = “John Doe”; $array[1] = “200 East Boulevard”; $array[2] = “Winter Park, Florida”;

While

While is a loop which runs a sequence of Perl commands based upon a certain condition. The basic Syntax would be:

while ( some condition is true ) { … Perl commands to run … }

So an example in this case assuming $i is initially equal to one, would be:

while ( $i < 5 ) { print “I am $i years old today\n;$i++ }

This would output:

I am 1 years old today

I am 2 years old today

I am 3 years old today

I am 4 years old today

foreach

To go through each item in an an array, you can use the foreach sequence Perl command. In his example, if we have the array

@array = (“Ini”, “Mini”, “Miny”, “Moe”);

We would use the following code to list each item:

foreach $temp (@array) { print $temp . “\n”; }

To get the following output:

Ini

Mini

Miny

Moe

if…else

To test conditions and optionally perform Perl commands based on those conditions, it would be appropriate to use the if…else commands. The basic syntax is follows:

if ( some conditional ){ …. }elseif ( some other conditional ){ …. }else{ …. }

Using the example below, if I wanted to test to see if the there are enough players $players in the soccer team this is what I would do.

if ( $players < 11 ){

print “There are too few players in the team\n”;

}elseif($players > 11){

print “There are too many players in the team\n”;

}else{

print “There are just enough players in the team\n”;

}

open

The open Perl command can be used to open a file.

open ( ID, “my_file.txt” );

Is the basic syntax, where ID is the ID assigned to the I/O stream. This opens a file for reading. To open a file for writing into you can do this notice the angle bracket.

open ( ID, “>my_file.txt” );

To append to a file instead of overwriting it, you can use double angle brackets:

open ( ID, “»some_file.txt”);

close

Whenever you are finished with a file handle or I/O stream you should close it using this command.

close ID;

sleep

The sleep command in Perl causes the script to stop execution for the specified number of seconds seconds, or forever if no argument is given. Example

… some perl commands ….

sleep(10); …. more perl commands

The code above will stop execution and sleep for 20 seconds before proceeding.

exit

This quits running a script and returns the value given after it. The most commons values are 0 for success and 1 for failure.

…. running a perl script …….. if everything is okay then ….exit 0;

mkdir

The mkdir Perl command creates the directory specified by FILENAME, with permissions specified by MASK using the following syntax

mkdir FILENAME, MASK

An example would be

mkdir myfolder, 0777

This creates a directory named “myfolder” and has read, write, execute permissions.

rename

This command renames a file from OLDNAME to NEWNAME using the following syntax.

rename OLDNAME, NEWNAME

An example would be

rename oldfile.txt, newfile.txt

chmod

This Perl command is used to change permissions of a list of files and returns the number of files successfully changed. It is a very common Perl command and comes in handy when setting up and configuring programs and user settings

An example of chmod in use is

$cnt = chmod 0755, “firstfile”, “secondfile”;

The $cnt above has the value of the files that were changed successfully. the 0755 is the octal value for the permissions of the owner of the file, the group members of the file and the users of the file each represented by a single digit.