Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't open directory, or what's wrong with my (Perl) code?

Tags:

perl

opendir

I have a file name logspath.txt which contain on the logs directory path on the machine. one directory each line. Although the directory exist, Perl say it doesn't.

#!/usr/bin/perl -w

open FILE,"logspath.txt" or die $!;
while (<FILE>){
   print $_;
   opendir ($DIR,chomp($_)) or die $!;

When I'm running the script I get:

/home/amosa/
No such file or directory at archive.pl line 6, <FILE> line 1.

Listing the directory :

~$ ls -l /home/amosa/
total 6
drwxr-xr-x  11 amosa    prodapp     1024 Mar  2 12:49 deploy
drwxr-xr-x   2 amosa    prodapp      512 Mar  2 12:39 lib
-rw-r--r--   1 amosa    prodapp      787 Mar  2 11:02 s

Any advice?

like image 337
DoronS Avatar asked Dec 13 '22 23:12

DoronS


1 Answers

chomp has no meaningful return value that you can then pass onto opendir. You need to chomp your string in a separate statement, above the opendir.

 chomp;
 opendir DIR, $_ or die ...
like image 101
Chris Jester-Young Avatar answered Jan 05 '23 00:01

Chris Jester-Young