Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.3 Input Control using the Diamond Operator

The 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:

#!/usr/bin/perl @userinput = <>; foreach (@userinput) { print; }
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.

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:

./showtext /etc/passwd
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 /etc/hosts
This will display both the hosts and passwd files located in the /etc directory of our system.

When using the diamond operator is read by default if nothing is specified on the command line. For example the following will read input from the keyboard:

./showtext
You can, of course, mix and match invocation arguments. The "-" invocation line argument instructs the script to read and as such can be included on the command line:

./showtext myfile1 - myfile2
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".

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.


 
 
  Published under the terms of the GNU General Public License Design by Interspire