2011-10-10 32 views
3

嘿,我正在编写一个程序,它使用@INC钩子来解密真正的perl源码。我有一个很烦人的问题,没有显示使用警告或任何我的标准技巧......基本上,当我开始创建新的密码对象时,循环跳到@INC中的下一个对象,没有错误或任何错误.... 我不知道该怎么办!@INC hook未知致命错误

#!/usr/bin/perl -w 
use strict; 
use Crypt::CBC; 
use File::Spec; 

sub load_crypt { 
    my ($self, $filename) = @_; 
    print "Key?\n: "; 
    chomp(my $key = <STDIN>); 
    for my $prefix (@INC) { 
      my $buffer = undef; 
      my $cipher = Crypt::CBC->new(-key => $key, -cipher => 'Blowfish'); 
      my $derp = undef; 
      $cipher ->start('decrypting'); 
      open my $fh, '<', File::Spec->($prefix, "$filename.nc") or next; 
      while (read($fh,$buffer,1024)) { 
        $derp .= $cipher->crypt($buffer); 
      } 
      $derp .= $cipher->finish; 
      return ($fh, $derp); 
    } 
} 

BEGIN { 
    unshift @INC, \&load_crypt; 
} 
require 'gold.pl'; 

另外,如果我把实际的密钥在初始化方法仍然失败

+0

你认为给'@ INC添加函数引用是否会完成? – TLP

+0

@TLP,阅读[require](http://perldoc.perl.org/functions/require.html)文档的末尾。它可以让你自定义模块源代码的加载方式。 (但在这种情况下,他做错了。) – cjm

回答

4

你有一大堆的问题在这里。首先,你正在使用File :: Spec错误。其次,你要返回一个已经在文件末尾的文件句柄,以及一个不是有效返回值的字符串。 (另外,我会把关键提示放在钩子外面。)

#!/usr/bin/perl -w 
use strict; 
use Crypt::CBC; 
use File::Spec; 

# Only read the key once: 
print "Key?\n: "; 
chomp(my $key = <STDIN>); 

sub load_crypt { 
    my ($self, $filename) = @_; 
    return unless $filename =~ /\.pl$/; 
    for my $prefix (@INC) { 
    next if ref $prefix; 
    #no autodie 'open'; # VERY IMPORTANT if you use autodie! 
    open(my $fh, '<:raw', File::Spec->catfile($prefix, "$filename.nc")) 
     or next; 
    my $buffer; 
    my $cipher = Crypt::CBC->new(-key => $key, -cipher => 'Blowfish'); 
    my $derp; 
    $cipher->start('decrypting'); 
    while (read($fh,$buffer,1024)) { 
     $derp .= $cipher->crypt($buffer); 
    } 
    $derp .= $cipher->finish; 
    # Subroutine writes 1 line of code into $_ and returns 1 (false at EOF): 
    return sub { $derp =~ /\G(.*\n?)/g and ($_ = $1, 1) }; 
    } 
    return; # Didn't find the file; try next @INC entry 
} # end load_crypt 

# This doesn't need a BEGIN block, because we're only using the hook 
# with require, and that's a runtime operation: 
unshift @INC, \&load_crypt; 
require 'gold.pl'; 
相关问题