View Full Version : Short Introduction to Perl

11-13-2010, 11:21 PM
Perl, Python, Ruby are quite famous scripting languages amongst hackers apart from main mainstream C/C++. I got some good tutorials and ebooks from Guru gOOgle on Perl :) I m gonna post a short tutorial on Perl here.You should have perl installed in your system. Its present in some Linux version.

Lets start with the basics, printing some text. Open up any text editor:


print "Hello World!"; #prints hello world to the screen

you can save the file as "all files" with .pl extension. like "hello.pl"

Lets disect this one line at a time:


The first line of every PERL script is a commented line directed toward the PERL interpreter. That way is the commonly used way for Linux users, but what if you're running Windows? Like so:


Make sure to see if you have the right directory set to perl.pl (WILL VARY!!)
Now The # makes it a commented line.
print "Hello World!"; #prints hello world to the screen

The print keyword sends output to the screen, in this case, Hello World! will be sent to the screen as output. Do not forget the semi-colon. There you see the # again which means its a comment and won't show up when you run it. Comments should always be used and will help keep code easy and readable. Both single or double quotes can be used in perl.

Lets move on for printing variables. To declare a string variable, you do the following:
$myVariable = "Hello World!";
my $myVar = "Hello World!";
print "$myVariable";

String variables always have an $ in the beginning following a variable name, but you can also put 'my' in front, which makes it easier in bigger size perl code, than followed by an = sign which means "set to" and then the string in quotations, dont forget the semi-colon and remember variables are case-sensitive!! Now in Perl, there is no int, char, double, long, float. It is all done with the $. Example:

#This defines many different variables
$stringvariable = "This is a string!";
$numbervariable = 5;
$mathvariable = "1 + 1";
#This is called Scalar Variables

print "$stringvariable\n";
print "$numbervariable\n";
print "$mathvariable";

Scalar variables are simple variables containing only one element--a string, a number, or a reference. Strings may contain any symbol, letter, or number. Numbers may contain exponents, integers, or decimal values. Very simple and dead to the point.

Arrays are defined using the @ followed by the array name and then the "set to", or =, then your elements defined enclosed in parenthesis and in quotations and seperated by comma's:
@myArray = (" Zero", " One", " Two", " Three", " Four", " Five");
print "@myArray";
This will output:
Zero One Two Three Four Five

Now, you're probably wondering why we are printing our variables with double quotations around them, and you're probably wondering how you would print out character such as $, @, %, ', " you would do this with a backslash(\) character:
$mystring = "This is mystring!";
print "\$mystring\n";
print "$mystring";

The first print will print the text $mystring and not the variable $mystring because we have used the backslash character. This is called escape characters. the second one will print the value in $mystring because there is no escape characters. You're probably wondering why I keep using \n and this should come easy for alot you. \n is a formatting character, there's a whole bunch of them. \n begins a newline where ever you put it at.

Formatted Characters using backslash:
\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


$newline = "\n";
$allUpperCase = "\U";

print "Hello, Here comes a newline! $newline";
#change the word cat to all uppercase using our variable
print "$allUpperCase cat";

Let's go back to arrays for a little bit, and use this example:

@myArray = ("Zero","One","Two","Three");

Every value in your array is assigned to an element, the elements always start at 0 and so forth. Which means, the value in @myArray, "Zero" is the element 0. "One" is element 1, and so on.

Array values can get hard to read if you have alot of them, with all those quotations and commas, to simplify it you can use qw();

@easierArray = qw(Zero One Two Three Four Five Six Seven Eight);
So this was quick introduction to Perl. Hope you all will like it :) For further study of this language , please download the documentation from the official Perl site. Its very helpful . Thanks

11-15-2010, 10:40 AM
nicce guide .. would help starters and newbies in perl like me a lot :)

11-15-2010, 09:28 PM
me also a newbie bro..these languages r quite interesting :)