
| Contents |
|||||||||||||||
| [an error occurred while processing this directive] |
8.3 Input Control using the Diamond OperatorThe diamond operator (so called because it resembles a diamond and allegedly named this by the daughter of Perl creator, Larry Wall) is represented by the <> characters and allows a Perl script to support input from a number of different sources. The key benefit of this is that it allows the choice of input to be specified at runtime rather than hard coded at the script development stage. Lets take our previous example and adapt it slightly to make use of the diamond operator:
What this essentially means is that we can execute our script with a variety of invocation options on the command line to designate the source of the input.#!/usr/bin/perl @userinput = <>; foreach (@userinput) { print; } For example we can include the file name that we want to display as an invocation argument on the command line that we want to display. For example:
This will output the contents of the /etc/passwd file. We can also use multiple command line arguments to select more than one file:./showtext /etc/passwd
This will display both the hosts and passwd files located in the /etc directory of our system../showtext /etc/passwd /etc/hosts
When using the diamond operator
You can, of course, mix and match invocation arguments. The "-" invocation line argument instructs the script to read./showtext
The above command line will cause the file "myfile1" to be processed, followed by the STDIN input stream (until EOF is received in the input stream) and finally the file named "myfile2"../showtext myfile1 - myfile2 If an invalid filename is specified as an invocation operator when using the diamond operator an error message will be displayed that will look something like:
Can't open myfile1: No such file or directory at ./showtext line 2. |