Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^6: Use of uninitialized value in string eq

by Anonymous Monk
on Sep 10, 2014 at 17:00 UTC ( [id://1100188]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Use of uninitialized value in string eq
in thread Use of uninitialized value in string eq

No--it looks like I must have added the | immediately prior to the initial posting. I've taken out the $fh from the code, so it now reads:

my @files = <*.htm>; foreach $file (@files) { open($file or die "can not open .txt file: $!") ; while(<$file>) { my $dom = Mojo::DOM->new(<$file>); my $text = $dom->all_text(); for (split/\s+/, $text) { push @sequence, $_ ; if (@sequence >=10) { shift @sequence until @sequence ==10 ; ++$sequences{"@sequence"}; } } } } close($file) ;

This specification is what generates the "Can't use string..." error I cited.

Replies are listed 'Best First'.
Re^7: Use of uninitialized value in string eq
by mr_mischief (Monsignor) on Sep 10, 2014 at 17:37 UTC

    This won't do what you want, whether it compiles or not.:

      open($file or die "can not open .txt file: $!") ;

    This line appears to have changed from the original node:

    foreach $file (@files) {

    That will not work under strict unless you've added a my $file somewhere above that. Try not to hand-copy the code. Using copy and paste is much more accurate.

      Why won't that line do what I want? Is there something wrong with the open file statement I'm using? When I try a two-argument open command by assigning $file to a filehandle called INFILE, I get an error message saying: "Useless use of a constant (INFILE) in void context..." In addition, under the copied and pasted code I show in my earlier comment (which does have a 'my $file' at the top not included in the copy and paste), I continue to get an error message saying: "Can't use string ("1000694-R20120314-C20111231-F10-"...) as a symbol ref while "strict refs" is use", referring to the line where my open file command is. This error seems to suggest my code is reading the name of the file as a string instead of the file itself. Any idea how to fix that?

        Take it piecewise. The part inside the brackets is $file or die "...". That will die if $file is zero or an empty string, or otherwise false.

        Then the result gets given to the open function. open "filename"; doesn't make any sense.

        See open, and use something more like open my $inputFileHandle, '<', $filename or die "can't open $filename for reading: $!\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1100188]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found