Home arrow Perl Programming arrow Page 2 - Perl: Appending and Writing to Files

Writing to Files - Perl

In our last tutorial we left off on the topic of creating files and manipulating the data therein. In this article we will discuss how to append to a file and how to write to a file. If there is time, we will also discuss working with file checks.

TABLE OF CONTENTS:
  1. Perl: Appending and Writing to Files
  2. Writing to Files
  3. Overwriting Without Deleting Everything
  4. How to Delete a File
  5. More Ways to Test Files
By: James Payne
Rating: starstarstarstarstar / 9
November 14, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The phrase "writing to files" is a little misleading. When I think of writing to a file, I am really thinking of appending to the file. Why they didn't just name it Overwriting Files, I will never know, because that is really what you are doing. Basically the original file is overwritten and new data is written in its place.

Let's say we want to get rid of all the heroes in our database and add news ones to it. We do it the same way we append to a file except for this line:


open(DAT,">$my_file") || die ("The file has died!");

As you can see, we now only use one greater than (>) key. This tells the program that we are writing (or overwriting) data. You assign the new values the same way as before.


$my_file="superhero.txt";

$heroname="Batman";

$heropower="None. He does not need them";

$heroweakness="Slow speech and Action Bubbles";


open(PLOT,">$my_file") || die("The File is dead!");

print PLOT "$heroname|$heropower|$heroweaknessn";

close(PLOT);

If we printed this file, this would be the contents: Batman|None. He does not need them|Slow speech and Action Bubbles



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- 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: