Perl File Handle Details _ Application Tips

Source: Internet
Author: User
Tags chop stdin

In file I / O, to read data from a file, the application program must first call the operating system function and transfer the file name, and choose a path to the file to open the file. This function retrieves a sequence number, which is a Perl file handle (filehandle). The Perl file handle is the only basis for identifying the opened file. To read a piece of data from a file, the application needs to call the function ReadFile and transfer the address of the Perl file handle in memory and the number of bytes to be copied to the operating system. When the task is completed, the file is closed by calling a system function.

Except you write an artificial intelligence program that imitates a solipsist philosopher, your program uses methods that do not communicate with the outside world. In the third and fourth lines of the class example, you will see "GRADES", which is a data type that references another Perl file, called a filehandle. A handle is a name you give a file, device, socket, or pipe to help you remember the name you are dealing with, and hide some of the complexity of caching, etc. (Internally, handles are similar to C ++ streams, or I / O channels in BASIC.) Handles make it easier for you to input and output from different places. One of the things that makes Perl a good language is that it can communicate with multiple files and process them all at once. Having good symbolic names for external objects is an integral part of a good language [1].

Other things that make Perl a good language are: it is 8-bit, it is embeddable, and you can embed other programs in Perl through the extension mode. It is concise and easy to use on the web. The environment is clear and easy to talk to. You can reference it in many different ways (as you saw earlier). In short, the language itself is not so strictly structured that you cannot make it beyond your question. Back to TMTOWTDI.

You create a handle and associate it with a file via the open function. open takes two parameters: the handle and a file name you want to associate with it. Perl also gives some predefined (and pre-opened) handles. STDIN is the normal input channel of your program, and STDOUT is the normal output channel of your program. STDERR is an additional output channel so that the program can give some instructions when turning input into output [2].

Generally, these handles are connected to your terminal, so you can enter your program and see them, but they can also be connected to files. Perl can give you these predefined handles because your operating system already provides these. Under UNIX, a process inherits standard input, output, and errors from its parent process (typically a shell). One of the responsibilities of a shell is to build these I / O streams so that child processes don't have to think about these).
Now that you can use the open function to create handles for various purposes (input, output, pipe), you must be able to indicate what you want to do. Just like on the UNIX command line, you simply add characters to the file name.

Copy the code:

open (SESAME, "filename"); #Read from an existing file
open (SESAME, "<filename"); #Explicitly, same as above
open (SESAME, "> filename"); #Create a file and write it
open (SESAME, ">> filename"); #Continue writing to existing files
open (SESAME, "| output-pipe-command"); #Establish an output filter
open (SESAME, "input-pipe-command |"); #Create an input filter
As you can see, you can choose any name you like. Once the handle SESAME is opened, it can be used to access a file or pipe until it is explicitly closed (with close (SESAME)), or a series of open to the same handle connects this handle to another file [3 ].

Opening an opened handle implicitly closes the first file, making it undesirable for a Perl file handle, and opening a different file. You have to be careful that this is what you really want to do. Sometimes, by chance, for example, when you open ($ handle, $ file), $ handle contains exactly the empty string (null). Make sure to set $ handle to a single amount, otherwise you will open a new file for the empty handle.
Once you have opened a handle for input (or you are using STDIN), you can use "row read operation" <> to read a line. This one is also known for its diamond operation because of its shape. This diamond operation contains the handle () [4] you want to read. Use the STDID handle to read the answer provided by the user, as follows:

The empty diamond operation <> will read from all files specified on the command line, if not specified, read from STDIN. (This is standard behavior for many UNIX "filtering" programs)

Copy the code:

printSTDOUT "Enteranumber:"; #Request a number
$ number = <STDIO>; # Enter a number
printSTDOUT "Thenumberis $ number"; # Output this number
Do you understand the example we gave you? What does STDOUT do in the print statement? This is one of the ways you use an output handle. A handle can be used as the first parameter of the print statement, and if present, tells where to output. In the example, the handle is redundant because the output is already STDOUT. The default is STDIN for input and STDOUT for output. (In line 18 of the class example, we have omitted it to avoid confusing you.)
We also have one thing that makes you wonder. If you try the above example, you can notice that you get a special blank line. Because the newline is not automatically removed from your input line when you read it (for example, you type "9"). For these cases, Perl provides chop and chomp functions when you want to remove newlines. chop deletes (and returns) the last character passed to it indiscriminately, while chomp only deletes the end of the record identifier (generally ""), and returns the number of characters thus deleted.

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.