【问题标题】:Cleanup failed with tempfile using perl使用 perl 清理临时文件失败
【发布时间】:2015-04-14 23:54:44
【问题描述】:

我需要使用“iconv”来转换windows上生成的一些文件的char编码。有时这些文件非常大并且执行失败,因为它用完了 RAM。谷歌搜索我发现了一个名为“iconv-chunks.pl”的脚本,它基本上是一个 perl 脚本,它处理文件并且工作得很好,但它会在我的 /tmp 文件夹中生成临时文件。 问题是这个脚本每天都会为许多文件自动运行,即使它有清理标志,它也会在我的 /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;

任何帮助找到问题或完成后如何删除临时文件?

问候

【问题讨论】:

    标签: perl


    【解决方案1】:

    改变

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

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

    CLEANUP 用于在退出时触发删除临时目录,而不是文件。请注意,传递undef 作为第一个参数以使用默认模板是不必要的。

    【讨论】:

    • 非常感谢。它可能看起来很简单,但我不知道如何解决它。
    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2016-11-13
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多