Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical

'No such file or directory' error

by Win (Novice)
on May 04, 2007 at 12:04 UTC ( #613549=perlquestion: print w/ replies, xml ) Need Help??
Win has asked for the wisdom of the Perl Monks concerning the following question:

For some reason there is a 'No such file or directory' error coming back when I try the following:
opendir(DIR, "<$dir") or die $!; my $file_name; while (<DIR>){ $file_name = $_; }

Comment on 'No such file or directory' error
Download Code
Re: 'No such file or directory' error
by tinita (Parson) on May 04, 2007 at 12:11 UTC
    For some reason there is a 'No such file or directory' error coming back
    i guess the reason is that there is no such file called $dir (for whatever is in $dir). open() opens files.

    and now my guess: you want to read a directory instead of a file. please read perlfunc. the function for reading directory contents is called opendir, not open.

    update: oops, sorry. you are already using opendir, but then reading with the diamond operator. you should read directory contents with readdir. and, like Fletch said, you also shouldn't put a < or something in front of the directory name. still, reading perlfunc is a good advice.

      opendir is the function that I have used. Please read the code above.
Re: 'No such file or directory' error
by Fletch (Chancellor) on May 04, 2007 at 12:12 UTC

    Probably because you don't have a directory named after the contents of $dir prefixed with an < character. Nowhere in the documentation is opendir shown to use mode characters like open. But then considering your long history of just making crap up and expecting it to work, should we have expected any less?

    Addendum: And to save the next blindingly obvious question when you've fixed that, <CUR> wouldn't do anything useful either and would produce warnings under use warnings. I'd say read the readdir documentation, but then we all know how much good that would do.

      Bad symbol for filehandle .... is now the error message I get with:
      readdir(DIR, "$dir") or die $!;
      Update: After reading the docs, the following works:
      opendir(DIR, $dir) || die "can't opendir $dir: $!"; @file_names = grep { /^\./ && -f "$dir/$_" } readdir(DIR); closedir DIR;
        If I had you around, I'd just break a two inch thick wooden stick on your back.

        READ THE DOCS: readdir


        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        Right, because that's just what the readdir documentation that I just pointed you at IN THE SAME FRAKING POST YOU'RE REPLYING TO shows as how to use it.

        Quoth dominus: #11901 You can't just make [explitive deleted] up and expect the computer to know what you mean, Retardo!

        Update: And you should also keep #11900 in mind as well: "You cannot just paste code with no understanding of what is going on and expect it to work."

        Clarification: My initial "that's just what the readdir documentation" remark was intended as sarcasm because calling it with two arguments is quite obviously not what it shows. The update to his post initially had a copy and paste of the example from the readdir perldocs but with the grep being called in scalar context (which doesn't do anything useful as far as getting you a filename). The code was later stealth updated to correct the scalar into the array as shown now.

        After reading the docs, the following works

        I think* we've made a breakthrough.


        A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: 'No such file or directory' error
by blue_cowdawg (Prior) on May 04, 2007 at 14:02 UTC
        For some reason there is a 'No such file or directory' error

    Probably because you are seriously misusing the opendir system call. Straight from the docs:

    opendir DIRHANDLE,EXPR Opens a directory named EXPR for processing by "readdir", "telldir", "seekdir", "rewinddir", and "closedir". Returns true if successful.
    Pray tell, where in your code are you using readdir? Secondly, what's in the scalar $dir?

    As some monks have already pointed out... you lack a basic understanding of the code you are messing with.

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://613549]
Approved by tbone1
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-04-17 18:40 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (453 votes), past polls