Skip to content Skip to sidebar Skip to footer

Norm and Reading From Stdin if the Filename Is -

next up previous contents
Adjacent: Using the Diamond Operator Up: Files Input Previous: Files Input

Some Files Are Standard

In an effort to make programs more compatible, there are three connections that always be when your programme starts. These are STDIN, STDOUT, and STDERR. Really, these names are file handles. File handles are variables used to manipulate files. Just like you demand to grab the handle of a hot pot earlier you can pick it up, you need a file handle before you can use a file.

The three special file handles are always open STDIN, STDOUT and STDERR.

STDIN reads from standard input which is commonly the keyboard in normal Perl script or input from a Browser in a CGI script. Cgi-lib.pl reads from this automatically.

STDOUT (Standard Output) and STDERR (Standard Error)past default write to a console or a browser in CGI.

You've been using the STDOUT file handle without knowing information technology for every print() statement in this book. The print() function uses STDOUT as the default if no other file handle is specified. Later in this chapter, in the "Examples: Printing Revisited" department, y'all will see how to send output to a file instead of to the monitor.

Example: Using STDIN

Reading a line of input from the standard input, STDIN, is ane of the easiest things that you tin practise in Perl. This following three-line program will read a line from the keyboard and so brandish it. This will proceed until you press Ctrl+Z on DOS systems or Ctrl-D on UNIX systems stdin.pl.

while (<STDIN>) {      impress();  }      

The <> characters, when used together, are called the diamond operator. It tells Perl to read a line of input from the file handle inside the operator. In this case, STDIN. Later, you'll use the diamond operator to read from other file handles.

In this example, the diamond operator assigned the value of the input cord to $_ . Then, the print() function was chosen with no parameters, which tells print() to use $_ as the default parameter. Using the $_ variable tin save a lot of typing, but I'll let you decide which is more readable. Here is the aforementioned plan (stdin2.pl) without using $_.

while ($inputLine = <STDIN>) {      impress($inputLine);  }      

When you pressed Ctrl+Z or Ctrl+D, you told Perl that the input file was finished. This caused the diamond operator to return the undefined value which Perl equates to false and caused the while loop to end. In DOS (and therefore in all of the flavors of Windows), 26-the value of Ctrl+Z-is considered to be the end-of-file indicator. As DOS reads or writes a file, it monitors the data stream and when a value of 26 is encountered the file is closed. UNIX does the same thing when a value of 4-the value of Ctrl+D-is read.

Tip When a file is read using the diamond operator, the newline graphic symbol that ends the line is kept as part of the input cord. Ofttimes, yous'll meet the chop() function used to remove the newline. For instance, chop($inputLine = <INPUT_FILE>);. This statement reads a line from the input file, assigns its value to $inputLine then removes that last grapheme from $inputLine-which is almost guaranteed to be a newline graphic symbol. If you fear that the last character is not a newline, employ the chomp() function instead.

Example: Using Redirection to Change STDIN and STDOUT

DOS and UNIX permit you lot modify the standard input from being the keyboard to being a file by changing the control line that yous apply to execute Perl programs. Until now, you probably used a command line similar to:

perl -w 09lst01.pl

In the previous example, Perl read the keyboard to get the standard input. But, if there was a way to tell Perl to employ the file 09LST01.PL as the standard input, you could have the program print itself. Pretty neat, huh? Well, it turns out that yous can change the standard input. It's done this way:

perl -west 09lst01.pl < 09lst01.pl

The < graphic symbol is used to redirect the standard input to the 09LST01.PL file. You at present have a plan that duplicates the functionality of the DOS type command. And it merely took three lines of Perl lawmaking!

Yous can redirect standard output to a file using the > character. And so, if you wanted a copy of 09LST01.PL to be sent to OUTPUT.LOG, yous could employ this command line: perl -w 09lst01.pl <09lst01.pl >output.log

Keep this use of the < and > characters in listen. You'll be using them again shortly when we talk most the open() function. The < character will signify that files should be opened for input and the > will exist used to signify an output file. Just first, let's continue talking near accessing files listed on the command line.


 
  • Using the Diamond Operator (<>)

next up previous contents
Next: Using the Diamond Operator Upwardly: Files Input Previous: Files Input
dave@cs.cf.ac.uk

riveradregat1991.blogspot.com

Source: https://users.cs.cf.ac.uk/dave/PERL/node67.html

Post a Comment for "Norm and Reading From Stdin if the Filename Is -"