Nó được sử dụng trên Stack Overflow tất cả các thời gian nhưng tôi thực sự không hiểu nó, cũng không phải tôi có thể làm cho nó hoạt động. Tuy nhiên nó có vẻ giống như một công cụ kiểm tra thực sự tốt.Sử dụng __DATA__ trong một chương trình
Làm cách nào để tập lệnh đọc trong mọi thứ bên dưới __DATA__
vào bộ xử lý tệp? Tôi đã thử một vài cách để đọc nó thay vì tacking trên một tập tin bên ngoài. Dữ liệu là hợp pháp, đó là từ tệp AutoSys JIL cho các định nghĩa công việc.
#!/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 ($line =~ /\/\* -{17} \w+ -{17} \*\//) {
$count = 1;
}
elsif ($line =~ /(alarm_if_fail:)/) {
$count = 0;
}
elsif ($count) {
if ($line =~ m/insert_job: (\w+).*job_type: CMD/) {
push(@line_stat, $1);
}
elsif ($line =~ m/command:(.*)/) {
push(@line_stat, $1);
}
elsif ($line =~ m/machine:(.*)/) {
push(@line_stat, $1);
}
}
}
foreach my $line_wot (@line_stat) {
print "$line_wot\n";
}
__DATA__
/* ----------------- COME_AND_PLAY_WITH_US_DANNY ----------------- */
insert_job: COME_AND_PLAY_WITH_US_DANNY job_type: CMD
command: /bin/bash -pwd
machine: capser.com
owner: twins
permission: foo,foo
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "04:00"
description: "Forever, and ever and ever"
std_in_file: "/home/room217"
std_out_file: "${CASPERSYSLOG}/room217.out"
std_err_file: "${CASPERSYSLOG}/room217.err
alarm_if_fail: 1
profile: "/autosys_profile"
timezone: US/Eastern
/* ----------------- COME_AND_PLAY_WITH_US_AGAIN_DANNY ----------------- */
insert_job: COME_AND_PLAY_WITH_US_AGAIN_DANNY job_type: CMD
command: /bin/bash -ls
machine: capser1.com
owner: twins
permission: foo,foo
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "04:00"
description: "Forever, and ever and ever"
std_in_file: "/home/room217"
std_out_file: "${CASPERSYSLOG}/room217.out"
std_err_file: "${CASPERSYSLOG}/room217.err
alarm_if_fail: 1
profile: "/autosys_profile"
timezone: US/Eastern
/* ----------------- NEVER_PLAY_WITH_US_AGAIN_DANNY ----------------- */
insert_job: NEVER_PLAY_WITH_US_AGAIN_DANNY job_type: CMD
command: /bin/bash -rm *
machine: capser2.com
owner: twins
permission: foo,foo
date_conditions: 1
days_of_week: mo,tu,we,th,fr
start_times: "04:00"
description: "Forever, and ever and ever"
std_in_file: "/home/room217"
std_out_file: "${CASPERSYSLOG}/room217.out"
std_err_file: "${CASPERSYSLOG}/room217.err
alarm_if_fail: 1
profile: "/autosys_profile"
timezone: US/Eastern
'$ tôi fh = \ * DỮ LIỆU;' thay vì 'mở $ fh của tôi ...' –
Đối rõ ràng tôi thích một lối thoát rõ ràng() ngay trước __DATA__. – ulix
'DATA' _is_ một tập tin, giống như' STDIN'; nó đọc dữ liệu theo chữ '__DATA__' đặc biệt. Xem [\ _ \ _ DATA \ _ \ _] (http://perldoc.perl.org/functions/__DATA__.html) (có liên kết đến 'perldata') và, ví dụ, [bài đăng này] (https: // stackoverflow.com/questions/13463509/the-data-syntax-in-perl). – zdim