Beautiful! The following code is now in place and, having tested it with both Windows and Mac, it is working exactly as desired. Thank you so much for taking time to help with this. I have learned a few things.
None of the fields should ever contain tabs, so that should be no issue. I always use strict, but only use warnings while troubleshooting. It's presently commented out to save space in the log files (when working with UTF8, wide character warnings seem to appear for no reason at times, when everything is working properly, and sometimes I get the uninitialized errors that are also extraneous--and the logs simply aren't monitored often enough to make filling them with such errors useful). For what it's worth, when the script with this subroutine is run, the script is adding a "wide character" error to the error log even with warnings turned off, despite the fact that everything is working beautifully, and the zip file that is created contains perfect text in an Asian language. The error comes from having Asian text in one of the HTML headings for the page, even though the "use utf8;" pragma is in place. To avoid it I'd probably need to convert that text to HTML-entities which makes it totally unreadable in my code, and ugly. I'd rather get useless warnings.
sub exportdatabase {
#INCOMING GLOBAL VARS: $statement, $db_export_file,
# $cur_date, $cur_time, $OS, $table, $quest
fork: {
my ($recnum,$revnum,$book,$chap,$verse,$text) = '';
my @resp = ();
my $encoding = "UTF-8";
my $zipdata = '';
my $CRLF = "\n";
my $zipfile = "$db_export_file.zip";
my $q = CGI::Simple->new();
my $z = IO::Compress::Zip->new(\$zipdata,
Name => "$db_export_file" )
or die "zip failed: $ZipError\n";
if ($OS eq "Windows") {
$CRLF = "\r\n";
}
$statement = qq| SELECT a.RecordNum, a.RevisionNum, a.Book, a.Chap
+ter, a.Verse, a.Text from $table a INNER JOIN (SELECT RecordNum, max(
+RevisionNum) RevisionNum FROM $table GROUP BY RecordNum) b USING (Rec
+ordNum,RevisionNum); |;
&connectdb('exportdatabase');
$z->print( encode( $encoding, "RECORD#\tREVISION#\tBOOK#\tCHAP#\tV
+ERSE#\tTEXT, AS EDITED BY: $curdate $curtime (Pacific Time), EXPORTED
+ IN $OS FORMAT$CRLF", Encode::FB_CROAK|Encode::LEAVE_SRC ) );
while ( ( $recnum, $revnum, $book, $chap, $verse, $text ) = $quest
+->fetchrow_array() ) {
$z->print( encode( $encoding, "$recnum\t$revnum\t$book\t$chap\
+t$verse\t$text$CRLF", Encode::FB_CROAK|Encode::LEAVE_SRC ) );
}
$z->close();
print $q->header( -type => 'application/zip',
-Content_Disposition => qq{attachment; filename="$zipfile"},
-Content_Length => length($zipdata) );
binmode STDOUT; # just to play it safe
print $zipdata;
} # END fork
} # END SUB exportdatabase