7

看起来 main 中有一个符号被称为'_<-'(不带引号),其方式与其他看起来可能是句柄的事物相同:'_</usr/perl/lib/Carp.pm'例如。

有什么方法可以使用它吗?

或者如果我希望读取输入源,我是否必须使用源过滤器?


回复暴民:我不知道调试会在哪里打开。在我转储基表后,%INC 转储显示:

$VAR1 = {
      'warnings/register.pm' => 'C:/strawberry/perl/lib/warnings/register.pm',
      'XSLoader.pm' => 'C:/strawberry/perl/lib/XSLoader.pm',
      'English.pm' => 'C:/strawberry/perl/lib/English.pm',
      'Tie/Hash/NamedCapture.pm' => 'C:/strawberry/perl/lib/Tie/Hash/NamedCapture.pm',
      'unicore/lib/Perl/_PerlIDS.pl' => 'C:/strawberry/perl/lib/unicore/lib/Perl/_PerlIDS.pl',
      'unicore/Heavy.pl' => 'C:/strawberry/perl/lib/unicore/Heavy.pl',
      'warnings.pm' => 'C:/strawberry/perl/lib/warnings.pm',
      'utf8.pm' => 'C:/strawberry/perl/lib/utf8.pm',
      'Config.pm' => 'C:/strawberry/perl/lib/Config.pm',
      'overloading.pm' => 'C:/strawberry/perl/lib/overloading.pm',
      'Symbol.pm' => 'C:/strawberry/perl/lib/Symbol.pm',
      'Carp.pm' => 'C:/strawberry/perl/lib/Carp.pm',
      'bytes.pm' => 'C:/strawberry/perl/lib/bytes.pm',
      'Exporter/Heavy.pm' => 'C:/strawberry/perl/lib/Exporter/Heavy.pm',
      'utf8_heavy.pl' => 'C:/strawberry/perl/lib/utf8_heavy.pl',
      'strict.pm' => 'C:/strawberry/perl/lib/strict.pm',
      'Exporter.pm' => 'C:/strawberry/perl/lib/Exporter.pm',
      'vars.pm' => 'C:/strawberry/perl/lib/vars.pm',
      'constant.pm' => 'C:/strawberry/perl/lib/constant.pm',
      'Errno.pm' => 'C:/strawberry/perl/lib/Errno.pm',
      'overload.pm' => 'C:/strawberry/perl/lib/overload.pm',
      'Data/Dumper.pm' => 'C:/strawberry/perl/lib/Data/Dumper.pm'
    };
4

2 回答 2

2

您在 perl 调试器中看到了这个?这很可能是这些符号表条目的来源:请参阅文件DATA STRUCTURES MAINTAINED BY CORE中 perldoc 的部分perl5db.pl

我能看到_<-在符号表中获取条目的唯一方法是仅使用-d开关启动 perl,然后将 Perl 程序输入到标准输入中,例如:

$ perl -d

Loading DB routines from perl5db.pl version 1.32
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

print "Hello world\n";
<Ctrl-D>
main::(-:1):    print "Hello world\n";
  DB<1>

从这里,@{"_<-"}(或@{$main::{"_<-"}})包含您的输入,${"_<-"}${$main::{"_<-"}}包含文件的“名称”(只是-),并且%{"_<-"}/%{$main::{"_<-"}}包含有关断点和操作的信息,用于从标准输入单步执行代码。

如果没有strict refs,您还可以使用类似的方式查看此数据

  DB<6> $name="_<-"

  DB<7> p ${$name}
-
  DB<8> p @{$name}
BEGIN { require 'perl5db.pl' };
print "Hello world\n";

  DB<9> p %{$name}

没有与_<-(或任何其他_<...符号)的符号表条目关联的文件句柄。

于 2012-08-03T22:48:23.827 回答
2

或者如果我希望读取输入源,我是否必须使用源过滤器?

如果源文件有__END__or__DATA__标记,则DATA文件句柄可用。...这本身就很无聊。有趣的是,您可以seek定位 0,这会将您带到源文件的顶部:

use Carp;

print "Just another Perl hacker,\n";

eval { 
    no warnings qw/unopened/;
    seek DATA, 0, 0 
      or croak "Script lacking __END__ or __DATA__ tag has no DATA filehandle.";
};
if( !$@ ) {
    while(<DATA>){
        print;
    }
}
else {
    carp $@;
}

__END__

该脚本将执行(打印“只是另一个 Perl 黑客”),然后通过打印自己的源代码完成。

在上面的代码中,如果eval块确实捕获了异常,则回退可能是使用 FindBin 和$0,打开源文件,然后读取它。把它们放在一起,它看起来是这样的:

BEGIN {
    use Carp;

    sub read_source {
        my $source;
        local $/ = undef;
        eval {
            no warnings qw( unopened );
            my $DATA_position = tell DATA;
            croak "'tell DATA' failed: Probably no __END__ or __DATA__ segment."
              if $DATA_position < 0;
            seek DATA, 0, 0
              or croak
              "'seek DATA' failed: Probably no __END__ or __DATA__ segment.";
            $source = <DATA>;
            seek DATA, $DATA_position, 0 or croak    # Must leave *DATA usable.
              "seek to reset DATA filehandle failed after read.";
        };
        if ($@) {
            croak $@ if $@ =~ /reset/;    # Unstable state: Shouldn't be possible.
            eval {
                require FindBin;
                no warnings 'once';
                open my $source_fh, $FindBin::Bin . '/' . $0 or croak $!;
                $source = <$source_fh>;
            };
            croak "Couldn't read source file from *DATA or \$0: $@" if $@;
        }
        return $source;
    }
};

print read_source(), "\n";

该片段首先尝试从 读取DATA,这消除了加载 FindBin 和打开新文件句柄的需要。如果失败,那么它会尝试 FindBin 方法。如果两者都失败,则会引发异常。最终成功状态将整个源文件转换为$source_code. 句柄也将DATA恢复到调用此代码段之前的相同状态。

这应该可以有力地处理如何在不使用源过滤器的情况下读取源文件的问题。

于 2012-08-04T01:21:21.290 回答