|
Earlier in the Perl Tutorial we saw how to open a file for reading or writing. Unfortunately, when you search the web, or when you look at code in corporations you will see some slightly different syntax. Let's see what is that and what is the problem with that?
So what shall I do?Before explainin what you should not do, let me link you to the articles explain what you should do: Read how to open file for reading in a modern way or the one about writing to file in Perl. Now let's get back to the old and not-so-good-any-more practices.
The old and not recommended wayUntil perl 5.6 came out - that's until 2000 - we used to write code like this to open a file for writing: open OUT, ">$filename" or die ...; and code like this for reading: open IN, $filename or die ...; The "or die" part was the same as we do today, not fully spelled out here. As you can see open got two parameters. The first is a set of (usually upper-case) letters. That's the thing that will get the file-handle. The second is the combined opening mode and the path to the file that needs to be opened. That is, in the first case you see the greater-than sign meaning we are opening the file for writing but in the second example we used to omit the opening mode. That's because open() defaults to reading. There are two big differences:
Filehandle globThe first is that we use the strange variable without the leading $ to hold the filehandle. It works of course but there are several problems with it: It is global to all the application you write so if anyone in any module uses the same name (IN or OUT in our example) those will clash with yours. It is also harder to pass these variables to functions, than to do the same with regular scalar variables.
2 parameter openThe second difference is the fact that open got only two parameters. What if the variable $filename that you are using to open a file for reading, contains >/etc/passwd ? Oups. The open IN, $filename will actually open that file for writing. You just deleted the password file of your Linux operating system. Not good.
Need to close that filehandleAnother advantage of using lexically scoped scalar variables as file handles is that they will automatically be closed when they go out of scope.
How to avoid these problems?It's better to avoid both of these practices and use the "new", (11 years old?) 3-parameter open with scalar lexical variable for storing the file-handle. There are even policies in Perl::Critic that will help you analyze the code and locate every place where someone has used either of the above forms.
Good and Bad for readingBad: open IN, $filename or die ...; Good: open my $in, '<', $filename or die ...;
Good and Bad for writingBad: open IN, ">$filename" or die ...; Good: open my $in, '>', $filename or die ...;
Perl tutorial and video courseFor further articles see the Beginner Perl Maven tutorial book and video course.In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation. blog comments powered by Disqus |
Follow me: