Home
Username:
Password:
Perl Fundamentals Tutorials

First Steps / Program flow

Subtitles of the Movie

In this chapter we are going to look at some of the basic fundamentals of the way in which a Perl program works. We are going to be looking at the way in which Perl moves through a program that's written in Perl and executes each line of it, we are going to be looking at variables, which are very basic concepts in any programming language, and Perl is no exception to that. We are also going to be looking at the difference between strings and numbers, and what represents a string in Perl. We are also going to be looking at operators, which are symbols that are very important to be able to use in order to carry out operations within our Perl scripts. To begin with let's look at the program flow of a Perl script, and the way in which a Perl script can be anatomized into different parts. The first thing to note is that the Perl executable will read the script from top to bottom. So unless we tell it otherwise, it's going to start at the top of the script, and execute every line of code until it gets to the end. Also the lines of code in the Perl scripts, are delimited by semicolons at the end. So even if a line of code appears to spill onto more than one line, Perl is able to differentiate between each line by looking for the semicolon. So it's very important not to forget the semicolon at the end of each line. We also looked very briefly, at the end of the last chapter, at how important it is to use the so-called shebang line, which invokes the Perl executable. And tells the operating system that this is a Perl script and should be treated as such. As we saw also in the last chapter this is not strictly necessary, if we are going to be running our scripts on a Windows system. What's also important, again we touched on this at the end of the last chapter, is if we are going to be running our scripts on the web, we will need to include our line of code that will produce a header. Now this header has to come before any other output has been written to the web browser. So that's the first thing that needs to appear. Content type has to be told, otherwise the web browser won't know what it's accessing. In fact the web server won't even send any data correctly, won't be able to interpret what the Perl script is trying to tell it to do. So for most cases we are going to be producing HTML. There is also a lot of similarity between the structure of lines of Perl code, and the human language. Functions such as the print function, highlighted in red here, are very similar to the way verbs function in the human language. With the human language the verb is very central to a sentence, and all the other parts of the sentence fit around the verb. So either they are the subject that doing the verb, or the object that the verb is done to. Very similarly in Perl. The print function here is acting like the verb, and whatever comes after the print function is what the function is doing. I won't to use the word object, that has a very special sense in the context of programming, and we are going to come to that towards the end of the course. Suffice it to say however that even though a lot of Perl may seem quite alien to begin with, as you get used to it you will start to realize that it's just the way of expressing commands very simply to the Perl executable, which has a very literal way of interpreting our commands. We can't expect it to use any common sense in interpreting exactly what we said. It will treat what we said very literally and do exactly what we asked it to do. Unless we make something explicit, the Perl executable is not going to be able to work out what we mean. But, other than that, there are a lot of similarities between programming language and human language. One final thing I should note in this movie is that, any comments we include within our script can be marked with a hash at the beginning of the line. And these are ignored. But we can use these to remind ourselves what we are trying to do when we wrote a certain part of a program. This is probably irrelevant for a simple program like we have here with only two lines of actual code that does anything. But as we write longer and longer Perl scripts, we will find comments become more and more valuable. Especially when we come back to our code two years down the line, and we have no idea what we are trying to do, without taking a look at some comments that we'd left for ourselves, as kind of hints as to what we meant to do, and what we expected to happen when that program is run.

Tutorial Information

Course: Perl Fundamentals
Author: Joshua Mostafa
SKU: 33403
ISBN: 1-9320-7215-2
Release Date: 2002-12-19
Duration: 7.5 hrs / 113 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available