开发者

@INC hook unknown fatal error

Hey I'm writing a program that uses an @INC hook to decrypt the real perl source from blowfish. I'm having a quite annoying problem that doesn't show up using warnings or any of my standard tricks... Basically when I get to creating the new cipher object the loop skips to the next object in @INC without an error or anything.... I dont know what to do!

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

sub load_crypt {
    my ($self, $filename) = @_开发者_JS百科;
    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';

Also if I put the actual key in the initializing method it still fails


You've got a bunch of problems here. First of all, you're using File::Spec wrong. Second, you're returning a filehandle that's already at end of file, and a string that isn't a valid return value. (Also, I'd put the key prompt outside of the hook.)

#!/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';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜