Join Bill Weinman for an in-depth discussion in this video Using the OO interface for files, part of Perl 5 Essential Training.
- [Voiceover] The IO::File module provides an object-oriented interface for the standard file and stream functions. This is the most common way to work with files in Perl. So here's a working copy of iofile.pl from chapter 12 of the exercise files. And you'll notice down here on line 6, I import the IO::File module, and we'll talk about modules a little bit more later on, but this is how you import a module. And with the two colons there, it indicates that the IO::File module is part of a set of modules that share the IO namespace.
So down here in line 10, we create our file handle using the IO::File module. And in this case, our file handle is actually referenced to an object. So we have IO::File here with the dereferencing arrow, which is how you access methods and properties inside of a class or an object, and here we're using the class IO::File and the new method as a constructor, to construct this new object. And so the constructor returns the reference, and in the constructor, we simply provide the file mode and the file name, and we use the same or die structure that we used with the plain file handle.
And now our file object can be used exactly as we would use a file handle, so we can do this print while, and we can close in the same way. And so technically the close is not necessary here. The object interface closes the file in the destructor, so the file be closed automatically when the object goes out of scope. So I can use this, or I can not use it. I'm actually going to delete it here, because we don't need it. And I'm just gonna go ahead and run this, and it runs exactly as we would expect.
And so we have our lines.txt file, which I can show you right here, and we have copied that to the console, and it looks exactly like it's supposed to look. So now we can copy the file, just like we did with our file handle. So I'm gonna create a file name for our outfile like this. And I'm going to create a separate file handle. So change this to file1 and I'm going to create file2, IO::File->new and this will be a writing file, so I'm going to create a new file, and this will use the outfile file name, or die just like that.
And now I can use my while loop here, and I'm using the getline function. I could do this just with the file1 in angle brackets like I did before, but this is actually the more recommended object-oriented way, using the getline function, which pretty much does the same thing, but it's a little more explicit. And then I write to file2 like this, again using my object reference arrow operator, and I say print($line) just like that.
And now when I'm done, I just say Done. And I'll go ahead and run this, and you notice it runs, and there's our newfile.txt with exactly the same as the lines.txt. They are exactly the same. So you can see this is a little bit more straightforward. In some ways, it might seem like a little bit more typing, but for the most part, it's much more clear and easy to understand what's going on with the object-oriented interface. I can tell that I am reading from file1, and I'm writing to file2 because of the clear object-oriented notation.
And I understand this is our first exposure to object-oriented notation in Perl, but this is what it looks like, and it's actually really clear and very common. This is the common way to access files in Perl. You notice that the documentation looks a little bit thin. It shows how to open a file, and how the constructor works, and open method, and the binmode method, which we'll get to in the next lesson. And that's really all there is here in this documentation. So you look at what I'm doing, here I've got print here, I've got getline, where are those functions? So those are actually documented in IO::Handle, and IO::File inherits from IO::Handle, and this is where all the meat is, because you remember, it doesn't have to be a file on the disk.
It can be a pipe, it can be a socket to a network connection, it can be all kinds of things, and they all operate in the same way. And so all of these methods, for the most part, they all operate exactly the same no matter what your source or your destination. And so all of that documentation is here under IO::Handle, and it's really quite a bit of stuff, and it's quite detailed, and this is where you can learn about all of the things that you can do with a file. But for the most part, reading and writing a file is done like this, and we'll look later on in this chapter at how you can read and write binary files as well.
So the IO::File interface is both simple and powerful. This is the most common way to handle files in Perl, and you should use this interface when writing new code, wherever possible.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices