in reply to Match into list?

It does indeed work. Somehow you tried something different in the debugger.

A regex match returns differently in list vs. scalar context, and with or without /g. See http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators-operator%2c-regexp: "If the /g option is not used, m// in list context returns"...

Replies are listed 'Best First'.
Re^2: Match into list?
by cormanaz (Deacon) on Sep 04, 2007 at 00:08 UTC
    Taking the debugger out of it, when I run
    #!/usr/bin/perl -w use strict; my $line = '76.172.202.159 - - [31/Aug/2007:15:58:15 -0600] "GET / HTT +P/1.1" 200 29692 "http://www.paperbackswap.com/forum/view_topic.php?t +=70235&ls=" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8 +.0.12) Gecko/20070508 Firefox/1.5.0.12"'; my ($host,$date,$url_with_method,$status,$size,$referrer,$agent) = $li +ne =~ m/^(\S+) - - \[(\S+ \+\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) " +(.*?)" "([^"]+)"$/; print "$host\n$date\n$url_with_method\n$status\n$size\n$referrer\n$age +nt\n";
    The output is
    Use of uninitialized value in concatenation (.) or string at test.pl l +ine 6. Use of uninitialized value in concatenation (.) or string at test.pl l +ine 6. Use of uninitialized value in concatenation (.) or string at test.pl l +ine 6. Use of uninitialized value in concatenation (.) or string at test.pl l +ine 6. Use of uninitialized value in concatenation (.) or string at test.pl l +ine 6. Use of uninitialized value in concatenation (.) or string at test.pl l +ine 6. Use of uninitialized value in concatenation (.) or string at test.pl l +ine 6.
      Your regex only works with positive tz offsets, not negative :)

      Remember to always check if your match succeeds before trying to use the results; in list context, this looks like:

      #!/usr/bin/perl -w use strict; my $line = '76.172.202.159 - - [31/Aug/2007:15:58:15 -0600] "GET / HTT +P/1.1" 200 29692 "http://www.paperbackswap.com/forum/view_topic.php?t +=70235&ls=" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8 +.0.12) Gecko/20070508 Firefox/1.5.0.12"'; my ($host,$date,$url_with_method,$status,$size,$referrer,$agent) = $li +ne =~ m/^(\S+) - - \[(\S+ [-+]\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) + "(.*?)" "([^"]+)"$/ or warn "match failed!"; print "$host\n$date\n$url_with_method\n$status\n$size\n$referrer\n$age +nt\n";
      or:
      #!/usr/bin/perl -w use strict; my $line = '76.172.202.159 - - [31/Aug/2007:15:58:15 -0600] "GET / HTT +P/1.1" 200 29692 "http://www.paperbackswap.com/forum/view_topic.php?t +=70235&ls=" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8 +.0.12) Gecko/20070508 Firefox/1.5.0.12"'; if ( my ($host,$date,$url_with_method,$status,$size,$referrer,$agent) += $line =~ m/^(\S+) - - \[(\S+ [-+]\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) + "(.*?)" "([^"]+)"$/ ) { print "$host\n$date\n$url_with_method\n$status\n$size\n$referrer\n +$agent\n"; } else { warn "match failed!"; }
      You might take a look at the Regexp::Log module.