WoodyWeaver has asked for the wisdom of the Perl Monks concerning the following question:

I'm parsing time from a last output. I got an unexpected input, so getting an unexpected output shouldn't be surprising, but for the life of me I can't figure out what perl is doing.

subroutine is

sub minutes { my $time = shift; print "minutes handed $time " if $DEBUG_MINUTES; if ( $time =~ /(\d+):(\d+)/ ) { print "Interpreted as $1 hours $2 mins\n" if $DEBUG_MINUTES; return 60 * $1 + $2; } elsif (/(\d+)+(\d+):(\d+)/) { print "Interpreted as $1 days, $2 hours, $3 minutes\n" if $DEBUG_M +INUTES; return ( 24 * $1 + $2 ) * 60 + $3; } else { warn "I don't understand time $time\n"; return $time; } }
I was processing a line
archerh Thu Jun 22 10:25 - down (-6+-1 +2:-52)
and I get
minutes handed -6+-12:-52 Interpreted as 1 days, 0 hours, 25 minutes
How does \d+ do this?

Replies are listed 'Best First'.
Re: unexpected RE match
by Anonymous Monk on Jul 05, 2017 at 23:01 UTC

    elsif (/(\d+)+(\d+):(\d+)/) matches against $_ because you forgot to say elsif ( $time =~ /(\d+)+(\d+):(\d+)/ ), and $_ is probably the whole input line, and

    use Data::Dumper; print Dumper( "Thu Jun 22 10:25" =~ /(\d+)+(\d+):(\d+)/ ); __END__ $VAR1 = '1'; $VAR2 = '0'; $VAR3 = '25';
      many thanks. we always see what we expect, not what is there.
        Also note that your regex isn't going to match those minus signs. -G
        PerlMonks iz awe-some. I love the vibe.