English 中文(简体)
我为什么要把“坏档案描述”的封闭档案中印有“印有”的字面放在我的手稿中?
原标题:Why I am getting "print() on closed filehandle with bad file descriptor" in my Perl script?

我在Perl拥有一台TCP服务器(Revision 5.0 edition 8 subversion 0),该服务器利用这一代号在档案中登录。

sub logger {
  return if ($LOGFILE eq "") ;
  my ($idt, $str) = @_ ;
  unless( defined($str) ) {
    $str = $idt ;
    $idt =    ;
  }
  my ($s,$m,$h,$J,$M,$A) = localtime(time()) ;
  if(!open(OUT,">>$LOGFILE")) {
    warn "logger:error open [$LOGFILE]:[$!]
";
    return;
  }
  if (!printf OUT "%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d %s %s -> %s
",
          $A+1900, $M+1, $J, $h, $m, $s, $idt, $HOSTNAME, $str) {
    warn "logger:error print [$LOGFILE]:[$!]
";
    return;
  }
  if (!close(OUT)) {
    warn "logger:error close [$LOGFILE]:[$!]
";
    return;
  }
}

So there is simultaneous writing in this log file. Exceptionally , I have this error in STDERR:

printf() on closed filehandle

logger :error print [/my/path/logFile.LOG]:[Bad File Descriptor]

without!

How can I resolve this problem?

最佳回答

。 由于将升级改造为真正的伐木系统,如Log4perl,这一问题已经解决,因此,你可以使用一个称为$OUT的弹性文件:

sub logger {
    $LOGFILE or return;
    my ($idt, $str) = @_ ;

    unless( defined($str) ) {
        $str = $idt ;
        $idt =    ;
    }

    my ($s,$m,$h,$J,$M,$A) = localtime(time()) ;

    my $OUT;

    unless (open $OUT,  >> , $LOGFILE) {
        warn "logger:error open [$LOGFILE]:[$!]
";
        return;
    }

    unless (printf $OUT 
        "%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d %s %s -> %s
",
        $A+1900, $M+1, $J, $h, $m, $s, $idt, $HOSTNAME, $str
    ) {
        warn "logger:error print [$LOGFILE]:[$!]
";
        return;
    }

    unless (close $OUT) {
        warn "logger:error close [$LOGFILE]:[$!]
";
        return;
    }

    return 1;
}
问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签