LinuxQuestions.org
Visit Jeremy's Blog.
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 10-16-2009, 02:05 PM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
perl scritp cant do (-d "$dir") properly


Here is the code

Code:

   #!/usr/bin/perl
   $src ="/";
   
   
   opendir(DIR, $src);
   
   
   @names=readdir(DIR);
   
  while ($nameA = shift(@names)){
  next if ($nameA eq ".");
  next if ($nameA eq "..");
  
  
  if (-d $nameA){
  
  
  print "===================\n";
   }
  
  }
~
Any idea?
 
Old 10-16-2009, 07:04 PM   #2
Lordandmaker
Member
 
Registered: Sep 2005
Location: London, UK
Distribution: Debian
Posts: 258

Rep: Reputation: 38
Not without knowing what the problem is, no.
 
Old 10-16-2009, 07:31 PM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by knockout_artist View Post
Here is the code

Code:

   #!/usr/bin/perl
   $src ="/";
   
   
   opendir(DIR, $src);
   
   
   @names=readdir(DIR);
   
  while ($nameA = shift(@names)){
  next if ($nameA eq ".");
  next if ($nameA eq "..");
  
  
  if (-d $nameA){
  
  
  print "===================\n";
   }
  
  }
~
Any idea?
Just one. What happened? How did what happened differ from your expectations? If you called 911 and said "help" but offered no additional information, how soon do you think it would take for the fire trucks and helicopters to arrive?
 
Old 10-18-2009, 09:23 PM   #4
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
Looks ok-ish to me. I'd do it differently, and in any case, you should start all Perl progs with
Code:
#!/usr/bin/perl -w
use strict;
for best practice.

The core of your loop I'd do more like this
Code:
opendir(DIR, $dirname) or die "Can't opendir $dirname: $!\n";
while (defined($file = readdir(DIR))) 
{
    next if $file =~ /^\.\.?$/;     # skip . and ..
    # do something with "$dirname/$file"
}
closedir(DIR) or die "Can't closedir $dirname: $!\n";
As the Perl Cookbook points out
Quote:
As explained in the Introduction, the filenames returned by readdir do not include the directory name. When you work with the filenames returned by readdir, you must either move to the right directory first or prepend the directory to the filename.
See also
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
  


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
Perl - Can't use string ("html") as an ARRAY ref while "strict refs" OldGaf Programming 9 08-11-2009 12:14 PM
"Failed Dependency error" while installing RPM for "DateTime" perl modules giridhargopal.cj Linux - Newbie 7 11-19-2008 01:05 AM
perl install error: Can't locate object method "new" via package "Module::Build::Vers powah Linux - Software 0 10-24-2006 02:57 PM
problem "make"ing gtk+ "/usr/bin/env: perl -w" caid Linux - Newbie 8 07-29-2005 05:51 AM
LFS 4.1: Stalled at Perl, "missing seperator" error from "make" SparceMatrix Linux From Scratch 1 06-07-2003 04:31 PM


All times are GMT -5. The time now is 11:03 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