Perl file handle details

Source: Internet
Author: User
Tags chop

In file I/O, to read data from a file, the application must first call the operating system function, transfer the file name, and select a path to the file to open the file. This function retrieves a sequence number, namely, the filehandle of a Perl file. This Perl file handle is the only identification basis for opened files. To read a piece of data from a file, the application needs to call the ReadFile function and transmit the address of the Perl file handle in the memory and the bytes to be copied to the operating system. After the task is completed, the file is closed by calling the system function.

In addition to writing an artificial intelligence program by imitating the philosophers of the owner, your program uses methods that do not communicate with the outside world. In the third and fourth rows of the class example, you will see "GRADES", which is the data type that references another Perl file, called filehandle ). A handle is a name that you give to a file, device, socket, or pipe, so that you can remember the name you are processing and hide the complexity of some caches. (Internally, the handle is similar to the stream in the C ++ language (streams), or the I/O channel in BASIC) the handle makes it easier for you to input and output data from different places to different places. One reason for making Perl a good language is that it can communicate with multiple files and process them at a time. A good symbolic name for an external object is an integral part of a good language [1].

Others make Perl a good language: It is 8-bit and can be embedded. You can embed other programs in Perl through the extension mode. It is concise and easy to use on the network. The environment is clear and easy to talk. You can reference it in many different ways (as shown above ). In short, the language itself is not so strictly structured that you cannot make it more than your problem. Return to TMTOWTDI.

You create a handle and use the open function to connect it to a file. Open has two parameters: the handle and the file name you want to connect to it. Perl also provides some predefined (and pre-opened) handles. STDIN is the normal input channel of your program, while STDOUT is the normal output channel of your program. STDERR is an additional output channel, so that when the input is converted to the output, the program can provide some instructions [2].

Generally, these handles are linked to your terminal, so you can enter your program and see it, but they can also be linked to files. Perl can provide you with these predefined handles because your operating system already provides these. In UNIX, a process inherits standard input, output, and error from its parent process (usually a shell. One of the responsibilities of a shell is to establish these I/O streams so that sub-processes do not have to consider these ).
Since you can use open functions to create handles for various purposes (input, output, and pipeline), you must specify what you want to do. Just like in a UNIX command line, you simply add some characters to the file name.

Copy codeCode: open (SESAME, "filename"); # Read from an existing file
Open (SESAME, "<filename"); # explicitly, same as above
Open (SESAME, "> filename"); # create a file and write
Open (SESAME, "> filename"); # Write
Open (SESAME, "| output-pipe-command"); # create an output filter
Open (SESAME, "input-pipe-command |"); # create an input filter

As you can see, you can select any name. Once the handle SESAME is opened, it can be used to access files or pipelines until it is explicitly closed (with close (SESAME )), or a series of openers with the same handle can link the handle to another file [3].

Opening an opened handle is to implicitly close the first file so that it is not accessible to the Perl file handle and open a different file. You must be careful that this is what you really want to do. Sometimes, by chance, for example, when you open ($ handle, $ file), $ handle exactly contains null strings ). Make sure to set $ handle to a single volume. Otherwise, you will open a new file with an empty handle.
Once you open a handle for the input (or you use STDIN), you can use "row read Operations" <> to read a row. This is also known for its diamond operation because of its shape. This diamond operation contains the handle you want to read () [4]. Use the STDID handle to read the answers provided by the user, as shown below:

The empty diamond operation <> reads all files specified by the command line. If not, it reads data from STDIN. (This is the standard behavior of many UNIX "filter" programs)

Copy codeThe Code is as follows: printSTDOUT "Enteranumber:"; # Number of input requests
$ Number = <STDIO>; # enter a number.
PrintSTDOUT "Thenumberis $ number"; # output this number

Do you understand the example we gave you? In the print statement, what does STDOUT do? This is one of the methods for using an output handle. A handle can be used as the first parameter of the print statement. If it exists, it indicates where to output it. In this example, the handle is redundant because the output is already STDOUT. The default input value is STDIN, and the default output value is STDOUT. (In the 18 rows of the class example, we omitted it to avoid confusion .)
We also have one thing you don't understand. If you try the above example, you can notice that you get a special blank line. Because the newline is not automatically deleted from your input line during reading (for example, you enter "9 "). In these cases, Perl provides the chop and chomp functions when you want to delete line breaks. Chop deletes (and returns) The last character that is passed to it without any difference, while chomp only deletes the end of the record identifier (generally ""), returns the number of characters to be 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.