Learning Perl The Hard Way

Categories:

Recommended

This chapter presents two of the built-in types, arrays and scalars. A scalar is a value that Perl treats as a single unit, like a number or a word. An array is an ordered collection of elements, where the elements are scalars.

This chapter describes the statements and operators you need to read commandline arguments, define and invoke subroutines, parse parameters, and read the contents of files. The chapter ends with a short program that demonstrates these features.

In addition, the chapter introduces an important concept in Perl: context.

1.1 Echo

The UNIX utility called echo takes any number of command-line arguments and prints them. Here is a perl program that does almost the same thing: print @ARGV;

The program contains one print statement. Like all statements, it ends with a semi-colon. Like all generalizations, the previous sentence is false. This is the first of many times in this book when I will skip over something complicated and try to give you a simple version to get you started. If the details are important later, we’ll get back to them.

The operand of the print operator is @ARGV. The “at” symbol indicates that @ARGV is an array variable; in fact, it is a built-in variable that refers to an array of strings that contains whatever command-line arguments are provided when the program executes.

There are several ways to execute a Perl program, but the most common is to put a “shebang” line at the beginning that tells the shell where to find the program called perl that compiles and executes Perl programs. On my system, I typed whereis perl and found it in /usr/bin, hence

Category:

Attribution

Allen B. Downey. Learning Perl The Hard Way. http://www.greenteapress.com/perl/

VP Flipbook Maker

Convert your work to digital flipbook with VP Online Flipbook Maker! You can also create a new one with the tool. Try it now!