Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

why would opendir fail?

by Anonymous Monk
on Apr 28, 2001 at 11:38 UTC ( [id://76358]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a line in a script that looks like this:

opendir(DIR, $Path->{'real'});

This call returns false. The value of $Path->{'real'} is defined by a module that gets the value from a conf file. If I print out the value I get the following:

/usr/

This directory of course exists, so the following code returns true:

opendir(DIR, "/usr/");

Strangely, so does this code:

$Path->{'real'} = "/usr/"; opendir(DIR, $Path->{'real'});

This led me to think $Path->{'real'} was tainted and therefore not usable in the opendir call, however it still doesn't work if I untaint it like so:

$Path->{'real'} =~ /(.*)/; $Path->{'real'} = $1; opendir(DIR, $Path->{'real'});

This code is a functioning part of mozilla.org's installation of LXR running on a Solaris box with perl 5.004. I am trying to get it working on my own RedHat Linux 7.0 machine running 5.6.1. What could be going wrong here?

Replies are listed 'Best First'.
Re: why would opendir fail?
by redcloud (Parson) on Apr 28, 2001 at 17:48 UTC
    I don't know how you're parsing your conf file, but I'd check to make sure that $Path->{'real'} doesn't include a trailing newline.

    As a practice, when I'm printing out variables when debuging, I'll print delimiters around the variable to make this easy to see. For example, in this case something like print "|", $Path->{'real'}, "|\n";. If the second | ends up a line by itself, the problem is clear. (And chomp is your friend).

    That may not be what the problem is in your case, but it's worth checking.

      Bingo! (Almost) It turned out to be a preceding space. Thanks for putting the bug in my head!
Re: why would opendir fail?
by BMaximus (Chaplain) on Apr 28, 2001 at 12:42 UTC
    I notice that you don't have an or die "Can't open dir $Path->{'real'}: $!\n";. It could tell you a lot as to why its failing. To check to see if the tainting is ok make sure you have the -T switch on. Otherwise it won't complain if there is an issue (unlikely). I'm currently running RH 7.0 with 5.6.1 and have no problem running this test code. Make sure that your permissions are ok (its unlikely that they are not.).

    #!/usr/bin/perl -wT use strict; my $Path = {}; $Path->{'real'} = "/usr/"; opendir(THE_DIR, $Path->{'real'}) or die "Can't open dir $Path->{'real +'}:$!\n"; close(THE_DIR);


    Update: Also you might want to put a print to STDERR or a LOGFILE to print out $Path->{'real'} to see if the module that pulles the conf in is doing its job.

    Hope this helps,
    BMaximus

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://76358]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-18 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found