|
| | Submitted on: 12/31/2001 11:17:26 AM
By: T. E. Geek
Level: Beginner User Rating:
By 4 Users Compatibility:5.0 (all versions), Active Perl specific
Users have accessed this article 5360 times. | (About the author) |
| | A friendly and easy approach to tackling perl for the first time. Hopefully, those considering to learn this wonderful language, or those struggling with the basics will benefit from this brief tutorial. It introduces the language, it's strengths, and leaves you able to write a "goodbye world" program. | | | Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. | 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.
Happy Coding!
| | Other 3 submission(s) by this author
| | | Report Bad Submission | | | Your Vote! |
See Voting Log | | Other User Comments | 1/3/2002 2:41:41 PM:Mehdi Hanbali That was great. I wanted to learn perl
for a long time now, and thanks to this
tutorial it inspired me. I hope it's
easy as you say, after all... I do have
a background in many other languages
:)
Good work!
| 4/1/2002 9:53:08 AM:Disabled oh my god, thats a very good explaining
tutorial.. thank you BUT at this
place:
---- @list = ("hi!", 2, "LIKE
OMIGOD!!!", 42); -------... WHAT does
the number 42 mean?.. i mean what does
it do there?
| 4/1/2002 12:01:31 PM:T. E. Geek Hey-- actually that @ thingie magig
just signifies that we're creating an
array. An array is defined using a
list- all of that junk between the
parenthesis. Basically, each of the
things inbetween those parenthesis were
list elements- or in perl, as we call
em', list slices. Those are actually
just constants. The 42 is an arbitrary
constant, the "LIKE OMIGOD!!!" is an
arbitrary constant. All of those values
are simply members of array "list"
(@list). I can access the value 42, for
example, by typing "$list[3]" since it
is the fourth element in the list (the
list starts at zero, which is why we
use 3). In short, 42 is just a value in
the list- nothin' special.
| 7/1/2002 6:24:04 AM:Ian Lesnevski Great tutorial, but I'd like to make
some correction : in contemporary
implementations Perl is NOT a pure
interpreted language, but something
like Java : it "compiles" the code into
a special bytecode (not a machine
(*.obj) code), and then a special Perl
virtual machine (VM) similar to the
Java VM , interpretes this bytecode.
Unlike Java, Perl is not yet capable of
saving the bytecode for further use. So
Perl is not interpreting the code "line
by line" like an actor.Anyway , it is a
very good tutorual.
| 2/4/2003 1:53:57 PM:Simbro Great tutorial! Thanks! 5 globes!
| | Add Your Feedback! | Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. | | |