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

Hi guys,

This is continued from http://www.perlmonks.org/?node_id=668491

i patched the following code provided by svenXY into the server:

#!/usr/local/bin/perl -w use strict; use warnings; use DateTime::Format::Strptime; my $Strp = new DateTime::Format::Strptime(pattern => '%Y-%b-%d %T' +,); my $infile = 'logfile.2008-02-25.log'; my $outfile = 'report-2008-02-25.txt'; my($fh_out, $fh); my %lookup; my $channel = 'two'; my $time_delta = 3600; # seconds = 1 hour open($fh_out, '>', $outfile) or die "Could not open outfile: $!"; # open($fh, '<', $infile) or die "Could not open logfile: $!"; while (<DATA>) { next unless /$channel/; $_ =~ m/^(.*) UTC.*refs = (\d+)$/; my $dt = $Strp->parse_datetime($1); my $timestamp = $dt->epoch(); my $refs = $2; if ( defined($lookup{$refs}) && $lookup{$refs} + $time_delta <= $t +imestamp ) { print $fh_out "REFS $refs: occurrences at " . $lookup{$refs} . + "and $timestamp \n"; print "REFS $refs: occurrences at " . $lookup{$refs} . " and $ +timestamp \n"; } $lookup{$refs} = $timestamp; } close $fh_out; #close $fh;
when i try to run the program, it vomited out the following error:
Name "main::DATA" used only once: possible typo at a3.pl line 24. readline() on unopened filehandle DATA at a3.pl line 24.
The logfile.txt looks like this
2007-Nov-07 00:00:00 UTC (GMT +0000) - Poll: channel = ONE, ref = 2007-Nov-07 00:00:01 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:01 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:02 UTC (GMT +0000) - Poll: channel = THREE, ref = 2007-Nov-07 00:00:02 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:03 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:03 UTC (GMT +0000) - Poll: channel = TWO, ref = 2007-Nov-07 00:00:04 UTC (GMT +0000) - Poll: channel = TWO, ref =

any clue on how to fix this ?

Replies are listed 'Best First'.
Re: need help on unopened file handle
by McDarren (Abbot) on Feb 28, 2008 at 06:48 UTC
    Change your "while" line to read:
    while (<$fh>) {
    And uncomment the line that opens that filehandle.

    The DATA filehandle usually refers to some embedded data at the end of the code, which starts after the __DATA__ line. It's quite often used in examples.

    I suspect that you copied some code verbatim, without realising you needed to change that ;)

    Cheers,
    Darren :)

Re: need help on unopened file handle
by ikegami (Patriarch) on Feb 28, 2008 at 07:11 UTC

    By the way,
    my $channel = 'two'; next unless /$channel/;
    should be
    my $channel = 'TWO'; next unless /$channel/;
    or
    my $channel = 'two'; next unless /$channel/i;

    Better yet, since $channel contains text and not a regex, you should use
    my $channel = 'TWO'; next unless /\Q$channel\E/;
    or
    my $channel = 'two'; next unless /\Q$channel\E/i;

    Also, I wouldn't rely on $1 and $2 to have remained unchanged after calling two methods.

Re: need help on unopened file handle
by munni (Novice) on Feb 28, 2008 at 07:21 UTC
    Why have you commented out this line?
    # open($fh, '<', $infile) or die "Could not open logfile: $!";
    Uncomment it and use
    while(<$fh>)
    as mentioned above
A reply falls below the community's threshold of quality. You may see it by logging in.