我该怎么用__DATA__两次?
#!/usr/local/bin/perl
use warnings;
use 5.012;
while ( <DATA> ) {
print;
}
while ( <DATA> ) {
chomp if $. == 1;
print scalar reverse;
print "\n" if eof;
}
Run Code Online (Sandbox Code Playgroud)
__DATA__
one
two
three
four
five
six
Run Code Online (Sandbox Code Playgroud) 它一直用在Stack Overflow上,但我真的不理解它,也不能让它工作.然而,它似乎是一个非常好的测试工具.
如何让脚本将以下所有内容读__DATA__入文件句柄?我尝试了几种方法来阅读它,而不是在外部文件上添加.数据是合法的,它来自AutoSys JIL文件中的作业定义.
#!/efs/dist/perl5/core/5.10/exec/bin/perl
use strict;
use warnings;
my ( $job, $machine, $command, @line_stat );
#these 4 lines below do not read in data to filehandle
#my $data = do {
# local $/;
# <DATA>;
#} ;
my $data = <DATA>; # does not work either
open( my $fh, '<:encoding(UTF-8)', $data )
or die "Could not open file '$data' $!";
my $count = 0;
while ( my $line = <$fh> ) {
#chomp $line;
if ( …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写持久/缓存脚本.代码看起来像这样:
...
Memoize('process_fille');
print process_file($ARGV[0]);
...
sub process_file{
my $filename = shift;
my ($a, $b, $c) = extract_values_from_file($filename);
if (exists $my_hash{$a}{$b}{$c}){
return $my_hash{$a}{$b}{$c};
}
return $default;
}
Run Code Online (Sandbox Code Playgroud)
这将从循环中的shell脚本调用,如下所示
value=`perl my_script.pl`;
Run Code Online (Sandbox Code Playgroud)
有没有办法可以调用这个脚本,以保持其状态.从打电话到打电话.让我们假设初始化'%my_hash'和调用extract_values_from_file都是一项昂贵的操作.
谢谢