加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在Perl中,为什么不能将__DATA__用作可搜索的文件句柄?

发布时间:2020-12-16 06:28:27 所属栏目:大数据 来源:网络整理
导读:通过典型的slurp作品从DATA读取.尝试使用DATA作为我可以进行搜索的文件句柄是行不通的.有人能够指出我必须犯的明显错误吗? 码: #!/usr/bin/env perluse strict;use warnings;if ($ARGV[0] eq 'seek' ) { my $log_fh = *DATA; $log_fh-seek(64,0); print "
通过典型的slurp作品从DATA读取.尝试使用DATA作为我可以进行搜索的文件句柄是行不通的.有人能够指出我必须犯的明显错误吗?

码:

#!/usr/bin/env perl

use strict;
use warnings;

if ($ARGV[0] eq 'seek' ) {
    my $log_fh = *DATA;
    $log_fh->seek(64,0);
    print "n-- 64 --n",join ("",<$log_fh> );
} else {
    while (<DATA>) {
        print $_;
    }
}

exit;

__DATA__
01234567890123456789
1234567890123456789
1234567890123456789
12
X <- That X is the 64th char in
this file.
Y <- That Y is the 106th char in this file.
junk
more junk.
bye!

$perl file_from_data.pl slurp
01234567890123456789
1234567890123456789
1234567890123456789
12
X <- That X is the 64th char in
this file.
Y <- That Y is the 106th char in this file.
junk
more junk.
bye!

运行while()循环:

$perl file_from_data.pl slurp
01234567890123456789
1234567890123456789
1234567890123456789
12
X <- That X is the 64th char in
this file.
Y <- That Y is the 106th char in this file.
junk
more junk.
bye!

运行seek(),它似乎不是从DATA开始,而是脚本的开始:

$perl file_from_data.pl seek

-- 64 --
'seek' ) {
    my $log_fh = *DATA;
    $log_fh->seek(64,<$log_fh> );
} else {
    while (<DATA>) {
        print $_;
    }
}

exit;

__DATA__
01234567890123456789
1234567890123456789
1234567890123456789
12
X <- That X is the 64th char in
this file.
Y <- That Y is the 106th char in this file.
junk
more junk.
bye!

这是一个旧的Perl:

$perl -v

This is perl 5,version 16,subversion 3 (v5.16.3) built for x86_64-linux- 
thread-multi

解决方法

Running the seek(),it appears to not start at DATA but the start of the script

我认为你根本没有犯任何错误.这正是发生的事情. DATA是在源文件上打开的文件句柄.在您从该文件句柄第一次读取()之前,文件指针紧跟在文件中的__DATA__标记之后.但是您可以使用seek()将文件指针移动到文件中的任何位置.

我想要实现一个无法在其初始位置之前移回的“特殊情况”文件句柄会更难.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读