Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Introduction to Perl


Introduction to Perl

By ghostghost | 4306 Reads |
0     0

I take no credit for the following tutorial, none of it is mine, and I am just spreading it for others to learn perl who want to.


A note on organisation: This tutorial consists of a brief history of what Perl is, an explanation of how it works and why, and a sample program. The year? 1986. Children were being born, glasses were getting thicker, and computers were getting smarter. While the era of big hair and boring sitcoms was just booming, a very creative unix administrator- Larry Wall- came up with a very good idea. That idea? The Practical Extraction and Report Language (or, as you know it, Perl). Why is it named "PERL" and not "PEARL", you ask? While PEARL would seem to suit the ackronym just fine, another scripting language- one used for graphics- already called dibs on the name. So close, yet so far. Mr. Wall had several daunting tasks set upon his shoulders which he truly did not feel like tackling. I mean, Why would any programmer actually want to take the long way? So- he took the trademark hacker way out- write a program that did his work for him. This program was perl- a program he wrote to - well - "extract reports", and save them to file on his unix network. Since Perl's foundings were in the pit of text themselves, Perl happens to be heralded as being the most powerful text handling language in exsistance. Whew! (Don't be fooled by the competition [Python or C]- if you can do it in perl, it will save you a number of hours, and give you the power that you crave. Oh- and your style may get a bit sloppy- as is charactoristic history, absolute power corrupts absolutely. Now that you have a pretty good idea of where this "Perl" thing came from, allow me to explain what you can use it for. Perl has been lovingly dubbed "The Swiss Army Chainsaw". As you might guess, this means that it can do just about anything with it, and do it with a vengance. Those of you who have programmed in C, C++, Java, or even Basic, know how frustrating it is to use arrays, pointers, or even variables. The good news is that Perl requires NO declarations or any of that irritating junk C/C++ requires. Thats right- all variables are "Variant" (they can be set to any type of value), and lists are merely sets of such variables (pointers are way out of the scope of this article and many introductory books). Yes, I speak the truth. First, let me teach you how to actually create a Perl program. First, ensure that you've got it installed. IF not, head over to activestate.com (or whatever the new website of ActiveState Perl for Windows is [google ;-)]), or if your a unix user head off to cpan.org. Follow the instructions to install Perl- its not difficult at all. Once this has been done, grab your favorite text editor. In it, enter whatever Perl code you'd like, save it as a .pl file, and head off to either DOS or your command shell. In it, type "perl" followed by a space, and the name of your file. Perl will than attempt to interpret it, and run it. If there are errors or warnings they will be displayed. As you may have noticed, I refer to Perl as an interpreter, not a compiler. This is because perl does not turn your code into an obj file, which is than linked together into a binary aplication. Instead, Perl executes your program one line at a time- or however the code dictates- which has a number of advantages and disadvantages- none of which directly affect you at this point. It is for this reason (since Perl is interpreted), that your programs are refered to as scripts- they are truly almost the same as scritps for actors or actresses- they tell the interpreter what to do. Now- on to the code! Perl has three fundamental methods of storing data. You've got scalar variables, lists, and hashes. While this article will not discuss hashes in depth, it will briefly introduce them. First, you've got your scalar. A scalar chops/dices/slices/cuts/and only costs you a tiny amount of memory. You can give it a string, a number, or even a float. It's that simple. Heres what a scalar might look like if you were to write one right now:

$variable = "Yes, I am actually storing a string in a variable";

As you might notice, this assignment is pretty straight forward. You should note that most lines must end with a semicolon. But wait- whats that funky dollar symbol doing up there? That is what's called a derefrencer by some… all it does is say- 'HEY! Perl, you've got a freaking scalar over here'. Eventually, when you start learning some of Perl's functions on your own, you'll find that their output vary's based on context (whether or not the variable you want the function to return is a list, scalar, or whatever). If I had wanted I could have set $variable to any number, even 2345234523.35. Another neat variable type is the list, or array. While this isn't actually a data type (its merely a way of organising scalars), it is one of Perl's strongest points. To use a list you would do somthing like this:

@list = (1,2,3,4);

or even…

@list = ("hi!", 2, "LIKE OMIGOD!!!", 42); As you can tell, it's quite simple to define lists. Simply use the @ (instead of the $) symbol dereferencer before the variable name of choice, type an equal symbol, than IN PARENTHESIS (required), type a comma delinated list of whatever the heck you please. To access items from a list you merey type somthing like this:

$Place_One_Value = $list[0];

Two things may strike you here. First, why did the array suddenly get the dollar symbol next to its name, and, why is there a 0 instead of a one to get the place one value? First of all, all lists start with a 0. The first item is 0, the second is 1, and so on. Sorry- thats just the way it is [however, this can be changed, but doing so is quite advanced]. This code will return whatever the first value in a list is (for example, above, that would either be "hi!"). As you'll note the value returned is NOT a list- it is a single value- and therefor you change the @ list dereferencer to a scalar $ one. If I were to get more than one value from a list:

($one, $two) = @list[0,2]; than you'll note that there is, once again, a @ before the variable name. You can, as you see here, put two scalar's in paranthesis when asigning them values from a list. $one will equal @list[0], and $two will equal @list[2]. Simple? Yup! I'm sure you have no idea what your final program will look like at this point. In fact, I don't have a doubt. I mean, C/C++/VB have tons of things like "void main() { Lots of boringness }", or, "public sub Main()". What about perl? Allow me to show you one of the simplest programs you can write in perl:

1;

Yep, thats a program. Unlike other programming languages, Perl doesnt require all of that extra structural stuff. In fact, the first example (using a scalar) was more than enough for a true program. The only kind of header stuff you may want to use are perl's switches- or just signals to tell the Perl interpreter to look at your code in a different way. For example, many programs begin with a weird "#!" type of line running at the top. What ever might this be? Well, the full line looks like this:

#!/usr/bin/perl -w

Note that this line varies. The /usr/bin/perl thing is simply the path to your Perl interpreter, and the -w is the switch. Perl has a number of such switches, however, for this tutorial you only need know of the -w. The -w (w for warning) enables Perl's warning system. Perl is such a high level and understanding language (hell, it even understands me!) that it by default wont bother you with pesky small errors. It will simply fix them for you in the interpreted version. However, it is a very good idea to put the line "#!/usr/bin/perl -w" at the top of your code (where the /usr/bin/perl is your perl directory) to enable warnings. This will help your code be less prone to errors. Up to this point, you probably have a rough idea of how a basic Perl program ticks. But how do you display data? You can set variables, but what good does that do? This is simple. You use the function "print":

print file_handle list;

For now don't worry about file_handle. By default it is set to STDOUT, which is basically your terminal screen. As for list, all that means is that print works via printing a list.

print "hi","neat";

While that is all nice and pretty, it doesnt really affect us now. For now, we merely need to know how to display some basic information. All you need to do this is-

print "Hello, this is information!!";

as you can see, you simply type print, quotation marks, and what you want to say (don't forget a semicolon!).Now that we know how to print text– how do we print variables?? and lists?? So glad you asked. The beauty of the print function is that (as long as you use double quote ["]) Perl will look in your quotes for scalars or arrays (they MUST have dereferencers):

$variable = "hi!"; print "$variable What's up homie g?"; The output of this will be hi! What's up homie g?

That's it! With a list, it would work like this:

@list = ("nothing", "much"); print "well, @list"; and the output: well, nothingmuch.

As you can see here, the array wont automatically include a space. For now, stick with scalars when outputting values. This brings you up to the very starting point of the programmer. The first complete (well almost first) program he or she will write. The goodbye world program:

#!/usr/bin/perl -w print("Goodbye, cruel world!");

Wow, that was simple! We can even do this with variables like so:

#!/usr/bin/perl -w $goodbye = "Goodbye, cruel"; print "$goodbye world!";

As you can see, Perl is no horror to learn. IN fact, its quite easy. Unfortionately, I can not take you farther than this within this tutorial. However, I hope you managed to pick up the very basics of Perl– to maybe even be intrigued by its many capabilities. The power of Perl is endless. Hopefully your curiosity will be the same. All of this program can run on the internet- a system of complex texts just waiting for Perl to manipulate it. More of such information will be found in my second tutorial, or in some good Perl books you can find online. For example, check out Perl for Dummies by Paul Hoffman- that'll bring you up to speed faster than ever.

Comments
ghost's avatar
ghost 17 years ago

Dude, this is great! If everybody with skills would take the effort to write someting like this there would be way less newbs :D You absolutely interested me in Perl and I will look into it a bit deeper when I have time. Thanks man! You're great!

Rated Very Good

ghost's avatar
ghost 17 years ago

Thank you Ponguile for posting this article. I like that it is simple and provides a basic introduction to perl. This article made me want to look into perl at a deeper level. :)

ghost's avatar
ghost 17 years ago

NOW I CAN USE VARIABLES, ARRAYS AND PRINT THINGS! THX U! LOLZ <3

korg's avatar
korg 17 years ago

If you want to share info post a link in the forums. This is copy and pasted from http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=263&lngWId=6 And give credit to the original author:@ If you check the link there are more articles.

ghost's avatar
ghost 17 years ago

Riight, thanks korg i knew i forgot something!

Credit given to author whose name I am too lazy to look up right now

Also, I can't tell if you guys are being sarcastic or not when your thanking me, because I have mixed ratings xD