Log in

View Full Version : So you're crazy/stupid enough to learn Perl?



ComradeRed
31st August 2006, 03:16
So you're crazy/stupid enough to learn perl?

I've been axed to post this tutorial I wrote for the benefit of several members :D

Step One: Getting Perl

First off, I will assume that if you are on Windows, you do not have Perl. But if you have a *nix machine, you are smart enough to not need this tutorial.

So, the first step is to get PXPerl (http://pxperl.com/). Then you install it, and configure it however you please.

You will also want to change the settings of windows to allow you to change the file ending.

Step Two: the Basic Basics

Now that you have PXPerl installed, you will need to learn the first step of programming...variables. Just like math, programming uses variables.

Some programming languages are easy with it, like Perl, others have a number of different types, like decimals ("doubles"), integers, characters, Strings (a list of characters), booleans (something that is true or false), etc.

In Perl, a variable is designated by a sigil in front of the name of the variable. Take for example, the following code:

#This is a comment, this is myFirstPerlProgram.pl
#This is another comment
#the program ignores comments
#and is used to tell other programmers
#what you were thinking when you fuck up really badly
#and for your apologies

$hiya; #this is a variable declaration note the semicolon at the end of the line!
$hiya = "Goodbye, Cruel World!"; #this sets the variable $hiya to be "Goodbye, Cruel World!"
print ($hiya); #this prints the variable to the screen

Create a file called "myFirstProgram.pl" and put this in it. Then right click on the file and select "run".

Printed to the screen should be Goodbye, Cruel World!

The function print( ... ) prints to the screen the variable within it. But we can use print( ... ) another way too. Consider the following program:

#p2.pl
#when you get good, sign it like a book
#unless you are so ashamed of your work that you don't want credit for the disaster :p
print("This is one crappy program if it only prints this out.");

which prints out everything within the quotations.

You can also use single quotes ('this is one....this out') would work as well, but it has to begin and end the same way! Do not write something like 'I hope this works" because it won't.

A nifty thing that you can use while printing is carriage returns. You can create a new line using "\n", consider the following:

#p3.pl
#teaching the importance of carriage returns
print("Good\nbye\n cruel world?");

This should print:

Good
bye
cruel world?


Homework :P

Lab1: Write a program that sings "Happy birthday" to the user, saying "Happy birthday dear user!" instead of the user's real name.

Input From The Keyboard

Suppose you want the user to put something in...like his or her name. Well, you can store it as a variable! Take the following for example:

#p4.pl
print("How old are you?");
$age = <STDIN>; #store the user input as a variable $age
print("You're an old fart!");

The <STDIN> is the standard input from the keyboard. You store it as a variable &#036;age in this example.

Notice that whatever age you put in, even negative ages, you are considered an old fart&#33; We&#39;ll look at how to change the output depending on the input next (which is the bread and butter of most programming).

Homework:

Modify the happy birthday program to first ask who&#39;s birthday it is, then sing happy birthday to whomever.

Controlling the Flow

This next section assumes the reader knows basic logic operators like AND and OR.

Take the above program for example, how can we determine if you really are an old fart or not? Well, you would need a boolean statement...something that is true or false (in programming, 1 or 0; that&#39;s right, truth is 1...deep philosophical mystery solved&#33;).

Well, using conditionals. If you are older than (say) 18, then you are an old fart. If you are younger than 0, you are so old you are senile.

The conditional operators that come into effect are:
&#036;a == &#036;b (a is equal to b)
&#036;a > &#036;b (a is greater than b)
&#036;a >= &#036;b (a is greater than OR equal to b)
&#036;a < &#036;b (a is less than b)
&#036;a <= &#036;b (a is less than OR equal to b)

We program this using if(...) and else, take a look:
#p5.pl I think...
print&#40;&#34;How old are you?&#34;&#41;;
&#036;age = &#60;STDIN&#62;; #store the user input as a variable &#036;age
if&#40;&#036;age&#60;0&#41;
{
print&#40;&#34;You&#39;re an old fart&#33;&#34;&#41;;
}
else
{
if&#40;&#036;age&#62;18&#41;
{
print&#40;&#34;You&#39;re an old fart&#33;&#34;&#41;;
}
else
{
print&#40;&#34;You&#39;re a young fart&#33;&#34;&#41;;
}
}

Note how there is a "nested" if(...) else within the first else. You could keep doing this forever (though it is hard and boring to read).

If the condition of an if statement is true, it does what is in the brackets. Otherwise, it goes to the else statement (if one exists, it doesn&#39;t have to).

To be neat, clean, and not drive programmers into a homicidal rage, there is a better way to program this that is easier on the eyes. We introduce the else if(...) statement, which is rather simple:

#p5.pl I think...
print&#40;&#34;How old are you?&#34;&#41;;
&#036;age = &#60;STDIN&#62;; #store the user input as a variable &#036;age
if&#40;&#036;age&#60;0&#41;
{
print&#40;&#34;You&#39;re an old fart&#33;&#34;&#41;;
}
elseif&#40;&#036;age&#62;18&#41;
{
print&#40;&#34;You&#39;re an old fart&#33;&#34;&#41;;
}
else
{
print&#40;&#34;You&#39;re a young fart&#33;&#34;&#41;;
}

Now, in programming, you should try to be as effecient as possible. So, we&#39;ll learn the importance of our good friends AND and OR.

This can be used in our program to simplify things considerably&#33; The AND in programming is represented as &&, and the OR in programming is represented as ||. You use this if you are using multiple booleans in a single IF statement, consider the following:

#p5.pl I think...
print&#40;&#34;How old are you?&#34;&#41;;
&#036;age = &#60;STDIN&#62;; #store the user input as a variable &#036;age
if&#40;&#036;age&#60;0 || &#036;age&#62;18&#41; #if you are EITHER younger than 0 OR older than 18
{
print&#40;&#34;You&#39;re an old fart&#33;&#34;&#41;;
}
else
{
print&#40;&#34;You&#39;re a young fart&#33;&#34;&#41;;
}
What do you think would happen if we used AND instead of OR? Would it have worked? Try it out, don&#39;t be afraid to look errors straight in the eye.

Homework:

Ask the user what year the war of 1812 was fought in, then tell the user if he is right or wrong.

Arrays

So, now you know that a variable is denoted by the sigil in front (&#036;variable). But what if you have a million variables and you don&#39;t want to remember a million variable names?

Well, you could use an array. It stores a number of variables in one location. This time you use the &#39;at&#39; to denote arrays (@array_away). This is like a list of variables.

&#036;array_away[0] accesses the first component of the array, &#036;array_away[n+1] accesses the Nth component of the array.

Take the two code segments for example:

&#036;hi = &#34;Hello...world?&#33;?&#34;;
print&#40;&#036;hi&#41;;


#the same thing except with arrays
@hi;
&#036;hi&#91;0&#93; = &#34;Hello&#34;;
&#036;hi&#91;1&#93; = &#34;...&#34;;
&#036;hi&#91;2&#93; = &#34;world&#34;;
&#036;hi&#91;3&#93; = &#34;?&#33;?&#34;;
foreach &#036;foo &#40;@hi&#41;{
print&#40;&#036;foo&#41;;
}

Note the foreach loop, which goes through each of the entries of @hi and stores it temporarily as &#036;foo. This is then printed to the screen.

Step 1: loop, Step 2: goto Step 1

Now, in programming, there is a time saving thing called loops.

The way it works, there is some boolean condition and while it is true, it repeats. This is a while-loop.


while&#40;boolean == true&#41;{
do crap;
}


Or, if you need something to occur a certain number of times, you can use a for-loop:

for&#40;&#036;a=0; &#036;some_big_number&#62;&#036;a; &#036;a++&#41;{
do_crap;
}
OR
for&#40;&#036;a=some_big_number; 0&#60;&#036;a; &#036;a--&#41;{
do_crap;
} Note that the ++ and -- operators adds one and subtracts one respectively.

This is the bread and butter way of looping.

Note also that:
&#036;a+=&#036;b is the same as &#036;a = &#036;a + &#036;b
&#036;a-=&#036;b is the same as &#036;a = &#036;a - &#036;b
&#036;a*=&#036;b is the same as &#036;a = &#036;a * &#036;b
&#036;a/=&#036;b is the same as &#036;a = &#036;a / &#036;b
which sets &#036;a to be equal to the right hand side. Algebra does not apply here&#33;

Homework

Create a madlibs using <STDIN> to be put in an array, telling the user "A verb..." or whatever, then print out the story with the words substituted in.

Redundancy, Repetition, and Redundancy: An Introduction to Functions

So, by now, you know some of the basics of Perl Programming. We&#39;re going to cover next something that is key to programming: functions...though in Perl they&#39;re called subroutines for some reason.

Remember the analogy of variables in math and in programming? Well, there are also functions. The reason why is because if you are going to do one thing over and over again, it&#39;s easier for everyone if you just create something that does it over and over again.

So take this simple function for example:

#p6.pl ???
sub myFirstFunction {
my &#036;varParameter = @_;
return &#036;varParameter*6;
}
&#036;f = myFirstFunction&#40;&#34;hello&#34;&#41;; #this will be interesting for you if this is your first time
print&#40;&#036;f&#41;;

The sub indicates we&#39;re going to have a function. There can be a list of my ... which is the parameters of a function.

The @_ is the arguments of the subroutine (the function).

Consider the two examples of functions:
f(x,y,z) = x+y+z;

sub f{
my &#40;&#036;x, &#036;y, &#036;z&#41; = @_;
return &#036;x+&#036;y+&#036;z;
}They are exactly the same.

Homework


File Input/Output

That is, writing to and reading from a file. Well, this is quite simple really.


#p7.pl
open &#40;FILE, &#34; C&#58;&#092;myfile.txt&#34;&#41;; #the variable FILE will be used to call on C&#58;&#092;myfile.txt

#this READS only, no writing
#to write, you must say open&#40;FILE, &#34;&#62; C&#58;&#092;myfile.txt&#34;&#41;, NOTE THE &#62;
#and to add to the end of the file, you have to include the &#62; twice
#so it reads open&#40;FILE, &#34;&#62;&#62; C&#58;&#092;myfile.txt&#34;&#41;;

@stuff_in_the_file = &#60;FILE&#62;; #this stuffs the stuff into the array

foreach &#036;foo &#40;@stuff_in_the_file&#41;{
print&#40;&#036;foo&#41;;
}
close&#40;FILE&#41;; #this closes the file


To print to a file:


open&#40;FILE, &#34;&#62; C&#58;&#092;newFile.txt&#34;&#41;;
printf FILE &#40;&#34;ComradeRed is the best human alive.&#34;&#41;;
close&#40;FILE&#41;;

There are other ways to do it, but it is tedious and will require some more work.

bezdomni
31st August 2006, 04:33
:D

I <3 ComradeRed

which doctor
31st August 2006, 04:34
Now all we need is a Ruby on Rails tutorial&#33;

:P

ComradeRed
31st August 2006, 04:39
Originally posted by FoB+Aug 30 2006, 05:35 PM--> (FoB &#064; Aug 30 2006, 05:35 PM) Now all we need is a Ruby on Rails tutorial&#33;

:P[/b] Bah, it&#39;s unlikely that I would ever write a tutorial for such a wretched language.

Maybe my next one will be on Assembly :lol:


[email protected] 30 2006, 05:34 PM
:D

I <3 ComradeRed This is only the first one, the next one will probably be on Java or C++...and be a lot better since I use those more often.

Maybe a tut on switching from Windoze to Linux would be nice :D

(What am I saying Linux for? I mean Minix :P)

1984
7th September 2006, 04:45
Originally posted by [email protected] 31 2006, 01:40 AM
Maybe my next one will be on Assembly :lol:
For which machine...?

:P

In my current computer programming course I&#39;m screwing around the HC11. I spent my whole weekend trying to animate four 7-segment displays in order to draw a crappy animation.

People say that Assembly-ing that thing is the "baptism of fire" of electric and computer engineers here...

Sure, later we will work with C, but from what I&#39;ve heard, it gets even worse.

(...)

Anyway, an Assembly guide would fit like a glove to me right now.

:)

apathy maybe
12th October 2006, 07:49
Looks like a good tutorial, if I was ever interested in learning a language where it was possible to do heaps of stuff without a single letter I will have a look at it.

In the mean time I will stick with my basic PHP, though I know it is easy to create an application full of security holes.

Janus
13th October 2006, 01:28
Looks like a good tutorial, if I was ever interested in learning a language where it was possible to do heaps of stuff without a single letter I will have a look at it.
I would recommend Python rather than Perl for beginners.

There are so many commands in Perl that some people have literally made poems using the Perl language.

ComradeRed
13th October 2006, 21:37
Originally posted by [email protected] 12 2006, 02:29 PM

Looks like a good tutorial, if I was ever interested in learning a language where it was possible to do heaps of stuff without a single letter I will have a look at it.
I would recommend Python rather than Perl for beginners.

There are so many commands in Perl that some people have literally made poems using the Perl language.
Well, Python is good for creating GUIs. It&#39;s kind of like an open source parallel to Microsoft&#39;s Visual Basic...but doesn&#39;t suck as badly.

Once you learn a programming language, learning another isn&#39;t hard.

Of course you shouldn&#39;t be using Perl for large projects, but that is where scripting languages are really at a defecit: large projects.

There are some exceptions (e.g. if you consider Java a scripting language, then it would be one of them).

If you are absolutely a beginner to programming, I would highly suggest learning Karel++ or Lisp/Scheme (Karel++ is a programming language where you command a robot to do stuff on your screen).

For a good elementary (and free) textbook on Scheme, there is always The Structure and Interpretation of Computer programs (http://mitpress.mit.edu/sicp/) (a.k.a. the Wizard Book).