Home arrow Perl Programming arrow First Steps in Perl

First Steps in Perl

In this first part of a three-part series, you'll learn the fundamentals of programming in Perl. This article is excerpted from chapter one of the book Beginning Perl, Second Edition by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. First Steps in Perl
  2. Our First Perl Program
By: Apress Publishing
Rating: starstarstarstarstar / 0
January 05, 2012

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Every programming language has a number of things in common. The fundamental concepts of programming are going to be the same, no matter what language in which you do them. In this chapter, we’ll investigate the things you need to know before you start writing any programs at all. For instance:

  1. What is programming anyway? What does it mean to program? 
     
  2. How do we structure programs, and make them easy to understand? 
     
  3. How do computers see numbers and letters? 
     
  4. How do we find and eliminate errors in our programs?

Of course, we’ll be looking at these from a Perl perspective, and we’ll look at a couple of basic Perl programs, and see how they’re constructed and what they do. At the end of this chapter, you will be asked to write a couple of trivial Perl programs of your own.

Programming Languages

The first question we should ask ourselves when we’re learning programming is, “What is programming?” That may sound particularly philosophical, but the answer is easy. Programming is telling a computer what you want it to do; and we do this by writing it a program. The only trick, then, is making sure that the program is written in a way the computer can understand, and to do this, we need to write it in a language that it can comprehend—a programming language, such as Perl.

There’s nothing magical about writing a program, but it does call for a particular way of thinking. When you’re telling a human what you want them to do, you take certain things for granted.

  1. The human can ask questions if they don’t understand your instructions. 
     
  2. They can break up tasks into smaller tasks by themselves. 
     
  3. They can draw parallels between the current task and a task they have completed in the past. 
     
  4. Perhaps most importantly, they can learn from demonstrations and from their own mistakes.

Computers can’t yet do any of these things very well—it’s still much easier to explain to someone how to tie their shoelaces than it is to set the clock on the VCR.

The most important thing you need to bear in mind, though, is that you’re never going to be able to express a task to a computer if you can’t express it to yourself. Computer programming leaves little room for vague specifications and hand waving. If you want to write a program to, say, remove useless files from your computer, you need to be able to explain how to determine whether a file is useless or not. You need to examine and break down your own mental processes when carrying out the task for yourself—do you mean a file that hasn’t been accessed for a long time? How long, precisely? Then do you delete it immediately, or do you examine it? If you examine it, how much of it? And what are you examining it for?

The first step in programming is to stop thinking in terms of “I want a program that removes useless files,” but instead think “I want a program that looks at each file on the computer in turn and deletes the file if it is over six months old, and if the first five lines do not contain any of the words ‘Simon’, ‘Perl’, or ‘Camel’”—in other words, you have to specify your task precisely.

When you’re able to do that, you need to translate that into the programming language you’re using. Unfortunately, like any human language, the programming language may not have a direct equivalent for what you’re trying to say. So, you have to get your meaning across using the parts of the language that are available to you, and this may well mean breaking down your task further. For instance, there’s no way of saying “if the first five lines do not contain any of the following words” in Perl. However, there is a way of saying “if a line contains this word,” a way of saying “get another line,” and “do this five times.” Programming is the art of putting those elements together to get them to do what you want.

So much for what you have to do—what does the computer have to do? Once we have specified the task in our programming language, the computer takes our instructions and performs them. This is called running or executing the program. Usually, we’ll specify the instructions in a file, which we edit with an ordinary text editor; sometimes, if we have got a small program, we can get away with typing the whole thing in at the command line. Either way, the instructions that we give to the computer—in this case, written in Perl—are collectively called the source code (or sometimes just source) to our program.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: