Beginning Programming Perl(s)

In the last article, you learned how to install Perl. It wouldn’t make a lot of sense to install it on your computer if you never did anything with it, now would it? In this article, you’ll take your first steps to becoming part of a wild and crazy breed — a Perl programmer.

The Perils of Programming Pragmatically in Perl

Originally written as a way to make writing reports simpler, Perl has evolved to include web development, server scripts, and system administration. Some would say it has devolved into some other zany uses as well, like Perl Golf, a game programmers play to try and use the least amount of code possible to program specific challenges. There is also Perl Poetry, for all you wanna-be Shakespeare’s and Edgar Allen TyPoes (haha wordplay!).

Large websites such as Amazon, Ticketmaster, and IMDB.com use Perl. And in addition, you can use it to create graphical user interfaces.

So grab a candle and a crow’s quill, and some ink. And get ready to learn Perl the poetic way.

(Note: I just read a poem in Perl that took the Billy Joel Song, “Uptown Girl” and made it “Uptown Perl”).

A Data Type by Any Other Name Would Still Hold Data

Most programming languages are a real pain in the butt; they are nit-picky and like to play things by the book. Not Perl. Perl likes to sit around in its underwear drinking cans of Mountain Dew and making pyramids out of it. It likes to not take a shower for three days and eat a cold slice of that pizza it ordered a week ago and forgot to put in the fridge.

In your average programming language, you have to define the type of data you are using. Not so in Perl. Perl doesn’t care. It takes what it can get. In Perl, data types, or structures, are built off of the first data type: Scalar.

Scalar Data Types

A scalar, in simple terms, is a single piece of data. It could be a number, a letter, a novel, you name it. But it can only be that one thing. It’s like a folder you create on your desktop. You give it a name and throw some data into it. Done.

For now, we’ll refer to that Folder as a variable. A variable holds data that you designate (or the user designates) that can be changed at a later data (hence the name variable). To let Perl know you are designating a variable as a scalar data type, you must place the $ in front of its name. You can choose to name your variable whatever you like, so long as it consists of only letters, numbers, and underscores. You may not begin the name with a number, however numbers can appear in the name. To keep things uniform, I would use all lowercase letters, and separate words by using our friend the underscore.

Here are some sample names:


$my_stuff

$day_1

$junk

{mospagebreak title=Storing Data in A Variable}

There are many ways to store data in a variable. For now we will focus on the below example:


$my_name = ‘Hamburglar; ‘ # places the name Hamburglar in the variable

$my_iq = 1000; # places the value 1,000 in the variable

You’ll note the # symbol followed by text above. This is known as a comment. Comments are lines of text that the computer cannot see. They are used to let other programmers (and even yourself) know what you intended a line of code to do. When the computer sees the # symbol, it skips the remainder of that line and moves on to the next.

Try the following code:


#!/usr/local/bin/perl

$my_name = ‘James';

print “My name is below:n”;

print “$my_namen”;

The above code will print out the following to your screen:

  My name is below:

  James

You will notice a few new additions to our code. The first is the line:


#!/usr/local/bin/perl

This must appear on the first line of every program you create. It tells the computer which directory to look in.

In addition, we added the print command, which tells the program to print something to the screen. And lastly, we added the n or new line code. This simply informs the program to go to the next line and not print the next line of text on the same line.

{mospagebreak title=Quoth the Raven}

In the above example we worked with a string, or a group of characters. We encased it in single quotes (‘), which lets the program know where the string ends and begins. Which is fine and dandy. But what if you come across a contraction? If you try the following code you will get strange results:


#!/usr/local/bin/perl

$my_sentence = ‘I simply can’t do it!';

print “$my_sentencen”;

Because there is a single quote(‘) in the contraction can’t, the program sees the word can as the end of the string, instead of the word it with an exclamation mark, as we originally intended. To fix this, you escape the quote with the backslash() key.

 

#!/usr/local/bin/perl

$my_sentence = ‘I simply can’t do it!';

print “$my_sentencen”;

To use special characters like the n or to print a variable, we use the double quotes(“). Consider the following:


#!/usr/local/bin/perl

$my_sentence = ‘I simply can’t do it!';

print “$my_sentencen”;

print ‘$my_sentencen';

In the above code, the following would print out:

  I simply can’t do it!

  $my_sentencen

See? You need the double quotes for those special keys, otherwise they will be seen as a normal string of text.

{mospagebreak title=List Data Types}

Sometimes you store stuff in a folder in real life. Now imagine that you have 26 folders in a drawer alphabetized from A-Z. Since you are a programmer, those files probably contain the names and addresses of girls that have rejected you. If you are a young programmer, you are only using one drawer. If you are older, well…you’ve switched from drawers to outdoor storage sheds. Either way, that is an example of a List Data Type.

Simply put, a List is a group of scalars put in order. There are a variety of ways to order the scalars, which we will get into later on. If you are familiar with arrays in other programming languages, you’ll know that a List is the same thing.

Here is how you store data in a List:


@my_cats = {‘Joni ‘, ‘Chachi ‘, ‘Richie ‘, ‘Fonzie ‘, ‘Potsy ‘};

The above creates a list with five scalars in it. If we do the following code:


#!/usr/local/bin/perl

@my_cats = {‘Joni ‘, ‘Chachi ‘, ‘Richie ‘, ‘Fonzie ‘, ‘Potsy ‘};

print @my_cats;

This would print the following:


  Joni Chachi Richie Fonzie Potsy

I can store numbers in Lists also, or mix numbers and characters if I choose. Perl doesn’t care. It’s too busy unwrapping that Hershey bar (the one with almonds).

As you can see, Lists must have the @ symbol as the first part of their name. They use the lowercase naming convention (or at least I do) and it’s best to separate words with underscores, though in truth you don’t have to.

So let’s say a girl wants to use my cat program to see the names of all my cats. I’m clearly embarrassed to have so many (or I would be if I really did have that many). So I would create a program that would only show the name of one cat from my List.


#!/usr/local/bin/perl

@my_cats = {‘Joni ‘, ‘Chachi ‘, ‘Richie ‘, ‘Fonzie ‘, ‘Potsy ‘};

print $my_cats[1];

This would print the name Chachi to the screen. I know what you are thinking and you can cool your biscuits right there (within arms reach…I’m hungry). Why didn’t it print Joni? Simple. Lists start off at 0 and work their way up. To print Joni and Richie, you would use the following code:


#!/usr/local/bin/perl

@my_cats = {‘Joni ‘, ‘Chachi ‘, ‘Richie ‘, ‘Fonzie ‘, ‘Potsy ‘};

print $my_cats[0,2];

You may have noticed in the above examples that I switched the @ symbol to the $ symbol on the last line. In addition to wanting to confuse you, this is also necessary because you are calling individual portions of the item (scalars) and not the entire item itself.

You will note that the sample rules apply for quotes when working with arrays. If I wanted a list of contractions, I would do it this way


#!/usr/local/bin/perl

@my_contractions = {‘Won’t ‘, ‘Can’t ‘, ‘Don’t ‘};

print @my_contractions;

The above code prints:


  Won’t Can’t Don’t

Conclusion

While we are not finished with data types or this series, I am finished with this current tutorial. In the next tutorial, we will wrap up Lists and move on to the third data type, and use operators to manipulate data.

Till then…

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan