0

Can someone explain me how to understand this part of the code The code is extracted from pfLogSumm.pl a log analyzer for postfix mail

while(<>) {
    next if(defined($dateStr) && ! /^$dateStr/o);
    s/: \[ID \d+ [^\]]+\] /: /o;    # lose "[ID nnnnnn some.thing]" stuff
    my $logRmdr;

    more code

}

I can't understand what the regex is doing because don't have a assignment, don't have a conditional, simple is there

Cœur
  • 37,241
  • 25
  • 195
  • 267
DiegoR
  • 1

1 Answers1

1

By default, regular expressions (and many other functions) operate on $_. So

s/: \[ID \d+ [^\]]+\] /: /o;

is equivalent to:

$_ =~ s/: \[ID \d+ [^\]]+\] /: /o;

This replaces : [ID number ...] with just : in the input line.

This is a common idiom that you should see in many Perl scripts, you should be used to it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks Barmar!! Solved! – DiegoR Jun 23 '14 at 20:14
  • One more question please! Wath is doing the regex? replacing? extractng? and whats abuot the /o modifier? Thanks again!! – DiegoR Jun 23 '14 at 20:50
  • It's replacing something -- that's what `s///` means. The `o` modifier tells Perl to compile the RE, but it's considered obsolete now. See http://stackoverflow.com/questions/550258/does-the-o-modifier-for-perl-regular-expressions-still-provide-any-benefit – Barmar Jun 23 '14 at 22:05