MFATUTORIAL: Perl

Posted by MahFreenAmeh on Feb. 1, 2007, 10:31 a.m.

Welcome, students, to the wide world of Perl. Not really that wide, actually, now that I think about it. But perl is a simple language, and can acchieve most anything you want to when it comes to a programming language. Some drawbacks about perl:

*It's not necessarily an Object-Oriented langauge

*It has no support for Pointers

*Doesn't compile as fast as programs made in other languages (It's actually interpreted, so it can run on any system like Java)

*The syntax can become confusing after a while.

The good side:

*It's simplified. Almost anything you can accomplish with any other language can be accomplished with Perl.

*Regex support

*Simple GUI support with Tk

*Has header files, like C(++), and can be compiled and make'd with links to C files to actually include C functions.

That being said, I'm going to start going over perl itself.

At the beginning of every perl program, there is the Hashbang line. The hashbang will be different based upon your path to Perl.exe, or if you're using linux, just perl. For instance, my old perl path on windows was C:perlusrinperl.exe

So my hashband line would be

#!C:/perl/usr/bin/perl.exe

OR

#!/perl/usr/bin/perl.exe

OR

#!C:perlusrinperl.exe

It all depends on how you like to write it. The hashbang line is used to show the path to perl so that the script reading it knows what program to use in order to interpret it. If you're making CGI pages for a website, this is necessary, as without it, the script won't properly be interpreted.

Next, we're going to go over arithmetic. Perl supports the basic arithmetic of all languages: +, -, /, *, ^, Grouping via ( ), ++ (Postfix and prefix), – (Postfix, and prefix), and many mathematical functions as well. The functions are most likely contained within a perl module, just for your information.

In order to use a perl module, you have to make sure it exists. For me, these would have been in C:perllib. All of your modules will go into your lib directory. In order to use a perl module, anywhere in your script, put the keyword use, or require, then the name of the perl module. For instance, to use Tk, you put the line: "use Tk;"

Lines are ended with semi-colons. Just a random fact.

Perl has support for functions, but they are known as subroutines in Perl. THey are defined as such:

sub (subroutine name)(PROTOTYPES) {

#code

}

Comments in Perl are shown via #.

Anyways, with subroutines, you don't actually put the return values in the subroutine name, like in C, and what not. Rather, you have to get them individually through use of a special variable, @_. Perl has many special variables, which I will not go over here. Anyways.

Say you made a subroutine that wants to print user input via arguments.

YOu can do this two ways. If you're expecting a set amount of arguments, you can access them using $1, $2 … $n. Note that once you get above $9, you will have to write like ${1}0, so as to not confuse the interpreter.

If you just want to use all of the values, you can do this:

@arg = @_;

foreach $word (@arg) {

print "$word";

}

Note that you don't have to put $word in quotes, but it will work that way.

If you want to get values one at a time, you can use the shift command.

$arg1 = shift;

$arg2 = shift;

Et cetera.

Now let's see an example.

METHOD 1

sub puser {

print $1 . $2 . $3 . $4; #. is the concatenation operator in perl

}

METHOD 2

sub puser {

foreach $arg (@_) {

print $arg #Note that you don't have to store @_ in another array.

}

METHOD 3

sub puser {

$a = shift;

$b = shift;

print $a . $b;

}

So on and so forth. TO call a subroutine, you can do it as follows:

&puser(args)

&puser args

puser args

puser(args)

THe return function of most languages is one in perl as well, it does as is expected. IT returns a value.

All variables in perl start with either a $, @, or %. $ represents a scalar variable, or a normal variable. @ is an array, and % is a hash, an associative array. They are used as such.

$a = "blah";

@b = ("Blah","blah")

OR

$b[0] = "blah"

$b[1] = "blah"

When calling values of arrays in functions, replace @ with a $.

%a = {"Word1","Value1"}

more to come later.

UPDATE

When calling an associative array, or, as they are more often called, a hash, you call them as such:

$a{'Word1'}

One of the best things about perl is the ability to use variables WITHIN strings, somewhat like printf in c, but you don't have them as arguments. You can just type in $varname inside of a string, and it substitutes the name with the value the variable holds. In order to do special characters, such as ", $, and other reserved characters within strings, you escape them with a backslash ()

I think that's enough for now. If anyone has any questions, I'll address them in the next Tutorial.

~G

Comments

DesertFox 17 years, 9 months ago

Yay fer you. I'm going through hellfire and damnation trying to get java to run batch files.

melee-master 17 years, 9 months ago

Heh, interesting. I might have to learn Perl.

MahFreenAmeh 17 years, 9 months ago

Trying to get it to run batch files? I would think that it would be as simple as issuing a system call. Does java support that? Because I could do it easily in Perl.

system("pathtofile");