For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2005 > Script dying at opendir()









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Script dying at opendir()
geek

2005-04-27, 3:58 pm

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

jl_post@hotmail.com

2005-04-27, 3:58 pm

g wrote:
>
> 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

geek

2005-04-27, 3:58 pm

Thank you so much !! It works.

A. Sinan Unur

2005-04-27, 3:58 pm

"g" <metri.jain@gmail.com> wrote in news:1114616899.049589.37860
@l41g2000cwc.googlegroups.com:

> 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.

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
Chris Mattern

2005-04-27, 3:58 pm

g wrote:

> 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?"
Tintin

2005-04-27, 8:57 pm


"g" <metri.jain@gmail.com> wrote in message
news:1114616899.049589.37860@l41g2000cwc.googlegroups.com...
> 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> ) {
..
}


Tad McClellan

2005-04-27, 8:57 pm

Tintin <tintin@invalid.invalid> wrote:

> 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.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Villy Kruse

2005-04-28, 8:57 am

On Wed, 27 Apr 2005 14:23:24 -0400,
Chris Mattern <matternc@comcast.net> wrote:


> g wrote:
>
>
> pwd returns an newline at the end...
>



Cwd::cwd doesn't. It may use `pwd` internally, or may not, depending
on the system the program is running on.

Villy
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com