Home arrow Perl Programming arrow Perl: Working with Files

Perl: Working with Files

In our last series of tutorials we worked with Conditionals and Loops to create some basic Perl programs. This time around we are going to be working with files. Text files, CGI files, PL files, boiled files, fried files, Files Benedict, steamed Files. Okay, so I was kidding about the steamed Files.

  1. Perl: Working with Files
  2. How to Open a File
  3. How to Read a File
  4. Manipulating Data
  5. How the Code Works
By: James Payne
Rating: starstarstarstarstar / 8
November 07, 2007

print this article



How to Create a File

Creating a file to read from in Perl is pretty simple. Simply open up a .txt file and enter the following data:

  The Incredible Hulk|Super Strength|I rip my pants

  Daredevil|Heightened Senses|I have poor fashion sense due to blindness

  Apache Chief|the ability to grow Very Tall|I Wear a skirt

When you enter the data, note a few things. First, there are no headers here. Second, we separate each column of data with a pipe(|) symbol. And third, each line of data is separated by pressing the Enter key. Note that nothing should appear on the blank lines, not even a space. This is because when Perl reads it, it will read that line as a new row of data. 

When you finish entering the data, save the file. You can leave it as a .txt file if you like. The only downside to this is that the data will be visible to any users who happen upon your page. If you want to limit visibility of the data, you can save it as a .cgi or .pl file, depending upon your host. For simplicity's sake, save the file (use the name super.txt) in the same directory as you are saving your script. If you don't, you will have to point to the directory where your file is being saved.

At this point we could set permissions, but I will save that for another time. Setting permissions allows you to choose whether the file can only be read, can be edited, neither, or both.

>>> More Perl Programming Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: