🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.perl.misc
Article #49

Re: Writing to file?

#49
From: Jon Orwant
Date: Mon, 11 Dec 1995 00:00
44 lines
1672 bytes
In article <4a54h9$a0g@shellx.best.com> Zenin <zenin@best.com> writes:

   I can't seem to figure out how to write to a file.
   I thought it was somthing like
	   write(FOO, "filename");

   but I guess it's no so easy. :(

Oh, it is that easy.  But you don't want write()---you want print().
write() is for strange beasts called formats.

   So what is the syntax to do somthing like:
	   #!/usr/bin/perl
	   open(FOO, "original_filename");
	   while(<FOO>) {
		   do some stuff;
	   }
	   write(FOO, "new_filename");
	   close FOO;


#!/usr/bin/perl

open (FOO, "original_filename");
while (<FOO>) {
	do some(stuff);
}

open (FOO, ">new_filename");     # open new_filename for writing,
                                 # implicitly closing the old FOO
print FOO "some new text\n";     

close FOO;                       # not necessary, but good style.



To open FOO for both reading and writing, you can use
open (FOO, "<+filename");



Jon Orwant
MIT Media Lab
Technical Details
Message-ID: <ORWANT.95Dec11001738@fahrenheit-451.media.mit.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <4a54h9$a0g@shellx.best.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f9f6d,3944997a5420a56f
X-Google-Attributes: gidf9f6d,public
X-Google-Thread: fa0ae,3944997a5420a56f
X-Google-Attributes: gidfa0ae,public
X-Google-Thread: 112e82,3944997a5420a56f
X-Google-Attributes: gid112e82,public
From: orwant@fahrenheit-451.media.mit.edu (Jon Orwant)
Subject: Re: Writing to file?
Date: 1995/12/11
Message-ID: <ORWANT.95Dec11001738@fahrenheit-451.media.mit.edu>
X-Deja-AN: 121459944
references: <4a54h9$a0g@shellx.best.com>
to: Zenin <zenin@best.com>
organization: MIT Media Lab
reply-to: orwant@media.mit.edu
newsgroups: comp.lang.perl.misc,best.unix,best.general