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

The code I'm running produces the error message: "Use of uninitialized value in string eq at C:/Dwimperl/perl/site/lib/Mojo/DOM.pm line 226, <$fh> line 1." It repeats this error message many times while the program is running, and then one final error message saying: "Can't use an undefined value as a symbol reference at Six_gramsC.pl line 33." My code doesn't have 226 lines, so I'm not sure what document is being referenced by the first error message. I also believe I've defined the variable $fh. The program is designed to identify all unique ten-word sequences in a batch of text files and does partially work, in that it produces at least some correct output (the "sequence" hash looks mostly, but not quite all, correct). Can you provide any further insights as to what is causing the error message? (/p>

#!/usr/bin/perl use strict ; use warnings ; use Mojo::DOM; my $path = "U:/Perl/risk disclosures"; chdir($path) or die "Cant chdir to $path $!"; # This program counts the total number of unique six-grams in a 10-K a +nd enumerates the frequency of each one. # Starting off computing a simple word count for each word in the 10-K +. my @sequence ; my %sequences ; my $fh ; # Here creating an array of six-grams. my @files = <*.htm>; foreach my $file (@files) { open($fh, $file|) ; while(<$fh>) { my $dom = Mojo::DOM->new(<$fh>); my $text = $dom->all_text(); for (split/\s+/, $text) { push @sequence, $_ ; if (@sequence >=10) { shift @sequence until @sequence ==10 ; ++$sequences{"@sequence"}; } } } } close($fh) ; my @keys = sort { "\L$sequences{$a}" <=> "\L$sequences{$b}" or "\L$a" cmp "\L$b" } keys %sequences ; open(my $fh3, '>', 'report4.txt') ; foreach (@keys) { print $fh3 "$_ \t $sequences{$_}\n "; } close $fh3 ;

Replies are listed 'Best First'.
Re: Use of uninitialized value in string eq
by toolic (Bishop) on Sep 10, 2014 at 15:54 UTC
    My code doesn't have 226 lines, so I'm not sure what document is being referenced by the first error message.
    As the warning message states, the warning is coming from line 226 of file C:/Dwimperl/perl/site/lib/Mojo/DOM.pm, not the code you posted. Mojo::DOM on CPAN has more than 26 lines.

      Thank you for your response. Line 226 of the referenced file is:

      $_->[1] eq 'pre' and $trim = 0 for $self->_ancestors, $tree;

      I have no idea what this means or how it relates to my code. Any wisdom would be appreciated.

        In your code, you declared $fh, but you should check the status of the open to make sure it is defined (Tip #7 from the Basic debugging checklist).