“错误的文件描述符”“消化”

问题描述:

我想实现的Perl消化的CRC,但不幸的是我得到:“错误的文件描述符”“消化”

精华读取失败:错误的文件描述符

我该如何解决这个问题?

这是模块的代码示例here

sub crc3439() { 
    $ctx = Digest::CRC->new(type=>"crc16"); 
    $ctx = Digest::CRC->new(width=>16, init=>0x2345, xorout=>0x0000, 
          refout=>1, poly=>0x8005, refin=>1, cont=>1); 

    my $binfile = 'envbin.bin'; 
    open(fhbin, '>', $binfile) or die "Could not open bin file '$binfile' $!"; 
    binmode(fhbin); 

    $ctx->add($binfile); 
    $ctx->addfile(*binfile); 
    $digest = $ctx->hexdigest; 
    return $digest; 
} 
+0

看起来像你想一)覆盖'$ binfile',而不是读它,并且b)使用'* binfile'的文件句柄,而不是'* fhbin'。 – 2014-09-02 09:11:56

+0

@JimDavis试过$ ctx-> addfile(* fhbin); 。它会抛出相同的错误 – Asthme 2014-09-02 09:17:42

首先,你覆盖​​,而不是读它。将打开模式更改为'<'应该可以解决该问题。

您的->addfile正在添加一个不存在的文件句柄;您可能需要*fhbin,或者使用词法(my $fhbin)文件句柄。

另外,您还会使用额外的->new调用覆盖$ctx

sub crc3439 { 
    my $binfile = shift; 

    my $ctx = Digest::CRC->new(
     type => "crc16", 
     width => 16, 
     init => 0x2345, 
     xorout => 0x0000, 
     refout => 1, 
     poly => 0x8005, 
     refin => 1, 
     cont => 1, 
    ); 

    open(my $fhbin, '<', $binfile) or die "Could not open bin file '$binfile' $!"; 
    binmode($fhbin); 

    $ctx->add($binfile); 
    $ctx->addfile($fhbin); 

    return $ctx->hexdigest; 
} 


print crc3439('foo.bin'); 
+0

@thank u但它的十六进制值有点像000007 – Asthme 2014-09-02 09:36:35

+0

你期待什么? – 2014-09-02 09:38:01

+0

确切值crc 32值.... – Asthme 2014-09-02 09:39:10