Introduction
Open Source projects work in a collaborative manner. Many people share their knowledge and resources for the common good. There is no owner. However, there is usually some form of governing committee.
Users can submit bug reports and suggest enhancements. Bugs and enhancements are reviewed by a committee. Changes are made by a group of volunteers.
There are actually two types of software that use the collaborative manner of creation. They are free software and open source software.
Free software is free in every sense of the word. You have the freedom to modify the source code, give copies to your friends, add or remove functionality, and so on. The software costs nothing. You share your changes and modifications with others.
However there is one restriction. You can't sell it. The concept is simple. Free software promotes the idea of the free exchange and use of ideas and knowledge. This system developed from the practice of free exchange of programs at universities and colleges.
Open Source software is similar. You have the freedom to modify it and distribute it as you see fit. You can even sell it.
However there is one restriction. Whatever changes you make must be made available to others. You share the modified source code with others. They have the option to sell it, distribute the software for free, make changes and resell it.
The difference between these two types of software is that free software always remains free.
There are many open source programming languages. Some of these are: PHP Python, and Ruby.
These languages serve to solve specific programming issues. The following list provides a brief synopsis.
PHP is a language for writing server-side programs. It provides the same type of functionality as Microsoft's ASP.NET, VB.NET and C#.
Python is a multi-purpose language.
Ruby is an object-oriented language. Its syntax and structure is similar to the languages Ada, Perl and Smalltalk.
Open source programming languages are platform-independent. They work on Windows, Macintosh and Linux systems.
About This Article
This article discusses Perl. I chose Perl because it is a common language that is used for Web programming. This article provides an introduction to the different variables and data types available.
This is an important part of Perl and any programming language. The types of data that a language can handle help define the problems a language can solve.
After reading this article you will have an introductory understanding of:
Creating variables
Printing the values of variables
Manipulating variables
Using special or system variables, and
Array (List) processing
About Perl
Introduction
Perl is a versatile programming language created by Larry Wall in 1987. He designed this language to extract and manipulate text files. It has since grown into a rich programming language.
Perl is often used to accept and manipulate information from the Web. For example, Perl is used to extract information from online forms. However it is powerful enough to create and maintain online bulletin boards.
You can also use Perl to create dynamic web pages. It allows you to write HTML from within a Perl program.
There are a variety of websites that provide a wealth of information about Perl. Some of these are:
www.perl.org
www.cpan.org
Syntax
Perl, like all languages has a grammar and a syntax. A Perl script consists of statements or sentences. A semicolon appears at the end of each statement. Here is an example:
print "Welcome to the world of Perl programming!";
The print command prints the string (the text that appears within the quotes) as follows:
Welcome to the world of Perl programming!
Commands are like verbs. They perform some type of action. Most statements begin with a command.
The print command can print multiple sentences within a string.
print "Welcome to the world of Perl programming! It is an exciting world where a creative person can have fun.";
Here is the output.
Welcome to the world of Perl programming! It is an exciting world where a creative person can have fun.
This is nice but the print command offers a lot more versatility. Here is the same example with one small change.
print "Welcome to the world of Perl programming!\nIt is an exciting world where a creative person can have fun.";
Notice the difference? There is a \n between the two sentences. This sequence of characters is called a backslash escape sequence. It consists of a backslash and one or more characters.
Here is the output from this example.
Welcome to the world of Perl programming!
It is an exciting world where a creative person can have fun.
Notice the difference? Each sentence begins on a new line. The new line backslash escape sequence (\n) prints the following sentence on a new line. Backslash escape sequences are like adverbs since they modify commands (verbs).
A string can contain more than one type of backslash escape sequence.
print "Welcome to the world of Perl programming!\nIt is an exciting world where a creative person can have fun.\n\nYou can do a variety of activities in this world. They are:\ncreate lists\tperform math functions\textract data and many other tasks.";
This example creates the following output.
Welcome to the world of Perl programming!
It is an exciting world where a creative person can have fun.
You can do a variety of activities in this world. They are:
create lists perform math functions extract data and many other tasks.
There are two newline backslash escape sequences to create double spacing between the second and third sentences. This example also uses the tab backslash escape sequence (\t). Perl provides a variety of backslash escape sequences.
Variables
Variables are nouns. They represent prices, addresses, names and so on. This is a good way to think about variables and programming in general. Programming is a form of communication. You use the language's constructs to represent real world tasks.
Perl offers five variable types. They are: scalar, string, numeric, arrays, and special variables.
Each type has a specific use.
Scalar
A scalar variable represents an alphanumeric quantity. It can be an integer (5, 25, 147), a real number (25.3, 878.3425) or one or more characters (a, b, carrots, 84 Chester Ave.).
Scalar variables can change. The values they represent can be part of an arithmetic operation or a string function.
A scalar variable is identified by its initial character, the dollar sign ($). The remainder of the name consists of alphanumeric characters. Here are some examples.
$Book_Title
$book_Price
$_Book_Description
$comment1
Variable names are case sensitive. For example, $book_title and $Book_Title are two different variables.
String Data
String data consists of alphanumeric characters. These variables represent names, addresses, descriptions, etc. This example creates some scalar variables and prints their values.
$Book_Name = "Unix For Mac";
$Book_Author1 = "Sandra Henry-Stocker";
$Book_Author2 = "Kinn Bartlett";
print $Book_Name;
print $Book_Author1;
print $Book_Author2;
Here is the output.
Unix For MacSandra Henry-StockerKinn Bartlett
The output is not exactly what you want. The text is more readable if each value is on a new line.
$Book_Name = "Unix For Mac";
$Book_Author1 = "Sandra Henry-Stocker";
$Book_Author2 = "Kinn Bartlett";
print $Book_Name . "\n";
print $Book_Author1 . "\n";
print $Book_Author2
This script includes an interesting set of characters - . "\n". The . is the concatenation operator. Concatenation is a fancy word that means join two or more strings together. The newline backslash escape sequence is enclosed within quotes to distinguish it from an arithmetic operation.
You can also include the newline backslash escape sequence in a variable.
$Book_Name = "Unix For Mac";
$Book_Author1 = "Sandra Henry-Stocker";
$Book_Author2 = "Kinn Bartlett";
$newline = "\n";
print $Book_Name . $newline;
print $Book_Author1 . $newline;
print $Book_Author2
The output is nicely formatted on new lines. However who is Kinn Bartlett? What is 26.99? This data is more useful with identifying labels.
$Book_Name = "Unix For Mac";
$Book_Author1 = "Sandra Henry-Stocker";
$Book_Author2 = "Kinn Bartlett";
$newline = "\n";
print "Title: " . $Book_Name . $newline;
print "Authors: " . $Book_Author1 . " and " . $Book_Author2
Notice the space after the colon in the labels? Notice the space before and after the word and? Concatenation does not add a space. You need to add the space or spaces manually. Here is the output for this example.
Title: Unix For Mac
Authors: Sandra Henry-Stocker and Kinn Bartlett
Numeric Data
Numeric data represents quantities, ages, prices, etc.
$Book_Name = "Unix For Mac";
$Book_Author1 = "Sandra Henry-Stocker";
$Book_Author2 = "Kinn Bartlett";
$Book_Price = 26.99;
$Book_Quantity = 2;
$newline = "\n";
print "Title: " . $Book_Name . $newline;
print "Authors: " . $Book_Author1 . " and " . $Book_Author2 . $newline;
print "Price: " . $Book_Price . $newline;
print "Quantity: " . $Book_Quantity;
This example displays the book's price and quantity.
Title: Unix For Mac
Authors: Sandra Henry-Stocker and Kinn Bartlett
Price: 26.99
Quantity: 2
You can easily perform mathematical functions. This example displays the total price for the two books.
$Book_Name = "Unix For Mac";
$Book_Author1 = "Sandra Henry-Stocker";
$Book_Author2 = "Kinn Bartlett";
$Book_Price = 26.99;
$Book_Quantity = 2;
$newline = "\n";
print "Title: " . $Book_Name . $newline;
print "Authors: " . $Book_Author1 . " and " . $Book_Author2 . $newline;
print "Price: " . $Book_Price . $newline;
print "Quantity: " . $Book_Quantity . $newline;
$Total_Price = $Book_Price * $Book_Quantity;
print "Total Price: \$" . $Total_Price;
Notice the backslash before the $? You use a backslash escape sequence to display characters that have special meaning in Perl. Since this character designates a scalar variable, the backslash tells the Perl interpreter to treat the $ as a regular character.
Here is the output.
Title: Unix For Mac
Authors: Sandra Henry-Stocker and Kinn Bartlett
Price: 26.99
Quantity: 2
Total Price: $53.98
Arrays
Arrays hold multiple values. An array can hold the month names for a year, quarterly sales figures, addresses, different book genres and so on. Visualize an array as either a row or a column in a spreadsheet or table.
Array names begin with an at sign (@). The remaining characters are alphanumeric. Here are some examples.
@book_types
@monthNames
@SalesQtr3
@YearEndSales_2005
The values of an array are specified within a set of parentheses. Each value is enclosed in quotes. Values are separated by commas.
@book_types = ("Mysteries", "Computer-Related", "Fiction", "Gardening");
Table 13 - Creating an Array
Array values are referenced by their position in the list. An index number references each value. The first index number is 0, the second index number is 1 and so on. The following example prints the first (Mysteries) and third items (Fiction) in this array.
@book_types = ("Mysteries", "Computer-Related", "Fiction", "Gardening");
$newLine = "\n";
print @book_types[0] . $newLine;
print @book_types[2] . $newLine;
Each value is referenced by an index number. This number is enclosed within square brackets.
Here is the output.
Mysteries
Fiction
This works well for short arrays. However, what if the array contains many values? Perl makes it easier to create long arrays and very easy to print their values.
Here is an example. Our array contains these values: Mysteries, Computer-Related, Fiction, Gardening, History, HistoricalFiction, Religion, Sports, Philosophy, Medicine and Mythology.
The first nuisance is to remember the beginning and ending quotes for each value. You also need to remember to place a comma after each value.
Perl alleviates this nuisance with the qw operator. This operator allows you to list array values without the quotes and the comma. Separate each value with a space.
@book_types = qw(Mysteries Computer-related Fiction Gardening History HistoricalFiction Religion Sports Philosophy Medicine Mythology);
Perl provides an easy way to print the values of an array. It is ridiculous to count the positions of the values you want to print. The foreach statement eliminates this hassle. Here is the syntax for this statement.
foreach $scalarVariable (@arrayName)
The foreach statement loops or repeats a series of statements until all the values in a variable or data structure are processed. In this case it will execute a series of statements until all the values of the array are processed.
The scalar variable holds each value of the array. The array is placed within the parentheses. During each repetition the array's subscript is incremented by one placing the new value into the scalar variable.
@book_types = qw(Mysteries Computer-related Fiction Gardening History HistoricalFiction Religion Sports Philosophy Medicine Mythology);
$newLine = "\n";
foreach $Type_of_Book (@book_types)
{
print $Type_of_Book . $newLine;
}
A set of curly braces ({}) follows the foreach statement. This set of braces contains the statements that the foreach statement processes. In our example, it prints the values of the array that the $Type_of_Book variable contains. Here is the output.
Mysteries
Computer-related
Fiction
Gardening
History
HistoricalFiction
Religion
Sports
Philosophy
Medicine
Mythology
Special Variables
Perl contains some special or system variables. They have predefined meanings that help Perl accomplish specific tasks. The following list defines some of these variables and what they do.
$[This variable sets the value of the index number for an array. In a previous example, I stated that the first value in an array is position 0. This variable allows you reference the first value as position 1. Here is an example.
$[ = 1;
$0This variable contains the name of the current script. The following example prints the name of the current script. Here is an example.
print $0;
$,This variable allows you to define the character that separates output displayed by the print statement. Here is an example that defines the output delimiter as a space.
$, = " ";
List Processing - Manipulating Arrays
Arrays or lists are useful data structures. We use them all the time in the non-programming world. To Do lists and shopping lists are common examples.
Sometimes it is useful to split a list into multiple lists. For example, I split my to do list into two lists: tasks that I need to do and tasks that I want to do.
It is easy to split arrays using Perl. Our list of book types may be more useful if they are split into a fiction list and a non fiction list. The fiction list contains these book types: Mysteries, Fiction, HistoricalFiction and Mythology. The non fiction list contains: Computer-Related, Gardening, History, Religion, Sports, Philosophy and Medicine.
The first (0), third (2), sixth (5) and last (10) items are fiction. The second (1), fourth (3), fifth (4), seventh (6), eighth (7), ninth (8) and tenth (9) are non fiction.
Here is the code:
$newLine="\n";
$, = " ";
@book_types = qw(Mysteries Computer-related Fiction Gardening History HistoricalFiction Religion Sports Philosophy Medicine Mythology);
@fiction = @book_types[0,2,5,10];
@nonFiction = @book_types[1,3,4,6,7,8,9];
print "The fiction books are: ";
print @fiction;
print $newLine;
print $newLine;
print "The non fiction books are: ";
print @nonFiction;
These lines create the fiction and non fiction lists.
@fiction = @book_types[0,2,5,10];
@nonFiction = @book_types[1,3,4,6,7,8,9];
A quick way to print the contents of an array is to use the print statement with the array name. However, there is a problem. The list or array values print without any delimiter. Each value prints in sequence without any spaces or commas to distinguish one from the other.
One of the special variables solves this annoyance. The $, variable defines the character that prints between items. The following line defines a space as the output character.
$, = " ";
Sorting Lists
Lists are more useful when sorted. Perl accomplishes this using the sort function and the cmp (compare) operator.
$newLine="\n";
$, = " ";
@book_types = qw(Mysteries Computer-related Fiction Gardening History HistoricalFiction Religion Sports Philosophy Medicine Mythology);
@fiction = @book_types[0,2,5,10];
@nonFiction = @book_types[1,3,4,6,7,8,9];
@fictionAZ = sort{$a cmp $b}@fiction;
@nonFictionZA = sort{$b cmp $a}@nonFiction;
print "The fiction books are: ";
print @fictionAZ;
print $newLine;
print $newLine;
print "The non fiction books are: ";
print @nonFictionZA;
Our example is similar to the previous one except for two lines.
@fictionAZ = sort{$a cmp $b}@fiction;
@nonFictionZA = sort{$b cmp $a}@nonFiction;
The sort function sorts the list. However, do we want to sort the list in ascending or descending order? The cmp (compare) operator designates the sort order.
The compare operator uses two special scalar variables, $a and $b. The sequence $a cmp $b sorts in ascending order and $b cmp $a sorts in descending order.
The original array follows the comparison sequence. The sort order is stored in a new array which, in our example, is the array we use in the print statement.
In Closing
Perl is a versatile language. Like any language, Perl has a grammar.
Perl commands are verbs. They perform an action. For example, the Perl command "print" displays or prints information.
Adverbs modify actions. Perl has adverbs that are called backslash escape sequences. The adverb "\n" prints or displays information on anew line.
Perl nouns are either variables or data types. Scalars represent numeric or alphanumeric entities. Arrays represent lists.
Perl, like any other language exists as a form of communication. Once the basics are understood, it is easier to express your idea or program. Future articles will explain other aspects of this language.
Published by Alan Cohen
I am a writer who enjoys writing about a variety of issues and topics. View profile
Free Computer Software that Delivers, Courtesy of Open SourceIf you need expensive software but your wallet won't support it, the answer is a lot easier (and a lot more legal!) than cracked programs. Think Open Source! There's an Open S...
God and the Creative Commons: Open Source ReligionThere are a number of websites which are creating open source religious instruction. Will we soon see a "Wikipedia for Christians"?- Open Source SoftwareEnter Open Source, Freeware and Shareware. Open Source products are not necessarily free from cost, the free refers to the source code being open for others to use and incorporate.
- Are You Open-minded About Open Source?Why spend tons of money on software? Cost saving tips to help you get more valuable software for free.
- A Review of Pidgin - An Open Source Instant Messenger ProgramPidgin, an open source instant messenger program, is unlike most programs out there. What are some advantages? It's free, it's memory-friendly and it's simply awesome.
- How to Create Your Own Computer Programs the Easy Way
- E-commerce Application Solutions Utilizing Microsoft's .NET
- Open Source (R)Evolution
- Open Source Power Tools for Microsoft Windows Users
- Computer Programming - Then and Now
- Why I Love Open Source Software
- Ruby: An Open Source Programming Language





1 Comments
Post a CommentThere are many PERL books available. I am a big fan of the "For Dummies" series.
Give me some ideas about the type of documentation you like. Maybe I can write some articles that can help you.