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

I am trying to open a file to append and for some reason, it is not working and my commets like "Vijay944" is not written at all. Can someone help?

open NEW944, ">>$mail_944file" or warn $!; print LOG "open NEW944\n"; while (<NEW944>) { print LOG "inside NEW944\n"; print NEW944 "inside NEW944\n"; chomp; print LOG "Vijay944 - $_\n"; if ( ! /^darien_inbound/ ) { print NEW944 $mail_group,"\n"; print NEW944 " \n"; print NEW944 " + \n"; print NEW944 "Transaction ISA Ctl# Date +Time GS Ctl# Order# +\n"; print NEW944 " (YY/MM/DD) + \n" +; print NEW944 "------------------------------------------------- +-------------------------------------------\n"; } }

Replies are listed 'Best First'.
Re: question about open
by ig (Vicar) on Oct 28, 2010 at 05:20 UTC

    Even if you solve your problem reading and writing the same file, I think there is a problem with your logic. You read the file line by line from the beginning and for every line read you append a line and then, if the line you read doesn't match your pattern, you append six more lines, none of which match your pattern. If you continue in this way, the loop will continue and the file will grow until you run out of disk space or hit a file size limit.

Re: question about open
by toolic (Bishop) on Oct 28, 2010 at 01:12 UTC
    You are opening NEW944 for output only, but then you try to read it as input with your while loop. Maybe you are looking for:
    open NEW944, "+>>$mail_944file" or warn $!;

    See Mixing Reads and Writes

      I have done that, but still, no luck..

Re: question about open
by kcott (Archbishop) on Oct 28, 2010 at 02:17 UTC

    It looks like $mail_944file is probably empty to start with. Try adding some test code to check this; something like:

    ... while (<NEW944>) { print "Reading line $.\n"; print "Contents: $_\n}; ...

    Also take a look at -X functions - you can check for file existence, file size and so on.

    -- Ken