Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hey,

if any of you would care to assist me...

im writing a script which opens itself does a bunch of modifications and then writes the changed code to another file.

first, it opens itself, reads certain lines into an array, then writes these lines to another file, then opens that file, does some more modifications (it does them on the file instead of the array because the code needs to pass over all the data at once , so i unset $/) and then i want it to write that output back to the file, but i cant write it back to the same file because if the file is opened for updating it, the file gets messed up, and if its opened for the other kind of read-write, then it gets clobbered...i know i could create a third file as a temp file but i'd rather not..so what i really need is either a way to traverse an entire array in one step so i dont have to write it to a file before everything is done, like unsetting $/, but for an array, or a way to solve the problem otherwise without creating a third file.

thank you.

Replies are listed 'Best First'.
Re: file editing/writing
by merlyn (Sage) on Jun 04, 2001 at 02:23 UTC
    One, don't do that. Store your data in a separate file. Don't rewrite the program.

    Two, if you must, see Inline::Files, the latest crazy thing from "the Damian". It has program rewriting capabilities as you desire.

    -- Randal L. Schwartz, Perl hacker

Re: file editing/writing
by Beatnik (Parson) on Jun 04, 2001 at 01:57 UTC
    I recommend using the 'in-place' command line parameter (like mentioned in in-place edit, regex, one-liner and covered extensivly in perlrun.

    As a sidenote: flock is pretty handy :)

    Update: Ok, I obviously misread the question (could it be the formatting??). Anyway, look at merlyn's answer.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: file editing/writing
by Anonymous Monk on Jun 05, 2001 at 19:05 UTC
    I think you write (the array) to the file and read it in (with undef $/) essentially to make a String from an Array. Use join instead.