...without using a module, and without putting the whole file into an array or something? ... Um, well... you are already reading the whole file into a scalar, and modifying that scalar, so you're close; this get you rest of the way (mostly -- but it's not tested):
$/ = undef; $filename = "$root/content/topics/$get{'topic'}"; open( TOPIC, "+<", $filename ) or die "Unable to read/write $filename\ +n"; $_ = <TOPIC>; if( /^\Q$get{'post'}\E/ ) { s/.*\n$/$post/m; } seek( TOPIC, 0, 0 ); # rewind to start of file print TOPIC; close TOPIC;
Since this seems to be part of a CGI process, we have to raise the standard "issues"... You need to apply some form of file locking, because two browsers could try to make distinct changes on the same file at the same time. You probably need to include taint checking (at least), or employ some reasonably secure way of specifying the name of the file to be opened and over-written. (I've added the "easy" part: the contents of $get{post} need to be enclosed within the "quote-meta" operators \Q...\E, in case it happens to include things like period, plus, asterisk, parens, brackets, and so on, which might cause a run-time error or mis-match.)

In reply to Re: modifying a line in a file with some restrictions by graff
in thread modifying a line in a file with some restrictions by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.