Perl Notes (II)

Source: Internet
Author: User
Tags documentation numeric print print scalar stdin perl script


Part II network Programming with Perl


1 Input/output Basics


1.1 Filehandles



Filehandles are the foundation of networked applications. In this section we review the ins and outs of Filehandles. Even if you are a experienced Perl programmer, you are might want to scan this section to refresh your memory on some of the M Ore obscure aspects of Perl I/ o.


1.1.1 Standard filehandles






A filehandle connects a Perl script to the outside world. Reading from a filehandle brings in outside data, and writing to one exports data. Depending on how it is created, a filehandle may is connected to a disk file and to a hardware device as a such port, To a local process such as a command-line windows in a windowing system, or to a remote process such as a network server. It's also possible for a filehandle to is connected to a "bit bucket" device that just the up data and sucks it.












A FileHandle is no valid Perl identifier that consists of uppercase and lowercase letters, digits, and the underscore cha Racter. Unlike variables, a filehandle does not have a distinctive prefix (like "$"). So to do them distinct, Perl programmers often represent them in all capital letters, or caps.












When a Perl script starts, exactly three filehandles are open by Default: stdout, stdin, And stderr. The stdout filehandle, for "standard output," is the default filehandle for output. Data sent to this filehandle appears on the user's preferred output device, usually the command-line window from which the Script was Launched. stdin, for "standard input," is the default input filehandle. Data read from this filehandle be taken from the user's preferred input device, usually the keyboard. stderr  ("s Tandard error ") is used to error messages, diagnostics, debugging, and other such output. By default stderr uses the same output device as stdout, but this can is changed at the user ' s discretion. The reason that there are separate filehandles for normal and abnormal output are so this user can divert them independ ently; For example, to send normal output to a file and error output to the screen.












This code fragment'll read a line of input to STDIN, remove the terminating end-of-line character with the Chomp () fun Ction, and echo it to standard output:




$input = <STDIN>;

Chomp ($input);

Print STDOUT "If I heard you correctly, said: $input/n";




By taking advantage of the fact that STDIN and STDOUT are the defaults for many I/O operations, and by combining Chomp () W ith the input operation, the same code could is written more succinctly as this:




Chomp ($input = <>);

Print "If I heard you correctly, said: $input/n";




We Review the <> and print () functions in the next section. Similarly, STDERR is the default destination for the Warn () and die () functions.












The user can change the attachment of the three standard filehandles before the script. On UNIX and Windows systems, which is done using the redirect metacharacters "<" and ">". For example, given a script named muncher.pl this command'll change the script's standard input so it comes from th E file data.txt, and its standard output so that processed data ends up in Crunched.txt:




% muncher.pl <data.txt >crunched.txt





Standard error isn ' t changed, so diagnostic messages (e.g., from the built-in warn () and Die () functions) appear on the SC Reen.






on Macintosh systems, the users can change the source of the three standard filehandles by selecting filenames from a dial OG box within the Macperl development environment. 1.1.2 Input and Output Operations






Perl gives your option of reading from a filehandle one line at a time, suitable for text files, or reading from it in Chunks of arbitrary length, suitable for binary byte streams like image files.












For input, the <> operator are used to read from a filehandle in a line-oriented fashion, and read () or Sysread () to Read in a byte-stream fashion. For output, print () and syswrite () are used for both text and binary data (you decide whether to make the output Line-orie nted by printing newlines).

















$line = <FILEHANDLE>






@lines = <FILEHANDLE>






$line <>






@lines <>












The <> ("angle bracket") operator is sensitive to the context in which it is called. If It is used to assign to a scalar variable, a so-called scalar context, it reads a line of text from the indicated Fileh Andle, returning the data along with its terminating end-of-line character. After reading the last line of the FileHandle, <> 'll return undef signaling the End-of-file (EOF).






When <> are assigned to an array or used in another where Perl ordinarily expects a list, it reads all lines fr Om the filehandle through to EOF, returning them as one (potentially gigantic) list. This is called the A list context.






If called in a ' void context ' (i.e., without being assigned to a variable),<> copies a line into the $_ global Varia ble. This is commonly seen in while () loops, and often combined with pattern matches and other operations-use $_ Y:




while (<>) {

  print ' Found a gnu/n ' if/gnu/i

}




The <FILEHANDLE> form of this function explicitly gives the filehandle to read from. However, the <> form is "magical." If the script is called with a set of file names as command-line arguments, <> 'll attempt to open () each argument In turn and would then return lines to them as if they were concatenated into one large.






If no files are given on the command line, or if a single file named "-are given, then <> reads from standard input and is equivalent to <stdin>. The Perlfunc pod documentation for a explanation of how this works (pod Perlfunc, as explained in the preface).












$bytes = Read (FileHandle, $buffer, $length [, $offset])






$bytes = Sysread (FileHandle, $buffer, $length [, $offset])












The read () and sysread () functions read data of arbitrary length from the indicated filehandle. Up to $length bytes of data is read, and placed in the $buffer scalar variable. Both functions return the number of bytes actually read, numeric 0 on the "end of", or undef on a error.






This code fragment would attempt to read bytes of data from STDIN, placing the information in $buffer, and assigning the Number of bytes read to $bytes:




my $buffer;

$bytes = Read (STDIN, $buffer, 50);




By default, the read data is placed at the beginning of $buffer, overwriting whatever is already there. Can change this behavior by providing the optional numeric $offset argument, to specify that read data should is Writt En into the variable starting at the specified position.






The main difference between read () and Sysread () is this read () uses standard I/O buffering, and Sysread () does not. This means so read () won't return until either it can fetch the exact number of bytes requested or it hits the "End of" File. The Sysread () function, in contrast, can return partial reads. It is guaranteed to least 1 byte, but if it cannot immediately read the number of bytes requested the Fileh Andle, it'll return what it can. This behavior are discussed in the detail later in the Buffering and Blocking.












$result = Print FileHandle $data 1, $data 2, $data 3 ...






$result = Print $data 1, $data 2, $data 3 ...












The print () function prints a list of data items to a filehandle. In the ' the ', the FileHandle is given explicitly. Notice that there is no comma between the filehandle name and the "the". In the second form, print () uses the current default filehandle, usually STDOUT. The default filehandle can be changed using the One-argument form of select () (discussed below). If No data arguments are provided, then print () prints the contents of $_.






If output was successful, print () returns a true value. Otherwise it returns false and leaves the error message in the variable named $!.






Perl is a parentheses-optional language. Although I prefer using parentheses around function arguments, most Perl scripts drop them with print (), and this book fol Lows that convention as.












$result = printf $format, $data 1, $data 2, $data 3 ...












The printf () function is a formatted print. The indicated data items are formatted and printed according to the $format format string. The formatting language is quite rich, and are explained in detail in Perl's POD documentation for the related sprintf () (s Tring formatting) function.












$bytes = Syswrite (FileHandle, $data [, $length [, $offset]])












The Syswrite () function is ' an alternative way ' to ' write to a filehandle ' that gives for the process. Its arguments are a filehandle and a scalar value (avariable or string literal). It writes the data to the FileHandle, and returns the number of bytes successfully written.






By default, Syswrite () attempts to write the entire contents of $data, beginning at the start of the string. Can alter this behavior by providing a optional $length and $offset, in which case syswrite () would write $length byte s beginning at the position specified by $offset.






Aside from familiarity, the main difference between print () and syswrite () are that former uses standard I/O buffering, While the latter does is not. We discuss this later into the buffering and Blocking section.






Don ' t confuse Syswrite () with Perl ' s unfortunately named write () function. The latter is part of the Perl ' s formatting package, which we won ' t discuss further.












$previous = Select (FileHandle)












The Select () function changes the default output filehandle used by print print (). It takes the name of the FileHandle to set as the default, and returns the name of the previous default. There is also a version of the Select () that takes four arguments, which are used for I/O multiplexing. We introduce the four-argument version in Chapter 8.






When reading data as a byte stream with read () or Sysread (), a common idiom are to pass length ($buffer) as the offset into The buffer. This'll make read () append the new data to the "end of" of data that is already in the buffer. For example:


my $buffer;

while (1) {

 $bytes = read (STDIN, $buffer, 50,length ($buffer));

 Last unless $bytes > 0;

}

1.1.3 Detecting the end of File



The End-of-file condition occurs when there's no more data to is read from a file or device. When reading from files this happens to the literal end of the file, but the EOF condition applies as-when-reading FR Om Other devices. When reading to the Terminal (command-line window), for example, EOF occurs when the user presses a special Key:control -D on UNIX, Control-z on Windows/dos, and command-. On Macintosh. When reading from a network-attached socket, EOF occurs is the remote machine closes its end of the connection.






The EOF condition is signaled differently depending on whether your are reading from the FileHandle a s a byte stream. For Byte-stream operations with read () or Sysread (), and the EOF is indicated when the function returns numeric 0. Other I/O errors return undef and set $! To the appropriate error message. To distinguish between an error and a normal end of file, can be test the return value with defined ():




while (1) {my

 $bytes = Read (STDIN, $buffer,);

 Die "read error" unless defined ($bytes);

 Last unless $bytes > 0;

}




In contrast, the <> operator doesn ' t distinguish between EOF and abnormal conditions, and returns undef into either CA Se. To distinguish them, can set $! To Undef before performing a series of reads, with check whether it is defined afterward:




Undef $!;

while (defined (my $line = <STDIN>)) {

  $data. = $line;

}

Die "Abnormal read error: $!" if defined ($!);




When you are using <> inside the conditional by a while () loop, as shown in the most code recent, and you can D Ispense with the explicit defined () test. This is makes the loop easier on the eyes:




while (my $line = <STDIN>) {

  $data. = $line;

}




This would work even if the line consists to a single 0 or a empty string, which Perl would ordinarily as false. Outside while () loops, is careful to use defined () to test the returned value for EOF.






Finally, the There is the EOF () function, which explicitly tests a filehandle for the EOF condition:










$eof = EOF (FileHandle)




The EOF () function returns True if the next read on FileHandle'll return to an EOF. Called without arguments or parentheses, as in EOF, the function tests the last filehandle read from.


When using while (<>) to read from the command-line arguments as a single pseudofile, EOF () has "magical"-or at least Confusing-properties. Called with empty parentheses, as in EOF (), the function is returns true at the end of the very file. Called without parentheses or arguments, as in EOF, the function returns True at the end of each of the individual files O n the command line. The Perl POD documentation for examples to the circumstances in which this behavior is useful.










in practice, I/t have to use EOF () except in very special circumstances, and a reliance on it is often a signal That something are amiss in the structure of your. 1.1.4 Anarchy at the "End of"






When performing line-oriented I/O, you have to watch for different interpretations of the end-of-line. No two operating system designers can seem to agree on "how lines should" in text files. On UNIX systems, lines end with the linefeed character (LF, octal/012 in the ASCII table); On Macintosh systems, they end with the carriage return character (CR, octal/015); And the Windows/dos designers decided to "end" of the text with two characters, a carriage return/linefeed pair (CRLF, or octal/015/012). Most line-oriented network servers also use CRLF to terminate lines.






This leads to endless confusion when moving text files between machines. Fortunately, Perl provides a way to examine and change the end-of-line character. The global variable $/contains the current character, or sequence of characters, used to signal the "end of". By default, the It is set to/012 on the Unix systems,/015 on Macintoshes, and/015/012 on Windows and DOS systems.






The line-oriented <> input function would read from the specified handle until it encounters the End-of-line R (s) contained in $/, and return the line of text with the end-of-line sequence still attached. The Chomp () function looks for the end-of-line sequence at the end of a-text string and removes it, respecting the current Value of $/.






The string escape/n is the logical newline character, and means different things on different platforms. For example,/n is equivalent to/012 on UNIX systems, and to/015 on Macintoshes. (On Windows systems,/n is usually/012, but the later discussion of DOS text mode.) In a similar vein, the/R is the logical carriage return character, which also varies from system to system.






When communicating with a line-oriented network server this uses CRLF to terminate lines, it won ' t being portable to set $/t o/r/n. Use the explicit string/015/012 instead. To make this less obscure, the Socket and Io::socket modules, which we discuss in great detail later, have a option to ex Port Globals named $CRLF and CRLF () the correct values.






There is a additional complication when performing line-oriented I/O on Microsoft Windows and DOS machines. For historical reasons, Windows/dos distinguishes between filehandles in "text mode" and those in "binary mode." In binary mode, what you are exactly what for you. When you print to a binary filehandle, the data are output exactly as you specified. Similarly, read operations return the data exactly as it is stored in the file.






In text mode, however, the standard I/O library automatically translates LF into CRLF pairs on the way out, and CRLF Into LF on the way in. The virtue of this is, it makes text operations on Windows and UNIX Perls look at the Same-from the programmer ' s point of View, the DOS text files end in a single/n character, just as they does in UNIX. The problem one runs into are when reading or writing binary files-such as images or indexed databases-and the files become Mysteriously corrupted on input or output. This is due to the default Line-end translation. Should This is happen to you and you Should turn off character translation by calling Binmode () on the filehandle.









Binmode (FileHandle [$discipline])




The Binmode () function turns on binary mode for a filehandle, disabling character. It should be opened, but before no I/O with it for the called after the FileHandle. The Single-argument form turns on binary mode. The Two-argument form, available only with Perl 5.6 or higher, allows you to turn binary mode in by Providing:raw as the Value of $discipline, or restore the default text mode Using:crlf as the value.


Binmode () only has a effect on systems like Windows and VMS, where the end-of-line are more sequence one than. On UNIX and Macintosh systems, it has no effect.




Another way to avoid confusion over text and binary the mode is to


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.