★ wanayoo — archive 1999 http://www.networkcomputing.com/netdesign/perl2.htmlNouvelle recherche | Portail wanayoo

       
NWC     Internet   Site Map

Getting StartedTechnology GuideDepartmentsresearch CenterSales & Marketing
FAQs Site Map Staff Article Index Current Issue




Beginning PERL: Part II

July 31, 2000

Brought to you by:



This manuscript is an abridged version of a chapter from the Wrox Press book Beginning PERL.

Our expert, Simon Cozens, will enlighten you with our latest network design manual chapter, "Beginning Perl." See how to handle files, directories, data and more.


Table of contents:

Got a tough question on Perl?
Ask the experts!

For a limited time, you can put Simon Cozens, author of "Beginning Perl," to the test.

Post your question, and if Simon answers it, you'll receive a free Network Computing collectable. Click here for more info.

Writing to Files

We've been using the print operator to print a list to standard output. We'll also use a different form of the print operator to print to a file. However, as we mentioned above, files are usually open either for reading or for writing — not both. We've been opening files and reading from them, but how do we open them for writing?

Opening a File for Writing

We actually use a syntax that's used by the shell for writing to files. In Windows and UNIX, if we want to put standard output into a file, we add the operator > and the file name to the end of the command. For example, saying something like this:

> perl summary.plx chapter6 > summary6

will create a file called summary6, which contains the following text:

1:We're starting to write real programs now, and real programs

2:What we want to do in this chapter is extend these techniques

3:First though, let's do some groundwork. When we're dealing

4:We've actually already seen a filehandle: the STDIN of <STDIN>.

5:As a counterpart to standard input, there's also standard

6:Every program has these three filehandles available, at least

Now, to open a file for writing, we do this:

open FH, "> $filename" or die $!;

This will either create a new file or completely wipe out the contents of an already existing file and let us start writing from the beginning. Don't use this on files you want to keep! If we want to add things to the end of an existing file, use two arrows, like this:

open FH, ">> $filename" or die $!;

There's no easy way of adding or changing text at the beginning or middle of a file. The typical way to do this is to read in the original and write the changed data to another file. We'll see shortly how this is done.

Similarly, you can redirect data to a program's standard input by using the left arrow, like this:

>perl summary.plx < chapter6.txt

As you've probably guessed, this means you can open files for input by saying:

open FH, "< $filename";

This is exactly the same as open FH, $filename; as we've used previously; it's just a little more explicit.

Writing on a Filehandle

We're now ready to write the file, which we'll do by using a special form of the print operator. Normally, to print things out from the screen, we say this:

print list;

When we want to write to a file, we'll use this instead:

print FH list;

So, for instance, here's one way of copying a file:

Try It Out: File Copying

We'll read in a file one line at a time, writing each line onto the new file:

#!/usr/bin/perl

# copy.plx

use warnings;

use strict;

my $source = shift @ARGV;

my $destination = shift @ARGV;

 

open IN, $source or die "Can't read source file $source: $!\n";

open OUT, ">$destination" or die "Can't write on file $destination: $!\n";

print "Copying $source to $destination\n";

while (<IN>) {

print OUT $_;

}

Now there isn't much to see in this program, but let's run it anyway:

> perl copy.plx gettysburg.txt speech.txt

Copying gettysburg.txt to speech.txt

>



PAGE: 1 I 2 I 3 I 4 I 5 I NEXT PAGE
 
Advertisement

Printer Print Full Article
Printer Print This Page
E-Mail E-mail this URL
In This Article
arrow  Beginning Perl: Part I
arrow  Ask A Perl Question
arrow  Answers to Perl Q & A

Tool
arrow  Events
arrow  IT Training
arrow  Company Directory
arrow  Reader Service

Get NWC
Subscribe to Network Computing magazine and our Free, weekly E-Mail newsletter.


Spotlight
E-Commerce Survey!
Are you conducting E-business? If so, we want to hear from you. Please take a few minutes and fill out our online questionnaire on payment services. You could win a $200.00 American Express gift certificate!


UnixWorld | Network Design Manual | Interactive Buyer's Guide | WANsites | Real-World Labs | Learn IT | Careers | Article Index |

Home | Technology Guides | Site Map | FAQ | Subscriptions | Contacts | Sales & Marketing | 2000 Edit Calendar |

Network Computing

Byte.com |  Bank Systems & Technology |  CMPmetrics |  eBusiness Expo |  File Mine |  InformationWeek |  Insurance & Technology | 
InternetWeek |  PC Expo |  Planet IT |  TechCalendar |  TechEncyclopedia |  TechLearning | 
TechShopper |  TechWeb News |  TechWeb Today |  Wall Street & Technology |  WebTools |  Winmag.com | 


TechWeb is brought to you by CMP Media, Inc., Copyright © 2000 - Privacy Statement