2011-10-10 65 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';