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

Will someone tell me why this:

#!/usr/bin/perl -w use strict; use Benchmark; my $log = "205.188.209.11 - - \[02/Jun/2000:00:30:00 -0400\] \"GET /lm +es/elements/eshome-5-y12-b.jpg HTTP/1.0\ " 200 2072"; sub regexp { my ($source,$timestamp,$method,$uri,$status,$bytes) = /^(\S+) \S+ \S+ \[([^]]+)\] "(\w+) (\S+).*" (\d+) (\S+) +/; } sub do_split { my @fields = split; $fields[3] =~ s/\[//; $fields[4] =~ s/\]//; my $timestamp = $fields[3]; my $source = $fields[0]; my $status = $fields[$#fields-1]; $fields[5] =~ s/\"//; $fields[7] =~ s/\"//; my $method = $fields[5]; my $uri = $fields[6]; my $protocol = $fields[7]; } timethese(1, { by_regexp => 'regexp($log)', by_split => 'do_split($log)' });

Gives this:

Benchmark: timing 1 iterations of by_regexp, by_split... by_regexp: 0 secs ( 0.00 usr 0.00 sys = 0.00 cpu) (warning: too few iterations for a reliable count) Use of uninitialized value at ./bench2.pl line 14. Use of uninitialized value at ./bench2.pl line 15. Use of uninitialized value at ./bench2.pl line 19. Use of uninitialized value at ./bench2.pl line 20. by_split: 0 secs ( 0.00 usr 0.00 sys = 0.00 cpu) (warning: too few iterations for a reliable count)

I know that I have the iterations set to 1, but that is so I won't have a ton of crap on my screen while I'm fixing this.

JJ

Replies are listed 'Best First'.
RE: strange uninitialized value problem
by swiftone (Curate) on Jun 02, 2000 at 21:53 UTC
    sub do_split { my @fields = split;
    By default split operates on $_, while your argument is in @_. So all the $fields[N] lines are uninitialized.

      Either I'm missing something, or I'm dumb.

      $log = "something", do_split($log) should have a string, right?

      J. J. Horner
      Linux, Perl, Apache, Stronghold, Unix
      jhorner@knoxlug.org http://www.knoxlug.org/
      
        $log = "something", do_split($log) should have a string, right?

        Right, but that string is in $_[0] not $_.
        Try putting:

        shift;
        as the first line of your sub. That will pop $_[0] into $_;
Re: strange uninitialized value problem
by Fastolfe (Vicar) on Jun 03, 2000 at 01:15 UTC
    I also believe the 'my $log' will prevent Benchmark from seeing $log. You should use 'our $log' or 'use vars qw/$log/' prior to the assignment.
      Fortunately, $log is available to the line you mention. It's in the same lexical scope (the main file block).

      Note that our has just been introduced in Perl 5.6.