Book Details
Perl 5 Interactive Course
- Jon Orwant
- Waite Group Press
- September 1996
Top
Chap 1 Meet Perl: The Basics
Lesson 1 The Look and Feel of Perl
Running a Perl Program
Let's run our first Perl program. Following that great tradition of computer language books, we'll begin with a program that prints Hello world!.
Create a file (call it hello) containing the line shown below. (Don't forget the semicolon at the end! Every (well, nearly every) Perl statement ends in a semicolon.)
| print "Hello world! \n"; # prints Hello world! followed by a newline (\n) | % perl hello Hello world! |
If you want hello to be executable directly from the UNIX command line, so that you can just say hello instead of perl hello,
you need to add one line to the beginning of your program. That line is #!location_of_your_Perl_binary.
(This book will use #!/usr/bin/perl as the first line of all its programs because Perl installs itself as /usr/bin/perl on most UNIX systems.
If Perl is somewhere else on your computer, you'll have to replace /usr/bin/perl with the appropriate pathname.)
On all lines but the first, # is used to begin a comment.
On UNIX systems, you'll need to make the program executable with the chmod command.
| #!/usr/bin/perl print "Hello world! \n"; # prints Hello world! followed by a newline (\n) | % chmod +x hello % ./hello Hello world! |
Command-Line Flags (switches)
| -v | displays version number | % perl -v This is perl, v5.8.8 built for i386-linux-thread-multiCopyright 1987-2006, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/ , the Perl Home Page. |
| -e | execute statements from command line (Just about anything you can do in a Perl program, you can do from the command line as well.) | % perl -e 'print 4;' 4 % perl -e 'print "Hello world! \n";' Hello world! |
Command-line flags can also be placed inside programs, directly after #!/usr/bin/perl.
Is Perl Compiled or Interpreted?
The short answer is interpreted, which means that your code can be run as is, without a compilation stage that creates a nonportable executable program.
When you run a Perl program, it's first compiled into bytecode, which is then converted (as the program runs) into machine instructions.
So it's not quite the same as shells, or Tcl, which are "strictly" interpreted without an intermediate representation.
Nor is it like most versions of C or C++, which are compiled directly into an architecture-dependent format.
Furthermore, there is a separate utility called the Perl Compiler, which you can use to make your own architecture-dependent binary, should you choose.
Top
Lesson 2 Scalars
| begin with | variable |
| $ | scalars |
| @ | arrays |
| % | hashes |
variable name should begin with $, @ or %, followed by a letter or an underscore, followed by more letters, digits, or underscores and is case-sensitive.
| #!/usr/bin/perl -w $greeting = 'Hello;'; # Set the value of $greeting to 'Hello' print "$greeting world!\n"; # and print it, followed by "world!n" | % hello2 Hello world! |
Double Quotes and Single Quotes
| $greeting = 'Hello'; print '$greeting'; | $greeting |
| $greeting = 'Hello'; print "$greeting"; | Hello |
Perl performs variable interpolation within double quotes, which means that it replaces a variable's name with its value.
Single quotes don't perform variable interpolation.
| print "Hello, world!\n\n\n"; | Hello world! |
| print 'Hello, world!\n\n\n'; | Hello world!\n\n\n |
Double quotes do something else that single quotes don't do: They interpret backslashed characters.
"Plain" strings can be either double-quoted or single-quoted.
Comments (0)
You don't have permission to comment on this page.