Perl   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了perl – 在程序中使用__DATA__大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
它一直用在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 ( $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,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,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

解决方法

您无需打开DATA文件句柄,只需从中读取即可.
while (my $line = <DATA>) {
    ...
}

大佬总结

以上是大佬教程为你收集整理的perl – 在程序中使用__DATA__全部内容,希望文章能够帮你解决perl – 在程序中使用__DATA__所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。