我可以得到一个源 - 源?

Axe*_*man 7 perl perl-io

看起来main中的符号'_<-'(没有引号)与其他看起来像是可以处理的东西相同:'_</usr/perl/lib/Carp.pm'例如.

有没有办法使用它?

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


回复mob:我不知道Debug会在哪里打开.转出基表后,%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'
    };
Run Code Online (Sandbox Code Playgroud)

Dav*_*idO 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__
Run Code Online (Sandbox Code Playgroud)

该脚本将执行(打印“Just another Perl hacker”),然后将通过打印其自己的源代码来完成。

在上面的代码中,如果该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";
Run Code Online (Sandbox Code Playgroud)

此代码片段首先尝试从 读取DATA,这样就无需加载 FindBin 并打开新的文件句柄。如果失败,则会尝试 FindBin 方法。如果两者都失败,则会抛出异常。最终的成功状态会将整个源文件转换为$source_code. 句柄DATA也将恢复到调用此代码片段之前的相同状态。

这应该可以稳健地处理如何读取源文件而不诉诸源过滤器的问题。