in reply to Re: Match into list?
in thread Match into list?

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.

Replies are listed 'Best First'.
Re^3: Match into list?
by ysth (Canon) on Sep 04, 2007 at 00:37 UTC
    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.