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

What am I doing wrong here? $buffer is not getting printed to the dumpfile. Is it a syntax error?
my $current_date = strftime("%Y-%m-%d", localtime); my $current_time = strftime("%H:%M:%S", localtime); my $dumpfile = '>/backups/table_dumps/' . $db . '.' . $current_date . '.' . $current_time; my $valid_table_list = join ' ', @valid_tables; my $cmd = 'mysqldump -q --single-transaction --complete-insert -e' . ' ' . $db . ' ' . $valid_table_list . ' |'; open DUMPFILE, $dumpfile or die $!, open MYSQLDUMP, $cmd or die $!; while (my $bytes_read = read(MYSQLDUMP, my $buffer, 4096)) { print DUMPFILE $buffer; } close DUMPFILE; debug(qq{Finished dumping $db\n\n});

Replies are listed 'Best First'.
Re: Reading command output buffer
by jasonk (Parson) on May 09, 2008 at 19:07 UTC

    Well, you don't seem to have anything in $db (and aren't using strict or you would have seen that). But other than that you seem to be just going about it the hard way...

    my $datetime = strftime( "%F.%T", localtime ); system( join( ' ', 'mysqldump -qce --single-transaction', $db, @valid_tables, '>', "> /backups/table_dumps/$db.$datetime", ) );

    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!
Re: Reading command output buffer
by starbolin (Hermit) on May 10, 2008 at 02:21 UTC

    Yes it's a syntax error. In your open() statement $cmd is interpolated into a flat list before being sent to open(). So you opened a file "mysqldump" instead of a pipe. To fix this put parenthesis around $cmd.

    open MYSQLDUMP, "$cmd" or die $!;

    This use of open() has poor error handling as it forks a shell to run the command. If there is an error executing $cmd, it will be reported by the shell to STDERR which your script does not capture. System() is a better choice here.

    Update: Open() will only fork a shell if it needs to process shell meta-characters. So, open probably ok here.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
      Thanks for the post. Problem solved. This was a weird one. It would work sometimes. Sometimes not.
Re: Reading command output buffer
by apl (Monsignor) on May 09, 2008 at 19:21 UTC
    Are you certain there's at least 4096 bytes in the SQL dump? The read would return an undef if that's not the case. Then the while block wouldn't get executed.

    Revised: As per Fletch I had it wrong.

    I'd still test the error return, though...

      Erm, no. read will try to read up to 4096 and will return the number actually read (0 on eof, or undef if there's an error like trying to read when there's no data on a non-blocking handle).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.