in reply to CMD says files is closed when It should be open

choroba's file handle name advice is excellent. However, since you're wanting to place a file's entire contents into a string, you can use File::Slurp; to do this while avoiding file handles in the process. When using File::Slurp, you can replace this code segment:

open (DATA, $data); my @secdata = <DATA>; my ($str) = join "",@secdata; close(DATA);

With the following:

my $str = read_file $data;

Hope this helps!

Replies are listed 'Best First'.
Re^2: CMD says files is closed when It should be open
by doctordoctor (Initiate) on Aug 21, 2012 at 20:57 UTC

    I have updated my code to include what you suggest, and based on what I have read about File::Slurp you are correct, it is the proper choice. However, the problem I run into now is the error: "Uncaught exception from user code: read_file 'F:\SEC\10s\.' - sysopen: Permission denied at ... line 22. Perhaps for some reason the file name is not getting appended to the file path and that is causing the problem?

    #!/usr/bin/perl use 5.014; # so push/pop/etc work on scalars (experimental) use strict; use warnings; use diagnostics; use File::Slurp; use LWP::Simple 'get'; use HTML::TableExtract; use HTML::FormatText::WithLinks::AndTables; my $directory = 'F:\\SEC10q\\10Qs\\'; #print "$directory\n"; my $n = 0; opendir (DIR, $directory) or die $!; while (my $secfile = readdir(DIR)) { #print $directory."$secfile\n"; my $data = $directory."$secfile"; ########################## print $secfile; print $data; my $str = read_file($data); print $str; $n=$n+1; print "$n\n"; ######################### my $te = HTML::TableExtract->new( headers => [ 'Purchased','Average','Publicly','May'], slice_columns => 0,keep_html => 0,br_translate => 0 #shoul +d we turn the automap option on? ); $te->parse($str); } closedir(DIR);
      readdir returns ., the current directory, as one of the entries in the current directory. You only want to work with files, so you can add
      next unless -f $data;
      after line 18.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      choroba has a good eye(!), catching the '.' (current directory) entry, and then recommending using the '-f' file test operator.

      If you're only processing htm(l) files, then here's another option that you can place immediately below the while line:

      next unless $secfile =~ /\.html?$/;

      This will only allow the htm(l) file names through.