Promo Offers XMAS Offers:
KVM VPS @ UP TO 50% OFF for 3 mos. Learn More Dedicated @ UP TO 56% OFF for 1 mo. Learn More
  • Your Cart is Empty
  • Cart
  • Log In

Perl script

Perl is one of the most popular scripting languages in existence, with it's history going back as far as 1987 - more than 20 years. And the fact that a scripting language has survived more than 20 years in a constantly changing environment speaks for the quality, versatility and the popularity of the language. And just like the name "scripting language" says, the Perl language is designed for the creation of scripts. Scripts in Perl is not that different from a script in PHP or in every other programming language - it relies on variables, arrays, hashes, constants, etc.

Perl script file extension

A Perl script can be created using any basic text editor program. Regardless of the program you decide to use, each Perl file should be saved with a .pl (.PL) file extension in order to be recognized as a functioning Perl script and passed to the Perl interpreter. File names may contain numbers, symbols and letters, but no space. Underscore '_' symbols can be used instead of spaces.

Perl script headers

The first line of every Perl script is a comment line addressed to the Perl interpreter. This line points to the Perl installation path, and on our NTC Hosting servers represents: #!/usr/bin/perl.

When the script is optimized for web environment it contains several HTTP headers, explicating to the script that it's working with a web browser. The HTTP header can look like/or similar to: print "content-type: text/html \n\n". After the HTTP headers are programmed, more essential functionality can be added. This will tell the browser that the content of the file will be text and to skip a line. The “\n\n” part in the end is essential, so don't forget to include it, otherwise the headers will not be read correctly.

Change all occurances of a string in a file to another string:

#!/usr/local/bin/perl
if ($#ARGV != 3) {
    print "usage: chstring oldfile newfile oldstring newstring\n";
    exit;
}
$oldfile = $ARGV[0];
$newfile = $ARGV[1];
$old = $ARGV[2];
$new = $ARGV[3];
open(OF, $oldfile);
open(NF, ">$newfile");
# read in each line of the file
while ($line = <OF>) {
    $line =~ s/$old/$new/;
    print NF $line;
}
close(OF);
close(NF);

The "print" command

The print command in Perl is very similar to the echo command in PHP - it will display the string, passed to it. This is the most simple and basic Perl command.

The "Hello World" script in Perl:

print "Hello World!";

The string in the quotes will be the content, outputted to the web browser. If this is run from the server's command line, you will only see :

Hello World!

Perl Variables

Variables in Perl are defined in the same way as variables in PHP - with the “$” symbol, also know as sygil. A very important thing for Perl variables is that they, like the rest of the Perl commands, are case sensitive. This means that the “$test” and the “$Test” variables will have nothing to do with each other. Variables don't need to be declared before they are used.

A sample set of Perl variables

$a = 10;
$b = "Hello";

This will set the value of "$a" to 10 and the string "Hello" will be the value for "$b".

In Perl you can easily combine the values of two variables. In order to do that, we can use the "." symbol.

$a="Hello";
$b="World";
$a= $a . $b

If we know proceed to print the value of "$a" with the following line :

print $a;

the result will be "Hello World".

You can also combine the value of the variables this way:

$a. = $b

In order to extract the value of the array, we can use the print command. To extract the value of the “Perl” string, we will use the following statement:

Print the value of a sting in an array:

print $sample[0, "\n"];

To print the value of the "ARRAY" string, we will have to refer to it as "$sample[1]". If you add one more string in the same array, we will refer to it with "$sample[2]" and so on.

Perl hashes

Hashes resemble very much arrays - they are also referred to as “associative arrays”. In Perl, hashes are declared with the "%" symbol. A very important feature of the hashes is that they link a key to a value. Each hash can be indexed with these two variables - $key and $value. To create a hash, we use the "()" brackets, and to reference to an element in the hash, the "{}" brackets, as a difference to array referencing, where elements are referenced using the "[]" brackets.

An example of a Perl hash:

#!/usr/bin/perl
print "content-type: text/html \n\n";
# DEFINE A HASH
%coins = ("Twenty five", 25, "Ten", 10, "Five", 5);
# PRINT THE HASH
print %coins;

The result of print command will be:

Five5Ten10Twenty five25

In order to make the output of the hash more legible and understandable, we can use the "$key" and "$value" variables, looping trough them.

#!/usr/bin/perl
print "content-type: text/html \n\n";
# DEFINE A HASH
%coins = ( "Twenty Five" , 25,
"Five" , 10,
"Ten", 5 );
# LOOP THROUGH IT
while (($key, $value) = each(%coins)){
print $key.", ".$value."<br />";
}

The "each()" function used here will go trough the hash, and then will remove the topmost key and value pair, which are then stored in the $key and $value variables, and then printed. Each time the each() function passes trough the array, it will eliminate the topmost key and value pair, until it passes trough all of them.

The output will be:

Five, 5
Ten, 10
Twenty five, 25

Escape Characters in Perl

These characters can be used in any Perl string, formatting it the way you want it.

Character Description
\L Transform all letters to lowercase
\l Transform the next letter to lowercase
\U Transform all letters to uppercase
\u Transform the next letter to uppercase
\n Begin on a new line
\r Applys a carriage return
\t Applys a tab to the string
\f Applys a formfedd to the string
\b Backspace
\a Bell
\e Escapes the next character
\0nn Creates Octal formatted numbers
\xnn Creates Hexideciamal formatted numbers
\cX Control characters, x may be any character
\Q Do not match the pattern
\E Ends \U, \L, or \Q functions

Perl scripts with NTC Hosting

To give our clients maximum freedom, we, at NTC Hosting, offer a Perl-optimized hosting service. All our users can run their own .pl script files quickly and easily. In order to perform these, the Perl script needs file permissions of 755, and to point to the following path: #!/usr/bin/perl. When the above-mentioned requirements are fulfilled, you can enjoy easily running your script inside your Perl-enabled web hosting account.