Transcription from the manpage documents is still in progress
perlintro -- a brief introduction and overview of Perl
This document is intended to give you a quick overview of the Perl programming language, along with pointers to further documentation. It is intended as a "bootstrap" guide for those who are new to the language, and provides just enough information for you to be able to read other people's Perl and understand roughly what it's doing, or write your own simple scripts.
This introductory document does not aim to be complete. It does not even aim to be entirely accurate. In some cases perfection has been sacrificed in the goal of getting the general idea across. You are strongly advised to follow this introduction with more information from the full Perl manual, the table of contents to which can be found in perltoc.
Throughout this document you'll see references to other parts of the Perl documentation. You can read that documentation using the "perldoc" command or whatever method you're using to read this document.
What is Perl?
Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more.
The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are that it's easy to use, supports both procedural and object-oriented (OO) programming, has powerful built-in support for text processing, and has one of the world's most impressive collections of third-party modules.
Different definitions of Perl are given in perl, perlfaq1 and no doubt other places. From this we can determine that Perl is different things to different people, but that lots of people think it's at least worth writing about.
Running Perl programs
To run a Perl program from the Unix command line:
Alternatively, put this as the first line of your script:
...and run the script as "/path/to/script.pl". Of course, it'll need to be executable first, so "chmod 755 script.pl" (under Unix).
Basic syntax overview
A Perl script or program consists of one or more statements. These statements are simply written in the script in a straightforward fashion. There is no need to have a "main()" function or anything of that kind.
Perl statements end in a semi-colon:
print "Hello, world";
Comments start with a hash symbol and run to the end of the line
# This is a comment
Whitespace is irrelevant:
print "Hello, world" ;
... except inside quoted strings:
# this would print with a linebreak in the middle print "Hello world";
Double quotes or single quotes may be used around literal strings:
print "Hello, world"; print 'Hello, world';
However, only double quotes "interpolate" variables and special characters such as newlines ("\n"):
print "Hello, $name\n"; # works fine print 'Hello, $name\n'; # prints $name\n literally
Numbers don't need quotes around them:
You can use parentheses for functions' arguments or omit them according to your personal taste. They are only required occasionally to clarify
issues of precedence.
print("Hello, world\n"); print "Hello, world\n";
More detailed information about Perl syntax can be found in perlsyn.
Perl variable types
Perl has three main variable types: scalars, arrays, and hashes.
Scalars A scalar represents a single value:
my $animal = "camel"; my $answer = 42;
Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. There is no need to pre-declare your variable types.
Scalar values can be used in various ways:
print $animal; print "The animal is $animal\n"; print "The square of $answer is ", $answer * $answer, "\n";
There are a number of "magic" scalars with names that look like punctuation or line noise. These special variables are used for all kinds of purposes, and are documented in perlvar. The only one you need to know about for now is $_ which is the "default vari- able". It's used as the default argument to a number of functions in Perl, and it's set implicitly by certain looping constructs.
print; # prints contents of $_ by default
Arrays An array represents a list of values:
my @animals = ("camel", "llama", "owl"); my @numbers = (23, 42, 69); my @mixed = ("camel", 42, 1.23);
Arrays are zero-indexed. Here's how you get at elements in an array:
print $animals; # prints "camel" print $animals; # prints "llama"
The special variable $#array tells you the index of the last ele-