簡體   English   中英

使用Perl的tempfile清理失敗

[英]Cleanup failed with tempfile using perl

我需要使用“ iconv”來從Windows上生成的某些文件轉換char編碼。 有時這些文件很大,執行失敗,因為它用完了RAM。 在谷歌搜索中,我找到了一個名為“ iconv-chunks.pl”的腳本,該腳本基本上是一個Perl腳本,可以處理文件並運行良好,但是會在我的/ tmp文件夾中生成臨時文件。 問題在於該腳本每天都會自動運行許多文件,即使清除標志為ON,它也會在我的/ tmp目錄中不斷生成垃圾。

我正在談論的腳本是: https : //code.google.com/p/clschool-team4/source/browse/trunk/iconv-chunks.pl?r=53

#!/usr/bin/perl

our $CHUNK_SIZE = 1024 * 1024 * 100; # 100M

=head1 NAME

iconv-chunks - Process huge files with iconv

=head1 SYNOPSIS

  iconv-chunks <filename> [iconv-options]

=head1 DESCRIPTION

The standard iconv program reads the entire input file into
memory, which doesn't work for large files (such as database exports).

This script is just a wrapper that processes the input file
in manageable chunks and writes it to standard output.

The first argument is the input filename (use - to specify standard input).
Anything else is passed through to iconv.

The real iconv needs to be somewhere in your PATH.

=head1 EXAMPLES

  # Convert latin1 to utf-8:
  ./iconv-chunks database.txt -f latin1 -t utf-8 > out.txt

  # Input filename of - means standard input:
  ./iconv-chunks - -f iso8859-1 -t utf8 < database.txt > out.txt

  # More complex example, using compressed input/output to minimize disk use:
  zcat database.txt.gz | ./iconv-chunks - -f iso8859-1 -t utf8 | \
  gzip - > database-utf.dump.gz

=head1 AUTHOR

Maurice Aubrey <maurice.aubrey+iconv@gmail.com>

=cut

# $Id: iconv-chunks 6 2007-08-20 21:14:55Z mla $

use strict;
use warnings;
use bytes;
use File::Temp qw/ tempfile /;

# iconv errors:
#   iconv: unable to allocate buffer for input: Cannot allocate memory
#   iconv: cannot open input file `database.txt': File too large

@ARGV >= 1 or die "Usage: $0 <inputfile> [iconv-options]\n";
my @options = splice @ARGV, 1;

my($oh, $tmp) = tempfile(undef, CLEANUP => 1);
# warn "Tempfile: $tmp\n";

my $iconv = "iconv @options $tmp";
sub iconv { system($iconv) == 0 or die "command '$iconv' failed: $!" }

my $size = 0;
# must read by line to ensure we don't split multi-byte character
while (<>) {
  $size += length $_;
  print $oh $_;
  if ($size >= $CHUNK_SIZE) {
    iconv;
    truncate $oh, 0 or die "truncate '$tmp' failed: $!";
    seek $oh, 0, 0 or die "seek on '$tmp' failed: $!";
    $size = 0;
  }
}
iconv if $size > 0;

找到問題的任何幫助,或者完成后如何刪除臨時文件?

問候

更改

my($oh, $tmp) = tempfile(undef, CLEANUP => 1);

my($oh, $tmp) = tempfile(UNLINK => 1);

CLEANUP用於在退出時觸發刪除臨時目錄 ,而不是文件。 注意,不必使用undef作為第一個參數來使用默認模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM