http://www.perlmonks.org?node_id=667871


in reply to appending data to a file

Here's the code as I've deciphered it:

open (writefile,">>",$target_full_path) || die "Can't open $target_ful +l_path"; while($fetchdata=$sth4->fetchrow_array()) { print $fetchdata; #it is working print writefile $fetchdata; # not appending $rowcount++; #here it is counting } close writefile; print $rowcount;

When you open, it doesn't die? Maybe try printing $target_full_path to make sure it has what you think it does. Your argument to die should include $! in it somewhere since that will have the error that caused open to fail.

I'm sorry, I don't see why this wouldn't work. What was the slight modification that causes it to break?

Replies are listed 'Best First'.
Re^2: appending data to a file
by papidave (Pilgrim) on Feb 14, 2008 at 14:52 UTC
    Are you really trying to capture the return value of $sth->fetchrow_array() in a scalar? Your comments imply that you're seeing data on STDOUT from the first print $fetchdata;, but I don't see how it could be the data you wanted. The DBI docs suggest:

    while ( @row = $sth->fetchrow_array ) { print "@row\n"; }

    That said, it seems to me that you'd likely run into trouble if the return value were ever an empty string ("") or something else that evaluates to FALSE.

Re^2: appending data to a file
by bipinbalan (Initiate) on Feb 14, 2008 at 07:50 UTC
    Hi,
      I think this code will work. Check it..
    open (writefile,">>$target_full_path") || die "Can't open $target_full +_path"; while($fetchdata=$sth4->fetchrow_array()) { print $fetchdata; print writefile $fetchdata; $rowcount++; #here it is counting } close writefile; print $rowcount;
    Cheers !
      Bipin Balan