🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Writing to file?

#47
From: Laird Nelson
Date: Thu, 07 Dec 1995 00:00
47 lines
2288 bytes
Zenin (zenin@best.com) wrote:
> 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. :(

Have you grepped the perlfunc manpage for "open"?  This is a basic
usage of said function.

You open a file for reading by prefixing the filename with a "<" (or
nothing; it opens for reading by default), and for writing by
prefixing the filename with a ">".  I guarantee you will write several
scripts in which you open one file for reading, and hope to open
another for writing, but you'll forget the ">", and you'll spend at
least four hours wondering what the hell is the matter.  :)

This program, if I have my syntax right, opens $infile for reading,
$outfile for writing, and, for each line in $infile, writes the
following string to $outfile: "foobar.txt's line [whatever the line
number is] contains: [contents of current line from foobar.txt]".

I heartily recommend reading the perlfunc manpage, followed by Tom
Christiansen's FAQ.  That is, you are nuts if you don't.  :)

#!/usr/local/bin/perl -w

$infile = "foobar.txt"; # or whatever
$outfile = "snarfblat.txt"; # or whatever

open (INFILE, "<$infile");
open (OUTFILE, ">$outfile");

while (<INFILE>) {

    print OUTFILE "$infile's line $. contains: $_";

}

__END__



--
laird j. nelson                                           (617) 252-5147
at&t interchange online network                           25 first street
http://www.amherst.edu/~ljnelson/                         cambridge, ma 02143
    I do not speak for my employer, and my employer does not speak for me.
Technical Details
Message-ID: <4a6vfr$kg@amhux3.amherst.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: 112e82,3944997a5420a56f
X-Google-Attributes: gid112e82,public
X-Google-Thread: fa0ae,3944997a5420a56f
X-Google-Attributes: gidfa0ae,public
From: ljnelson@unix.amherst.edu (Laird Nelson)
Subject: Re: Writing to file?
Date: 1995/12/07
Message-ID: <4a6vfr$kg@amhux3.amherst.edu>
X-Deja-AN: 121150944
references: <4a54h9$a0g@shellx.best.com>
organization: last check
followup-to: comp.lang.perl.misc,best.unix,best.general
newsgroups: comp.lang.perl.misc,best.unix,best.general