Perl writing to a file


















Instead we use the read function that has a weird way of use. The function will try to read that many bytes from the file and put them in the scalar variable replacing whatever we had there. Optionally we can also supply a number to be "OFFSET", telling the read function where in scalar variable it should put the newly read bytes.

If we supply the current size of the scalar using the length function, then we append the newly read bytes to the end of the scalar variable. If we read that way repeatedly then we can read the whole content of the file into a single scalar variable.

Of course assuming the file can fit in the free memory of our computer. Then it saves the content to the second file. Effectively copying the content. If read returned undef it means there was an error during the read operation.

We raise an exception by calling die. If read was successful, but it returned 0 that means there were no more bytes to read. We arrived to the end of file, we can leave the loop by calling last. Otherwise we go for another iteration. After saving the content we print out the size of the two files using the -s operator and the size of the scalar variable.

They should be all the same number. Before you can write to a file you need to open it, asking the operating system Windows, Linux, OSX, etc to open a channel for your program to "talk to" the file. For this Perl provides the open function with a slightly strange syntax. The open function gets 3 parameters. We could have defined it earlier, but usually it is cleaner to do it inside, even if it looks a bit awkward at first.

The second parameter defines the way we are opening the file. The third parameter is the path to the file that we would like to open. It is called file-handle. We don't care much about the content of this variable; we will just use the variable later. It looks almost the same as the print in other parts of the tutorial, but now the first parameter is the file-handle and there is no!

The print call above will print the text in the file. Then with the next line we close the file handle.

Strictly speaking this is not required in Perl. Perl will automatically and properly close all the file-handles when the variable goes out of scope, at the latest when the script ends. In any case, explicitly closing the files can be considered as a good practice.

Furthermore, we only got the warning because we explicitly asked for warnings with use warnings statement. Try commenting out the use warnings and see the script is now silent when it fails to create the file. So you won't even notice it until the customer, or - even worse - your boss, complains. Nevertheless it is a problem. We tried to open a file. We failed but then still tried to print something to it.

If you have any comments or questions, feel free to post them on the source of this page in GitHub. Source on GitHub. Comment on this post.

Gabor can help refactor your old Perl code-base. He runs the Perl Weekly newsletter. Contact Gabor if you'd like to hire his service.



0コメント

  • 1000 / 1000