/howard$: ls test Apr15 May15 testpm.dat EDI204_blah testing_all_Oct14.tar testpm2.pl /howard$: perl -d -e 1 ... main::(-e:1): 1 DB<1> $path='/howard/test/' DB<2> opendir(DH,$path) DB<3> @files=readdir(DH) DB<4> closedir(DH) DB<5> x @files 0 '.' 1 '..' 2 'Apr15' 3 'EDI204_blah' 4 'May15' 5 'testing_all_Oct14.tar' 6 'testpm.dat' 7 'testpm2.pl' DB<6> open $fh,'<',$files[6] or die $! No such file or directory at (eval 22)[/home/edi/perl/5.8.8/lib/5.8.8/ +perl5db.pl:628] line 2. DB<7> open $fh,'<',$path . $file or die $! DB<8> close $fh DB<9> @fglob = glob($path . "*") DB<10> x @fglob 0 '/howard/test/Apr15' 1 '/howard/test/EDI204_blah' 2 '/howard/test/May15' 3 '/howard/test/testing_all_Oct14.tar' 4 '/howard/test/testpm.dat' 5 '/howard/test/testpm2.pl' DB<11> open $fh,'<',$fglob[6] or die $! DB<12>

A couple things to note:

  1. readdir returned the . and .. links, glob did not
  2. Both returned both directories and files (Apr15 and May15 are directories).
    You will want to test to make sure $file is a file before you try to open it.
  3. readdir returned file name only, glob the whole path. That's why open $fh,'<',$files[6] failed.
    I needed to append the path before opening the file.
Dum Spiro Spero

In reply to Re: Trouble opening and reading file within loop by GotToBTru
in thread Trouble opening and reading file within loop by H0tc@xe

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.