in reply to Doing a "Print" to a predetermined location in another file

Right, I'm gonna tell you something you already know, and in fact, already expect and possibly even dread: (all together now) there's more than one way to skin a camel (forgive me the gratuitous rephrasing :) ). In your case, here are some ways of doing this:

* Way the first: read in your target file one line at a time, write it back out to a temporary file but add your new content when necessary:

use strict; my $newcontent="My camel has been bitten by a rather large flea. Pleas +e call a vet."; my $input="camel.cgi"; my $tempfile="/tmp/camel.$$"; open TARGET, "<$input" or die "Blerch: $!\n"; open TEMP, ">/$tempfile" or die "Hcerlb: $!\n"; while (<INPUT>) { # check for your cue: could be a running counter to # keep track of lines read, could be a regexp, could # be just about anything else. Let's assume the check # sets $ship_ahoy when you need to add lines. if $ship_ahoy {print TEMP $newcontent} print TEMP; } close INPUT; close TMP; rename $tempfile $input or die "Hrlecb:$!\n"; print "Beware, this is untested code, and could definitely do with mor +e robust error checking!\n";
* Way the second: play seek games. This is probably the easiest if you want to enter your text at a fixed position in the file. I'm pressed for time at the moment, so I'll leave the implementation of this as an exercise to the reader. The link above should get you almost the entire way there.

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Doing a "Print" to a predetermined location in another file
by Donnie (Acolyte) on Oct 12, 2002 at 17:49 UTC
    Dear Robartes, I want to personally thank you for your great and finely detailed reply to my question. This is just great! Yesterday I was blindly flailing around and this moring I have all this help! I will spend my day looking at all the information you and others provided and hopefully get this thing to work. You are most kind to provide the example and I am very grateful. With very best regards, Donnie