Perl循环卡住阅读文件?

kth*_*ore -1 perl split readline while-loop

结束这个问题.会喝红牛.睡觉.代码和带有单元测试案例的品牌打击新问题.

更新:新文件在这里

配置文件也在这里

我再次重构了代码:

sub getColumns {
    open my $input, '<', $ETLSplitter::configFile
        or die "Error opening '$ETLSpliter::configFile': $!";

    my $cols;
    while( my $conline = <$input> ) {
        chomp $conline;
        my @values = split (/=>/, $conline);
        if ($ETLSplitter::name =~ $values[0] ) {
            $cols = $values[1];
            last;
        }
    }

    if($cols) {
        @ETLSplitter::columns = split (':', $cols);
    }
    else {
        die("$ETLSplitter::name is not specified in the config file");
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码总是死在这里die("$ETLSplitter::name is not specified in the config file");.

另一个线索是,如果我split (':', $cols);改为split (/:/, $cols);我得到这个错误.

 perl -wle "
 use modules::ETLSplitter;
 \$test = ETLSplitter->new('cpr_operator_metric_actual_d2', 'frame/');
 \$test->prepareCSV();"
 syntax error at modules/ETLSplitter.pm line 154, near "}continue"
 Compilation failed in require at -e line 2.
 BEGIN failed--compilation aborted at -e line 2.
Run Code Online (Sandbox Code Playgroud)

Sin*_*nür 6

本问题的最终发布:根据您的最新更新,我相信以下代码说明了使用/:/第一个参数时没有问题split.它还指出,当使用函数的参数而不是依赖于全局变量时,读取代码会更容易:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

for my $varname ( qw( adntopr.cpr.smtref.actv cpr_operator_detail )) {
    print $varname, "\n";
    print Dumper get_columns(\*DATA, $varname);
}

sub get_columns {
    my ($input_fh, $varname) = @_;

    while ( my $line = <$input_fh> ) {
        chomp $line;
        my @values = split /=>/, $line;
        next unless $varname eq $values[0];
        return [ split /:/, $values[1] ];
    }
    return;
}

__DATA__
adntopr.cpr.smtref.actv=>3:8:18:29:34:38:46:51:53:149
adntopr.smtsale2=>3:8:16:22:27:37:39:47:52:57:62:82:102:120:138:234:239:244:249:250:259:262:277:282:287:289:304:319:327:331:335:339:340:341:342:353:364:375:386:397:408
cpr_operator_detail=>3:11:18:28:124:220:228:324
cpr_operator_org_unit_map=>7:12
cpr_operator_metric_actual=>8:15:25:33:38:40:51

C:\Temp> tjm
adntopr.cpr.smtref.actv
$VAR1 = [
          '3',
          '8',
          '18',
          '29',
          '34',
          '38',
          '46',
          '51',
          '53',
          '149'
        ];
cpr_operator_detail
$VAR1 = [
          '3',
          '11',
          '18',
          '28',
          '124',
          '220',
          '228',
          '324'
        ];
Run Code Online (Sandbox Code Playgroud)

该代码中有很多错误.以下是我对你要做的事情的解释:

更新:鉴于您最近关于模式中正则表达式特殊字符的注释,如果您要在模式中使用它们进行拆分,请务必引用它们.还有$ETLSpliter::name可能包含其他特殊字符.我修改了代码来处理这种可能性.

sub getColumns {
    open my $input, '<', $ETLSpliter::configFile
          or die "Error opening '$ETLSpliter::configFile': $!");
      my @columns;
      while( my $conline = <$input> ) {
          my @values = split /=>/, $conline;
          print "not at: ".$conline;
          push @columns, $values[1] if $values[0] =~ /\Q$ETLSpliter::name/;
      }
      return @columns;
  }
Run Code Online (Sandbox Code Playgroud)

另一个更新:

因此,该模式确实/=>/基于您在下面的评论.然后:

my $conline = q{cpr_operator_detail=>3:11:18:28:124:220:228:324};
my @values = split /=>/, $conline;

use Data::Dumper;
print Dumper \@values;
__END__

C:\Temp> tml
$VAR1 = [
          'cpr_operator_detail',
          '3:11:18:28:124:220:228:324'
        ];
Run Code Online (Sandbox Code Playgroud)

没有错误 ...... 没有警告因此,还有其他一些事情你坚持不向我们展示.

其他备注:

  1. 使用词法文件句柄,让perl告诉你它可能遇到的错误而不是假设.

  2. 在最小适用范围内声明变量.

  3. 无需分配$_$conline在循环的身体的时候,你可以做的是,在while声明.

  4. 在原始代码中,您没有放入任何内容@columns或执行任何有用的操作$colData.

  5. 淡化了言辞.计算机遵循GIGO的原则.

  6. 查看您发布的链接上的代码,看起来您不知道可以执行以下操作:

    use File::Spec::Functions qw( catfile );
    ...
    catfile($ETLSpliter::filepath_results, $ETLSpliter::actual_name);
    
    Run Code Online (Sandbox Code Playgroud)

此外,看起来您正在使用哈希将完成工作的包:

$ETLSpliter{filepath}
Run Code Online (Sandbox Code Playgroud)

最后,你意识到Spliter是不正确的.ITYM : Splitter.