I found this sub in another module (TextUnicode):
# Custom file save routine to handle unicode files
sub SaveUTF {
my ( $w, $filename ) = @_;
$filename = $w->FileName unless defined $filename;
my $dir = dirname($filename);
my $perms = ( stat($dir) )[2] & 07777;
unless ( $perms & 0200 ) {
$perms = $perms | 0200;
chmod $perms, $dir
or $w->BackTrace("Can not write to directory $dir: $!\n")
and return;
}
my ( $tempfh, $tempfilename ) = tempfile( DIR => $dir );
my $status;
my $count = 0;
my $index = '1.0';
my $progress;
my $fileend = $w->index('end -1c');
my ($lines) = $fileend =~ /^(\d+)\./;
my $unicode = ::currentfileisunicode();
# No BOM please
#if ($unicode) {
# my $bom = "\x{FEFF}";
# utf8::encode($bom);
# print $tempfh $bom;
#}
while ( $w->compare( $index, '<', $fileend ) ) {
my $end = $w->index("$index lineend +1c");
my $line = $w->get( $index, $end );
$line =~ s/[\t \xA0]+$//;
#$line = ::eol_whitespace($line);
$line =~ s/\cM\cJ|\cM|\cJ/\cM\cJ/g if (OS_Win);
utf8::encode($line) if $unicode;
$w->BackTrace("Cannot write to temp file:$!\n") and return
unless print $tempfh $line;
$index = $end;
if ( ( $count++ % 1000 ) == 0 ) {
$progress =
$w->TextUndoFileProgress( Saving => $filename,
$count, $count, $lines );
}
}
$progress->withdraw if defined $progress;
close $tempfh;
if ( -e $filename ) {
chmod 0777, $filename;
unlink $filename;
}
if ( rename( $tempfilename, $filename ) ) {
#$w->ResetUndo; #serves no purpose to reset undo
$w->FileName($filename);
return 1;
} else {
my $msg = "Cannot save $filename:$!. Text is in the temporary
+file $tempfilename.";
$w->messageBox(
-icon => 'warning',
-title => 'Warning',
-type => 'OK',
-message => $msg.(OS_Win?" (Are you using Windows Explorer
+ Preview?)":""),
);
$w->BackTrace($msg);
return 0;
}
}
Regards, Wayne