MFATUTORIAL: Perl II

Posted by MahFreenAmeh on Feb. 5, 2007, 9:46 a.m.

Welcome back to the MFATUTORIAL for Perl. Yes, I know the term MFATUTORIAL is kind of stupid, but I like it nonetheless. Anyways. I don't quite remember where I left off, but I do believe it had to do with hashes. OR rather, Scalar arrays. So now, I suppose… I'm not sure.

Gathering user input in Perl is rather easy. Define a variable to be from <STDIN>, such as $blah = <STDIN>. STDIN is a built in stream handler in Perl that, surprise surprise, reads from STDIN. Also, unless you want the newline to be in the string that the user gives you, it's best to use the function chomp on the var.

Ex:

$blah = <STDIN>;

chomp $blah;

Chomp strips out the newline operator at the end of the string.

And now, for one of my favourite parts of Perl, a part that I believe should be in all programming languages: Regex.

Regex is short for regular expression, and it allows you to find and substitute within a string. You can use Regex on a previously declared string by typing out the name of the variable, then =~, then the expression.

So, $blah =~ s/[AaEeIiOoUu]/*/g replaces all vowels with *'s. It's just a random expression to show how regex is used. Basically, it goes like this:

s/expression/replacement/[options]

s is there to determine the start, expressions are, of course, the expressions to match, and replacement is the replacement.

Options are optional, and the two most commonly used ones, I'm actually not sure if there are more… Well. The two most common used ones are i and g. i tells the interpreter to ignore case, so AGDAG is the same as agdag to it, and g means global, that is, replace every occurence.

If you want to test for something occuring in a string within an if statement, then you can do the following:

if($var =~ /expression/)

statements

File I/O is rather simple in perl.

To open a file, use the function, open. It is used as such:

open FILEHANDLE, PATH

Note that it's good programming etiquette to type your entire filehandle in uppercase letters.

So, open(TEST,"/blah.txt"); would open a text file, blah.txt, located within the root folder on your computer.

Note that it doesn't open it for output, though. To do such, you add certain operators before it.

open(TEST,"</blah.txt") opens it for reading, open(TEST,">/blah.txt") opens it for input, but not appendation, so it is destructive input, and open(TEST,">>/blah.txt") opens it for appending.

To print to a file, you use print with an extra operator:

print FILEHANDLE TEXT

Note that there is no period.

To assign the entire contents of a file to an array, you do the following: @arr = <FILEHANDLE>. To get the first line, $fline = <FILEHANDLE>

If you want to loop through an entire file until you get to EOF, then do the following:

while(<FILEHANDLE>)

statements

So, the following:

while(<TEST>) { print; }

is a perfectly fine line, what it does is loops through the file, and prints every single line. If you issue print without an argument, it prints what is currently held in $_, one of perl's special variables. $_ contains the current line in the text file, in this case.

To close a file, simply use close:

close FILEHANDLE

Perhaps you want to read from a directory? I personally don't use the opendir/readdir/closedir functions, so you're going to have to learn about those from someone else, but I do use a different format.

@var = </path/to/files/name.extension>

Note that name and extension can be * to open up every file of a certain name, extension, or all of the files with both a name and extension.

@var = </*.*> would open up everything in the root folder of the computer with a . in the middle.

@var = </*.txt> opens up all text fles.

To loop through these, use a foreach loop.

foreach $file (@var) {

print $file;

}

Perl supports something that most languages don't: using a logic operator AFTER a statement. That is, a logic test.

For instance:

print "a" if($var eq "a");

Or, print "a" unless($var ne "a");

I'll go over those in a moment. I just have to explain the significance of ne and eq.

When comparing strings, you always use things like eq, ne, gt, and lt, because any other operators are used for arithmetic, not strings.

Just a little tidbit. Assume that $var = "abc";

$var++ will make $var equal "abd". I thought that was interesting.

Logic control is simple. Just use if, or unless. If is used the same as other languages, and unless is basically the same as an opposite of if. So, if($var eq "a") and unless($var ne "a") mean the exact same thing.

All of the logic gates in other languages exist in perl: OR, AND, and NOT. That is to say, the basic ones. If you want to use them, you can just type in the word, or, and, or, if it's a NOT, then use the ! operator.

Loops are rather simple. There is the all too famous, while loop.

while(test) {

statements

}

For instance, while(++$a<=100) { print $a; }

NOte that you can use the post/prefix autoinc/decrement operators within the test rather than within the statements.

THat prints out every number from 0 to 100, assuming that $a was initialized with a value of 0.

For is the same as in most languages:

for(initializer;test;incamount)

So, for($a=0;$a<=100;$a++) { print $a; } does the same as the previous loop.

Then, there is do…while. It is used as such:

do {

statements

}

while (test);

It's the same thing as a while loop, except it always will run once.

Also, in perl, the else if construct is written as elsif.

I've ran out of things to type about, so I'll update this later, when I actually have something interesting to say.

Comments

Rob 17 years, 9 months ago

wtf is perl?

gtvg 17 years, 9 months ago

They are shiny things found in clams in the ocean. If you're really good, you can make them do things by using a language called 'Perl'. ;P

Rob 17 years, 9 months ago

I realised it was language, but what kind? Like browser based? or exe? or something else?