Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Bad file descriptor

by Anonymous Monk
on Jan 28, 2001 at 06:10 UTC ( [id://54812]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

My PERL development was revolving up to now around web and database connectivity. Now, I'm trying to use it in my day-to-day sysadmin task on our UNIX boxes. Here is a very simple script which gives my some trouble for one reason or another:
open(DIR, ".") or die "Can't open the current directory: $!\n"; my @names = readdir(DIR) or die "Unable to read current dir: $!\n"; closedir(DIR); foreach my $name (@names) { next if ($name eq "."); next if ($name eq ".."); if (-d $name) { print "found a directory: $name\n"; next; } if ($name eq 'core') { print "found 'core' file!\n"; } }
The script dies at my @names = readdir(DIR) or die "Unable to read current dir: $!\n"; closedir(DIR); with a "Bad file descriptor" error message.

What does that mean?

Thanks.

Replies are listed 'Best First'.
Re (tilly) 1: Bad file descriptor
by tilly (Archbishop) on Jan 28, 2001 at 06:16 UTC
      Oops. That's why I like coming here! I was staring at my code not seeing the obivious.

      Thanks for pointing this out! :-)

Re: Bad file descriptor
by moen (Hermit) on Jan 28, 2001 at 06:17 UTC
    That should be:
    opendir(DIR, ".") or die "Can't open the current directory: $!\n";
    instead of just open()
Re: Bad file descriptor
by Beatnik (Parson) on Jan 28, 2001 at 06:23 UTC
    It means a filehandles != a dirhandle. Use opendir() to open a dir for readdir().

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Bad file descriptor
by quidity (Pilgrim) on Jan 28, 2001 at 06:18 UTC

    You'll not be wanting open rather opendir:

    opendir DIRHANDLE,EXPR

    Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir, and closedir. Returns true if successful. DIRHANDLEs have their own namespace separate from FILEHANDLEs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54812]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-25 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found