Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Writing to a file at a specific point.

by mojobozo (Monk)
on Nov 14, 2002 at 16:20 UTC ( #212889=perlquestion: print w/ replies, xml ) Need Help??
mojobozo has asked for the wisdom of the Perl Monks concerning the following question:

I know how to append to a file, write to a file, etc. thanks to the tutorials. What I'd like to know though is how to write to a file at a specific location within the file, ala a guestbook.
_____________________________________________________
mojobozo
word (wrd)
interj. Slang. Used to express approval or an affirmative response to
something. Sometimes used with up. Source

Comment on Writing to a file at a specific point.
Re: Writing to a file at a specific point.
by Mr. Muskrat (Abbot) on Nov 14, 2002 at 16:26 UTC

    Guestbooks generally slurp the entire file. Then split on a specific line of text in the file
    (i.e. <!-- Add Guestbook Entries Here -->).
    Then it's simply a matter of writing out both sections of the guestbook file with the additional text added in between.

Re: Writing to a file at a specific point.
by Abigail-II (Bishop) on Nov 14, 2002 at 16:45 UTC
    If you want to write at a specific location, use the seek function. I doubt however that you can use it for a guestbook. Unless you write fixed size records.

    Abigail

      Abigail-II++,
      I started to include seek in a second paragraph just because TIMTOWTDI but decided against it because of the mention of a guestbook by the OP.

Re: Writing to a file at a specific point.
by talexb (Canon) on Nov 14, 2002 at 18:44 UTC

    Maybe I'm being thick, but can you explain the guest book comparison?

    The only way that I know how a guest book works is that new stuff gets appended to the end of the file. It sounds like you're talking about seeking to somewhere inside the file, that is, not to the beginning or the end.

    Help us understand the 'guest book' referencek, and I'm sure we can provide suggestions on a solution.

    --t. alex
    but my friends call me T.
Re: Writing to a file at a specific point.
by DigitalKitty (Parson) on Nov 14, 2002 at 19:09 UTC
Re: Writing to a file at a specific point.
by mojobozo (Monk) on Nov 15, 2002 at 16:20 UTC
    Well, as I couldn't figure out how to go back and update my original post, I'm doing it thusly!
    My solution (ala Mr. Muskrat): I slurped the file into a $var, split it on the magic line ("add here" , basically) into an @file, then opened the file again, printed into it $file[0], my new crap, magic line, $file[1]. close file. BINGO!
    My thanks to all who have helped me. This has definitely been a learning experience and I'm getting more and more intimate with perl. Almost to second base... (oh, baby)
    _____________________________________________________
    mojobozo
    word (wrd)
    interj. Slang. Used to express approval or an affirmative response to
    something. Sometimes used with up. Source

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://212889]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-07-12 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls