O'Reilly logo

Learning Perl, Fourth Edition by brian d foy, Tom Phoenix, Randal L. Schwartz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Input from the Diamond Operator

Another way to read input is with the diamond[116] operator: <>. This is useful for making programs that work like standard Unix[117] utilities, with respect to the invocation arguments (which we’ll see in a moment). If you want to make a Perl program that can be used like the utilities cat, sed, awk, sort, grep, lpr, and many others, the diamond operator will be your friend. If you want to make anything else, the diamond operator probably won’t help.

The invocation arguments to a program are normally a number of “words” on the command line after the name of the program.[118] In this case, they give the names of the files your program will process in sequence:

    $ ./my_program fred barney betty

That command means to run the command my_program (which will be found in the current directory), and that it should process file fred, followed by file barney, followed by file betty.

If you give no invocation arguments, the program should process the standard input stream. As a special case, if you give a hyphen as one of the arguments, that means standard input as well.[119] If the invocation arguments had been fred- betty, that would have meant that the program should process file fred, followed by the standard input stream, followed by file betty.

The benefit of making your programs work like this is that you may choose where the program gets its input at runtime; for example, you won’t have to rewrite the program to use it in a pipeline (which we’ll discuss ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required