Rather than a workaround, you may want to read perlopentut (gentle) and open.

Update with code: As the docs for open point out, this is NOT the preferred way to do it, but does this perhaps do what you wanted (preserve the prior content of main_log.txt, and append the content of sub_log.txt)?

# 795500 # $| = 1; # autoflush my $main_log = 'main_log.txt'; my $sub_log = 'sub_log.txt'; open LOGFILE, '<', $main_log or die "Cannot open $main_log for read, $!"; my $prior_content; while (<LOGFILE>) { $prior_content = $prior_content . $_; } close( LOGFILE ); open LOGFILE, '>', $main_log or die "Cannot open $main_log for write, $!"; print LOGFILE $prior_content; print LOGFILE "\n---\n"; print LOGFILE "\n\tStart: Coming from the script.\n"; open( SUBLOG, '<', $sub_log ) or die( "Error: Cannot open sub_log" ); print LOGFILE $_ while( <SUBLOG> ); close( SUBLOG ); print LOGFILE "\n\tEnd of sub_log as read by script.\n"; print LOGFILE "\tEnd: Coming from the script.\n"; close( LOGFILE );

If it's not what you intended, you may wish to deal with the problems almut pointed out... AND clarify your question (because your code doesn't do so for me).


In reply to Re: How to Write Information to an Opened File by ww
in thread How to Write Information to an Opened File by bichonfrise74

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.