简体   繁体   中英

Parsing a text file perl

A program, that is part of a pipeline, is parsing an output text file but raises an error. Here is the code

1 open(PRED, '<', $predfile);
2 my $pred_text;
3 { local $/; $pred_text = <PRED>; }
4 close PRED;

5 my @pred_blocks = split(/^\-+$/, $pred_text);

6  foreach my $pred_block (@pred_blocks) {
7     $pred_block =~ /^>(.+)$/m or die("Internal error while parsing $predfile");
   .....
}  

What should contain $pred_text after line 3? I suppose the whole text file. I also suppose I should obtain different blocks from the text file, but the list in line 5 is containing only one block representing the whole file. What is the regex in line 5 doing? How it is splitting the text? And in line 7 what $pred_block =~ /^>(.+)$/m exactly mean?

Any suggestion?

Many thanks

Nino

  • Line 3: reads the whole file, see Perl Idioms Explained
  • Line 5: Doesn't split the text into blocks. Change it to my @pred_blocks = split(/^-+$/m, $pred_text); , see Modifiers
  • Line 7: checks, if the current block includes lines starting with > . If it doesn't, it aborts the script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM