LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2008, 01:16 PM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
PERL can't read contents of directory.


Hi,

here is the code

Code:
use Cwd;
$DIRNAME =  "/tmp/etc";


open(DIR, $DIRNAME) or die "cant open  $DIRNAME";

my @name = readdir(DIR)    or die "can't read whats in the dir $DIRNAME\n";

foreach my $name (@name){
if (-d $name){
print "dir found \n";
}

}

closedir(DIR);
I do have files/dir in side /tmp/etc but above code is unable to read it.

Thanks
 
Old 09-25-2008, 09:16 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
1. use opendir : http://perldoc.perl.org/functions/opendir.html
In Unix things look like files, but a dir not same as a file

2. When using open, close, opendir, closedir, always use

opendir(DIR, $DIRNAME) or die "cant open $DIRNAME: $!";

$! = error msg supplied by Perl


Always use warnings and strict eg

#!/usr/bin/perl -w
use strict; # Enforce declarations


they'll save you a lot of stress later. You can syntax check a prog without running it using
perl -wc file.pl
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to erase contents in a file in perl john83reuben Programming 3 04-04-2008 03:57 PM
list contents of directory without listing contents baddah Linux - Newbie 2 06-12-2006 05:02 AM
How to read contents of CPU registers? irfanhab Programming 1 12-28-2004 09:04 AM
Can mount as user, but not read contents Rikra Linux - Hardware 13 12-07-2004 07:31 AM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 08:03 PM


All times are GMT -5. The time now is 09:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration