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

My script opens a file, reads it or creates it/writes data to it, and displays the contents. The problem is nothing is read when opening the file. I've verified the file is written to by opening it with Notepad. Here's the script:
use strict 'vars'; use warnings; use Fcntl; use POSIX; use POSIX qw(setsid); use POSIX qw(:errno_h :fcntl_h); File_Data(); sub File_Data { my $path = "fd_search.txt"; my $create = 0; my $write = 0; my @temp = ""; sysopen(my $FH,$path,O_RDONLY) or $create = 1; if (not $create) { @temp = <FH>; if (not @temp) { $write = 1; close($FH); } } if ($write || $create) { if ($create) { sysopen(my $FH,$path,O_CREAT) or die "Could not create $path."; close($FH); } sysopen(my $FH,$path,O_WRONLY) or die "Could not open $path to wri +te to."; print $FH "one two three"; close($FH); sysopen(my $FH,$path,O_RDONLY) or die "Could not open $path the 2n +d time"; @temp = <FH>; } close($FH); my $count = 0; print "<tr>\n"; print "<td>($count)@temp</td>\n"; print "</tr>\n"; }
Again, I can write to the file but can't read from it and can't figure out why?

Replies are listed 'Best First'.
Re: Reading file contents
by jethro (Monsignor) on Jul 04, 2011 at 16:34 UTC
    You are using FH to read from the file while the filehandle is $FH

      A strong case for not only using lexical filehandles, but using lexical filehandles that are lower-cased so that they don't even slightly look like old-school bareword glob filehandles.


      Dave

      I replaced FH with $FH and there wasn't any difference.
        Look at line 44 of your script:

        sysopen(my $FH,$path,O_RDONLY) or die "Could not open $path the 2nd time";

        You have already declared $FH so the my declares it again which masks the previously declared variable of the same name.

Re: Reading file contents
by GrandFather (Saint) on Jul 04, 2011 at 21:04 UTC

    When I run your code I get:

    "my" variable $FH masks earlier declaration in same scope at C:\Users\ +Peter\Delme~~\PerlScratch\noname1.pl line 36. readline() on unopened filehandle FH at C:\Users\Peter\Delme~~\PerlScr +atch\noname1.pl line 38. <tr> <td>(0)</td> </tr>

    which tells you exactly what jethro and d5e5 determined for you. Using strictures is good, but they only work for you if you take notice of the warnings and errors they issue!

    A few other tips: avoid needless initialisation. Use sensible and meaningful names. Take advantage of all failure information. Consider:

    use strict; use warnings; use Fcntl; use POSIX qw(setsid :errno_h :fcntl_h); File_Data(); sub File_Data { my $path = "fd_search.txt"; my $create = !sysopen(my $inFile, $path, O_RDONLY); my $write; my @data; if (not $create) { @data = <$inFile>; $write = !@data; close $inFile; } if ($write || $create) { if ($create) { sysopen(my $outFile, $path, O_CREAT) or die "Create $path failed: $!"; close $outFile; } sysopen(my $outFile, $path, O_WRONLY) or die "Create $path fai +led: $!"; print $outFile "one two three"; close $outFile; sysopen(my $inFile, $path, O_RDONLY) or die "Reopen $path fail +ed: $!"; @data = <$inFile>; } print "<tr>\n"; print "<td>(0)@data</td>\n"; print "</tr>\n"; }
    True laziness is hard work
      I'm not getting that error/warning.
        Sure you are, but maybe, just maybe, the it is in your error log
Re: Reading file contents
by davido (Cardinal) on Jul 04, 2011 at 16:25 UTC

    It would help to clarify for us the following: Does the decision path ever make it to the first portion of read code? If it does, we would want to figure out why the file isn't being read. But if it does not, we would want to find out why the file is failing to open for reading.

    Also, you said you can write to the file but not read from it. Have you verified that the file has been written to? Once it's been written to, does your second sysopen(....O_RDONLY) die? Help us to understand where the problem is occurring. It's possible that once you've made that determination for us, you will spot the problem yourself anyway. :)


    Dave

      No messages were ever displayed from any of the 'die' parts, meaning the file was successfully opened each time. I verified the file was successfully writen to by opening it with Notepad and see the data there. I had inserted descriptive output before and after the sysopen statements and it showed the file being open and written to everytime. For some reason the file could be opened for reading/writing but when opened for reading it could't see/read the data.