Perl Programming - More Advanced Perl Programs Part 2
Get the entire Perl Programming course for 20% off: http://stoneriverelearning.com/courses/perl-programming-for-beginners-online-course?product_id=38639&coupon_code=YOUTUBE20
This is another more advanced Perl application I’m going to show you guys just for demonstration purposes. I’ll actually not start off with the #!/usr/bin/perl. For this demonstration, I’m not going to use it. So I’ll just start off with my Perl instructions or Perl commands. In this Perl example, I’m going to read from a file and write to a file. Let me begin by reading from a file. So I’ll pull up one of my files in a Notepad. It’s called test.txt. As you can see, I just have three names here. So I’ll close it just to show you guys what’s inside that text file. I’ll begin with the file name. The file name is test.txt and I want to be able to open that file to read the contents into my Perl program. So I’ll use the built-in subroutine in Perl called open. I’ll create a container or a file handle is what it’s called for the first argument of the open subroutine by Perl. I want to read from the file and I want the path of the file that I want to open.
Let’s actually see what’s inside the file. So while there is content inside the text file, I want to read all of that content line by line into my Perl program. So I’ll create a variable to store each line of content from the file name which is test.txt. For each line inside the test.txt file, I want to store each line of information inside the container row. I want to use the file handle. I’ll use those. Again, in our following lectures, I’ll explain everything in details. I’ll use another built-in Perl subroutine called chomp. For its argument, I’ll pass in a value. I want to print that line of content. So I’ll pass that value. Of course, I want to create a new line to make it more clear and readable. So if I save that, let’s see what happens. I’ll run it, F5, and there we have it. Victor Davis, Jaddian Forte, Jason Williams. These are just the names I stored inside of my test.txt file. Open it up and I’ll click Edit. This is what you’ll see.
Now, you guys need to practice or it’s considered good practice. We need to close the file. After we’re done using the file, of course we want to close the file. So we have to remember this is the file handle that contains our file and we close it and that’s it. So run it again. Same thing but it’s safe now because it closes. Once we do that, let’s actually open the file now. Let me erase all this. Now just type it over again. File name is set to test.txt. We’re going to read from the test.txt file again or we’re going to write to the test.txt file. We’re going to use the built-in subroutine open. We want to create a file handle to reference our file name. The mode that we want to do where we want to write to the file, or you know what? Let’s append to the file. So we’re just going to add a data to the file test.txt. So print is going to be used to add a data to our file. I want to add data to our reference fh that contains test.txt and let’s add some more names: Bob Harrison, Jack Nicholson. Then we can go ahead and close the file. So Close. Put the file handle referenced to test.txt.
So now, if we click Run, these two names, Bob Harrison and Jack Nicholson, should be added to our test.txt file. If we click Run, as we can see, you think nothing happens here because nothing is being printed to our monitor or our standard output which is our display here. We just click Enter. Let’s actually look inside of our test.txt file. Right click and click Open. Here we have it. So we had our three and then we have Bob Harrison, Jack Nicholson. Let me just delete those to format it a little bit better for you guys. I’ll go back in here and I’ll click. I’ll add a new line. Let me see here. I’ll add a new line here as well. I’ll run it again. Enter and let’s check our test.txt file. We have to refresh it of course so I’m going to close it out and re-open it up. There we have it. It’s neatly printed out. This is one of the main purposes of Perl because the whole purpose of Perl is reading and analyzing text and generating a report for analysis for either an administrator or for another person to look at. So we can generate a nice, neatly-formatted report with some analyzed data from an input source. That was one of my other examples or more advanced examples we’re going to be learning throughout this course as well. It’s an extra bonus that I’ll probably throw in for you guys.