473,508 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ


Perl File Manipulation

By Blair Ireland
Senior Editor, TheScripts.com

File Manipulation in Perl

During this whole tutorial, I've been teaching you how to deal with and operate Perl functions. As of right now though, this means nothing to you, as I have not taught one of the most important things...... How to manipulate your data.

Without something to manipulate, all that I have taught and explained to everyone so far is worthless. So get your supplies ready, and be prepared to start hibernating in front of that computer screen of yours..... and maybe buy some of that tan-in-a-can stuff so at least it looks like you have been outside lately....

To manipulate data, you utilize these two great words, Input and Output. For input and output, the two main types are user and file input-output.

User input is obviously what the user is telling your program to do. The output is therefore what the program is spitting back out onto the screen. Usually in CGI though, this aspect of Perl is generally not used as much...... as your form parsers gather all the data you need.

File Input and Output is always used though, as it allows you to store data users send you.

Reading a File:

To read from a file, it must be opened and given a name first. You must also know the full path to the file. Below is an example...

open (STUFF, "/home/stuff/stuff.txt");

Usually the name given to the file is descriptive and will allow you to easily identify what the file contains later.

If you want to be able to manipulate this data, you must save it into a scalar variable ($) or an array (@) beforehand. Saving it into a scalar variable will put everything in that file on one single line. Saving the file into an array, on the other hand, will split up the file by each new line and store it into each slot in the array. Basically, it would look like this, if you used STUFF as the file handle that is....

$stuff = <STUFF>;

or

@stuff = <STUFF>;

If you want to be able to write to this file again later, or just hate sloppy coding, you must close this file after you manipulate the file in whichever way you feel like. To close the file, just write something like,

close <STUFF>;

Now that you have all the data in your scalar or array variable, you can apply all that you have so graciously been taught by us at TheScripts.com, but now you want to save this data back into a file.... hmm, there are two ways you can do this. If you manipulated all of the data, you will want to write everything again back to the file. Otherwise, you may just want to append the new data to the end of the file. Below are examples of both;

Writing to a File:

open (STUFF, ">/home/stuff/stuff.txt");

Appending to a File:

open (STUFF, ">>/home/stuff/stuff.txt");

Now that you have opened your file for writing, you will want to know how to write to the actual file. You would do so by the following;

print STUFF "Here is a new line of data being written to the file, along with a new line\n";

Understand? Good. Now I am sure I can send you out there to start making your scripts, or at least be able to modify existing ones to suit your needs.

« Perl Control Statements Perl Conclusion »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.