Reverend Brothers and Sisters,

This humble non programmer seeks your wisdom in a puzzling (for me) matter.

Goal: The bit of code giving me a headache is intended to retrieve the text from all txt files in a directory. It will be used in another script that creates xml files including that text (I am proud to report that my xml script works just fine, processing one file at a time).

What I have so far:

My problem: Why when I put these two together it doesn't work? Is my logic flawed (Oh the shame!)? Here's the offending code:

use locale; use strict; use warnings; if ( $#ARGV != 0 ) { die "Dossier à traiter? Usage : perl openDirFile.pl /users/vot +reNom/Dossier\n"; } my $dossier = "$ARGV[0]"; opendir (DIR, $dossier) or die "Impossible d'ouvrir le dossier ", +"$dossier : ", "$!"; while (my $fichier = readdir (DIR)) { next if $fichier !~ ".\.txt"; $/ = undef; open (FIC, $fichier) or die "Impossible d'ouvrir le fichier ", + "$fichier : ", "$!"; my $texte = <FIC>; print $texte, "\n"; close FIC; } closedir (DIR);

When I run the program I get the error message: Impossible d'ouvrir le fichier MecaLatin.txt : No such file or directory at openDirFile.pl line 15. The aforementioned file is the first one in the directory.

I did try other ways to write the code and lost my sanity in www.perl.org, but I only managed to get more obscure error messages... (sigh!)

Other information that may (or probably will not) be useful: I use Perl 5.8.8 on Mac OS 10.5.8.

Eternal thanks in advance for any insights.


In reply to Trouble opening files from a directory: "no such file". by Alienor

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.