Script dying at opendir()

G

geek

Hi all,

My script is dying when I try to open the current dir( at the line
opendir(DIR, "$dir").
This is the code.
==================================
my $dir = `pwd`;
my ($filename,$mydate);
print "HELLO";
opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");
print "HELLO";
while (defined($filename = readdir(DIR))) {
print "Hello";
if ($filename =~ /\.date$/){
======================================
Any help will be great.

Thanks,
MJ
 
J

jl_post

geek said:
My script is dying when I try to open the current dir
( at the line opendir(DIR, "$dir").
This is the code.
==================================
my $dir = `pwd`;
my ($filename,$mydate);
print "HELLO";
opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");
print "HELLO";
while (defined($filename = readdir(DIR))) {
print "Hello";
if ($filename =~ /\.date$/){
======================================
Any help will be great.


Try inserting the line:

chomp($dir);

right after you assign $dir the return value of `pwd`.

-- Jean-Luc
 
A

A. Sinan Unur

My script is dying when I try to open the current dir( at the line
opendir(DIR, "$dir").
This is the code.
==================================
my $dir = `pwd`;

You don't really need to spawn an external process to get the current
directory, you know.
my ($filename,$mydate);
print "HELLO";
opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");

The die message is very misleading. You would have found out your error
if instead you had used:

opendir my $dir_h, $dir
or die "Can't opendir $dir: $!";

(Note that I removed the useless quotation marks around $dir in the
opendir call -- see perldoc -q always).

On the other hand, if all you want is to read the entries in the current
directory, you could use:

opendir my $dir_h, '.'
or die "Can't open current directory: $!";

Sinan.
 
C

Chris Mattern

geek said:
Hi all,

My script is dying when I try to open the current dir( at the line
opendir(DIR, "$dir").
This is the code.
==================================
my $dir = `pwd`;

pwd returns an newline at the end...
my ($filename,$mydate);
print "HELLO";
opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");

....which you never chomped. The opendir fails and the die kills the
script, because the directory you tried to open doesn't exist. The die
should have told you this.
print "HELLO";
while (defined($filename = readdir(DIR))) {
print "Hello";
if ($filename =~ /\.date$/){
======================================
Any help will be great.

Thanks,
MJ

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
T

Tintin

geek said:
Hi all,

My script is dying when I try to open the current dir( at the line
opendir(DIR, "$dir").
This is the code.
==================================
my $dir = `pwd`;
my ($filename,$mydate);
print "HELLO";
opendir(DIR,"$dir") or die ("$0:can't opendir public_html: $!");
print "HELLO";
while (defined($filename = readdir(DIR))) {
print "Hello";
if ($filename =~ /\.date$/){
======================================
Any help will be great.

You already have answers as to why it doesn't work. I would write it as:

foreach my $file (<*.date>) {
..
}
 
T

Tad McClellan

Tintin said:
You already have answers as to why it doesn't work. I would write it as:

foreach my $file (<*.date>) {


I would write it as:

foreach my $file (glob '*.date') {


Because I don't want to have to pause everytime I see <> in my code
in order to figure out if it is readline() or glob().

<> is always input in my programs, so there's no need to slow
down when reading it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Sorting 3
Help with Hash 2
Merge files 1
opendir() 2
Efficiently searching multiple files 10
recursivity 8
Threads and Directory Handles 2
Very Sluggish Code 4

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top