Perl Echo Command

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:
2 Arrays and Scalars
#!/usr/bin/perl
print @ARGV;
I put those lines in a file named echo.pl, because files that contain Perl programs usually have the extension pl. I used the command
$ chmod +ox echo.pl
to tell my system that echo.pl is an executable file, so now I can execute the
program like this:
$ ./echo.pl

Now would be a good time to put down the book and figure out how to execute a Perl program on your system. When you get back, try something like this:
$ ./echo.pl command line arguments
commandlinearguments$

Sure enough, it prints the arguments you provide on the command line, although there are no spaces between words and no newline at the end of the line (which
is why the $ prompt appears on the same line).

We can solve these problems using the double-quote operator and the
n sequence.
print "@ARGV\n";

It might be tempting to think that the argument here is a string, but it is more accurate to say that it is an expression that, when evaluated, yields a
string. When Perl evaluates a double-quoted expression, it performs variable interpolation and backslash interpolation.

Variable interpolation: When the name of a variable appears in double quotes, it is replaced by the value of the variable.
Backslash interpolation: When a sequence beginning with a backslash ( ) appears in double quotes, it is replaced with the character specified by the sequence.

In this case, the n sequence is replaced with a single newline character.
Now when you run the program, it prints the arguments as they appear on the
command line.
$ ./echo.pl command line arguments
command line arguments
$
Since the output ends with a newline, the prompt appears at the beginning of the next line. But why is Perl putting spaces between the words now? The
reason is:
The way a variable is evaluated depends on context!

In this case, the variable appears in double quotes, so it is evaluated in inter-polative context. It is an array variable, and in interpolative context, the
elements of the array are joined using the separator specified by the built-in variable $". The default value is a space.

No comments:

Post a Comment