I have always had problems reading files from a directory and I hope someone can help. I think the problem is in the the "/" in the open command.
my $dir = 'input_files'; opendir(DIR, $dir) or die "can't opendir $!"; while (defined(my $file = readdir(DIR))) { # do something with "$d +irname/$file" print "The directory and file are $dir/$file\n"; open (IN, "< $dir/$file" ) or die ("Can't open input file $!"); while ( < IN > ){ print ; } } closedir(DIR);
File names
allergies immunizations
Test file content
# Input file for allergies # Date, Diagnosed By, Type, allergy, reaction,specifics<br> 2009-05-16,Children's Hospital Boston,drugs,penicillin,Blue rash,This +only happens on weekends<br> 2009-05-17,Boston Medical Group,drugs,Vitamin B,Rash on torso,This hap +pens after 9PM<br> 2009-05-17,Memorial Hospital,food,Diary,Upset stomach and gas, Happens + after drinking whole milk<br>
output:
perl create_indivo_schemas.pl Name "main::IN" used only once: possible typo at create_indivo_schemas +.pl line 18. The directory and file are input_files/allergies INThe directory and file are input_files/immunizations INThe directory and file are input_files/.. INThe directory and file are input_files/.
open (my $fh, "< $dir/$file" ) or die ("Can't open input file $!"); while ( < $fh > ){ print ; }
produces this output: perl create_indivo_schemas.pl
The directory and file are input_files/allergies
GLOB(0x8dbaa14)The directory and file are
input_files/immunizations
GLOB(0x8dd4214)The directory and file are input_files/..
GLOB(0x8e33a2c)The directory and file are input_files/.
This also will not work
open (my $fh, "< input_files/immunizations" ) or die ("Can't open inp +ut file $!");
I also tried this:
my @file_list = `ls input_files`; # get the list of files from the i +nput directory foreach (@file_list){ # loop through the files that are in the direct +ory chomp; my $filename = 'input_files/'.$_; print "file name is : $filename\n"; open (my $fh, "< $filename" ) or die ("Can't open input file $!"); open (my $fh, "< $filename" ) or die ("Can't open input file $!"); while ( < $fh > ){ # look through the data in the file print; print "\n"; }
Can someone tell me what's wrong? I think I had a problem with this a year ago but I was able to get it to work on windows. So, that makes me think it's not the code. Thanks My font on this post seems a bit large. Sorry.

In reply to reading files from a directory by kevyt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.