Home arrow Perl Programming arrow Page 3 - Perl 101 (Part 1) - The Basics

Your First Perl Program - Perl

Known as much for its confusing, often oddball, syntax as for itsefficiency and speed, Perl has a mystique that very few languages canmatch. This first article in a new series gives novice and intermediateprogrammers an introduction to the basics of the language.

  1. Perl 101 (Part 1) - The Basics
  2. ...And The Little Language That Could!
  3. Your First Perl Program
  4. To Err Is Human...To Debug, Divine!
  5. What's Next?
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 16
May 23, 2000

print this article


And now that you've got Perl installed and configured, how about actually doing something with it? Use your favourite text editor to type the following lines of code:
# Perl 101
print ("Groovy, baby!\n");

Save this file as "groovy.pl".

Next, you need to tell the system that the file is executable. On a UNIX system, this is accomplished by setting the "executable bit" with the "chmod" command:
$ chmod +x groovy.pl

And now run the script - in UNIX, try
$ ./groovy.pl

On a Windows system, you need to pass the name of the script to the Perl executable as a parameter, like this:
> perl groovy.pl

>  groovy.pl

In both cases, the script should return Austin Powers' trademark line. Ain't Perl groovy, baby!

In case things don't work as they should, it usually means that the system was unable to locate the Perl binary. This is a good time to holler for the system administrator.{mospagebreak title=Your First Perl Program Dissected} Let's take a closer look at the script. The first line

is used to indicate the location of the Perl binary. This line must be included in each and every Perl script, and its omission is a common cause of heartache for novice programmers. Make it a habit to include it, and you'll live a healthier, happier life.

Next up, we have a comment.
# Perl 101

Comments in Perl are preceded by a hash [#] mark. If you're planning to make your code publicly available on the Internet, a comment is a great way to tell members of the opposite sex all about yourself - try including your phone number for optimum results.

And finally, the meat of the script:
print ("Groovy, baby!\n");

In Perl, a line of code like the one above is called a "statement". Every Perl program is a collection of statements, and a statement usually contains instructions to be carried out by the Perl interpreter.

In this particular statement, the print() function has been used to send a line of text to the screen. Like all programming languages, Perl comes with a set of built-in functions - the print() function is one you'll be seeing a lot of in the future. The text to be printed is included within double quotes, and the entire thing is then surrounded by parentheses. Note the n character, used to indicate a new line.

C programmers will be familiar with the print() function call above, as also with the fact that in C, it is mandatory to place function arguments within parentheses. Perl is more flexible than C in this regard - in many cases, parentheses can be excluded in function calls, as in this example:
# Perl 101
print "Groovy, baby!\n";

Every Perl statement ends with a semi-colon. Don't ask why - just do it!

And here's an interesting bit of trivia - every Perl statement can be further sub-divided into smaller units called "tokens". If you take a look at the statement above, you'll see three distinct tokens - print, ("Groovy, baby!\n") and the semi-colon. The interesting thing about tokens is that they can be separated with white space and tabs - which, translated, means that you could also write the above line as
print  ("Groovy, baby!\n")       ;

print("Groovy, baby!\n") ;

and things would still work as advertised. Needless to say, this comes in very useful when dealing with long and complex scripts.{mospagebreak title=Two Plus Two} So now you know how to print text - how about a little math? Try this:

#!/usr/bin/perl # Some addition print (10+2);
And this should give you the answer of that particular mathematical operation.

You can even try subtraction, multiplication and division:

#!/usr/bin/perl # Some more math print (10-2); print (10 * 2); print (10/2);
Note how the various results are concatenated together in the absence of the newline character.

And you can even combine text and mathematical operations - this will be covered in greater detail over the next few weeks, but for the moment, you can play with this simple example:

#!/usr/bin/perl # Synthesis print ("Hello there! And what are you doing ", 1+1, "night, baby?\n");

>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: