Broad Network


Getting Started with Perl Optimized

Perl Basics – Part 1

Perl Course

Foreword: In this part of the series, you will install the Perl computer language and write your first simple program.

By: Chrysanthus Date Published: 29 Mar 2015

Introduction

Perl stands for Practical Extraction and Report Language. It is a scripting language, which is very good in manipulating text. Today you can use it to do other things that powerful languages like C++ achieve. When Perl was invented it was not made to run on Windows and some other operating systems. ActiveState is a company that came up with a Perl that could run on windows. ActiveState’s Perl is called ActivePerl. This is a series of tutorials using ActivePerl. This does not stop you from using Perl for some other operating system. If you are not using ActivePerl, then begin every code sample (program) with something like, #!/usr/bin/perl for the first typed line in the code; and all the code samples in this volume (series) will work. I say more about “#!/usr/bin/perl” below.

The latest version of ActivePerl is version 5.18.2 Build 1802. It works with Windows XP and the higher versions of Windows.

The principles outlined in this series will work with this version of Perl and the future versions of Perl.

You can learn Perl from this series (and volume) as your first computer language. In this volume, you will be practicing the code samples as you go along.

Pre-Knowledge
This is the first part of the series titled, Perl Basics. This series is the first series in my volume, Optimum Perl Coding, which is a Perl course.

The main pre-knowledge you should have, before studying this course is that of computer literacy. You should be computer literate. You should also know the basics of Internet usage. Having passed in Middle School (British O’Level) mathematics, is a plus (added advantage) to enable you understand the course. However, the little mathematics needed for the course is taught to you along with the course. You can study Middle School mathematics at the link given below.

Note: a program is software code that carries out one or more tasks.

Requirements
If you are using ActivePerl, you need the following requirements:

- ActivePerl Interpreter
- Text Editor
- Your Personal Computer having the Windows Operating System.

If you are using a different Perl, you need that Perl’s interpreter and the operating system that goes with the interpreter.  You still need the operating system’s text editor, as all code is typed in the text editor.

The text editor for Windows is called, Notepad.

An interpreter (a Perl interpreter for example) is the software engine that actually executes (carries out) what the program wants.

Download
You can download the ActivePerl interpreter from http://www.ActiveState.com/. It is free. You can also search the web for ActivePerl and download.

Installation
ActivePerl is easy to install. Just follow the instructions and choose the default settings.

If you are using the Windows Operating System, make sure the path “C:\Perl\bin;” without the quotes, is in what is known as the environment variables. To reach that in my computer (with Windows OS), I had to do the following clicks:

    Start Button|Control Panel|System|Advanced|Environment Variables|Path|Edit

Directory to Use
You will use the root directory, C:\ for your code samples. If you are really computer literate, then you can create sub directories in the root directory and use them; however, I will not go into that.

Your First Perl Script
The console is a monochrome (black and white) screen or window. In my computer the console is called, Command Prompt. You will use the console for calling programs and displaying results. In my computer, to access the console, you have to do the following clicks:

    Start Button|All Programs|Accessories|Command Prompt

So, now, bring out the console by using the above click sequence. In the console, change the prompt directory to the root directory, by typing the following and pressing the Enter Key:

    cd c:\

Now, type the following two lines in your text editor:

use strict;

print "Hello World! \n";

Save the file with the name, hello.pl in the root directory. The filename has the extension, pl (after the dot). All Perl program files have the extension, pl.

Now, go to your console and type the following and press the Enter Key:

    hello.pl

You should see the following printed (displayed) at the DOS prompt:

    Hello World!

The \n you typed, is not printed. \n is a special character called an escaped sequence. It is made up of the backslash followed by n in lower case. It acts as one character and it forces the flashing underscore (bar) to the next line in the console.

Explanation of the Script
The first line of the script is “use strict;” . With this statement, any error you make in your script (program) will not be tolerated by the Perl interpreter; this is actually a good thing. The next line is: print "Hello World! \n";

The word, print is an example of what is called, a Perl function. It is a kind of command. It tells the Perl interpreter, to print (display) what is on its right. What is on its right is "Hello World! \n" in double quotes. Note the difference between these quotes and the quotes obtained from the word processor. The double quote obtained from the word processor is curved, while the double quote obtained from the text editor is not curved. If you use the quotes (single or double) obtained from your word processor in your program (script), you will not have correct results.

The \n is an example of what is called escaped sequences. \n is one escape sequence. It is actually called the newline character (for New Line). The text inside the double quotes above, begins with “Hello World!” followed by “\n”. “Hello World!” is our text for output. It is printed by the print function. The newline character, “\n” is not printed. Normal characters are single characters. This special character is made up of two characters. Its purpose is to send the cursor (flashing underscore) to the next line in the console. The special character has to be typed between double quotes of the text editor, not double quotes of the word processor.

In this part of the series, you have learned about the “use strict” instruction and the print function. You have also learned the basics of producing a Perl program. A Perl program is often called, a Perl script.

That is it for this part of the series. As I said above, all the code samples for this series (and overall volume) will work in the tradition Perl Interpreter with its own operating system (Unix or Linux, etc.). For the traditional Perl and its operating systems, you will have to precede each of the code (samples) with something like, #!/usr/bin/perl . This is the path and directory to the effective Perl Interpreter called the Perl Executable. This line can be omitted for Windows’ Perl, which is ActivePerl.

Well, let us stop here. We continue in the next part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message