-1

结束这个问题。会喝红牛。睡觉。编写代码并返回带有单元测试用例的全新问题。

更新:新文件在这里

配置文件也在这里

我再次重构了代码:

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");
    }
}

这段代码总是死在这里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.
4

4 回答 4

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'
        ];

该代码中有很多内容。这是我对您尝试做的事情的解释:

更新:鉴于您最近对模式中的正则表达式特殊字符的评论,如果您打算在模式中使用它们进行拆分,请务必引用它们。也有$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;
  }

另一个更新:

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

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'
        ];

没有错误...没有警告因此,还有其他事情正在发生,您坚持不向我们展示。

其他备注:

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

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

  3. 当您可以在语句中执行此操作时,无需在循环主体中分配$_to 。$conlinewhile

  4. 在原始代码中,您没有@columns$colData.

  5. 淡化说辞。计算机的工作原理是 GIGO。

  6. 查看您发布的链接中的代码,您似乎不知道您可以这样做:

    use File::Spec::Functions qw( catfile );
    ...
    catfile($ETLSpliter::filepath_results, $ETLSpliter::actual_name);
    

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

$ETLSpliter{filepath}

最后,你确实意识到Spliter是不正确的。伊蒂姆:Splitter

于 2009-06-23T14:13:26.513 回答
3

你确定它卡住了吗?您永远不会在 中存储任何数据@columns,因此您的代码将始终返回一个空列表。

其他注意事项:

  • 您的die电话应包括$!(操作系统错误)。open除了不存在的文件之外,还有其他可能失败的原因,并且$!会告诉您真正的问题是什么。
  • 你可能应该做一个chomp $conline来摆脱换行符。
  • 您可以做while (my $conline = <CFILE>)而不是从$_.
  • 两个参数open(尤其是隐式<模式)是不好的形式。最好使用三参数形式(最好使用词法文件句柄):open(my $fh, '<', $filename) or die...
于 2009-06-23T14:08:26.197 回答
1

里面有什么$ETLSpliter::name- 任何/字符都应该被转义。

片段中的许多其他问题已经得到解决,所以我不会去那里。

于 2009-06-23T14:27:56.467 回答
0

终于想通了!!!!!!!哇睡眠是一种很棒的力量。

反正。问题出在我的死亡消息中的 $ETLSplitter::configFile 中。

die ('Error opening '.$ETLSpliter::configFile.': '.$!);

其中有 winblows 路径分隔符“/”。所以因为我用双引号输出,perl 将路径中的 '/' 插入为模式。从这里

die "Error opening some/path/to/ ...

...  /=>/, 

这弄乱了子程序中的整个程序流程。通过这样做解决了这个问题。

die ('Error opening '.$ETLSpliter::configFile.': '.$!);
于 2009-06-23T18:49:34.293 回答