-----Original Message-----
From: Mark Galbreath [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, May 10, 2006 9:11 AM
To: dbi-users@perl.org
Subject: DBD::XBase, STDOUT, and IO issue
> 
> Hi Guys,
>  
> I'm using a subclass of DBI called DBD::XBase for reading Oralce DBF
> tablespace datafiles, translating them into delimited text files, and
> then loading them into MySQL with DBI. The problem is the only way
> DBD:XBase can output the text file with field delimiters I need is it
> slurps the whole file to STDOUT. So I did this (don't laugh):
>  
>     my $table = new XBase;
>     open STDOUT, ">data.txt";
>     $table->dump_records( "fs" => "|" );
>     close STDOUT;
>  
> This does exactly what I need, except that now STDOUT is closed for
> further output (like for print statements), and if I do not close it,
> all STDOUT goes to the data file (I told you not to laugh!).
>
> I searched all night and cannot find an example of how to do this
> correctly. Capture the table dump's STDOUT with IO::Pipe somehow? The
> documentation of IO::Pipe is pretty sparse. Any suggestion is greatly
> appreciated.

This should do it:

  use IO::Handle;
  no warnings 'once';   # perl doesn't see the 2nd ref in the string

  # temporarily replace STDOUT
  open( SAVED_STDOUT, ">&STDOUT" ) or die "can't dup stdout: $!";
  open( OUT_FILE, ">", "data.txt") or die "can't create file: $!";
  STDOUT->fdopen(fileno(OUT_FILE), "w") || die "can't fdopen: $!";

  # print data to temporary STDOUT
  $table->dump_records( "fs" => "|" );

  # restore STDOUT
  open( STDOUT, ">&SAVED_STDOUT" ) or die "can't dup saved: $!";

  # close/flush data file
  close(OUT_FILE) || die "can't close: $!";

Hth,
Philip

Reply via email to