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

The question I have at this time is one of confusion. In my script I am calling a path for a predefined sub routine.
&scandir("c:/mydocu~1");
How can I set this to a variable called $workdir? Then have the code do a chdir to that directory for opening, reading and closing? Here is the top portion of my sub routine.
sub scandir { my ($workdir) = shift; chdir($workdir) || print "Unable to change to $workdir: $!\n";
Yes, I am using strict on this one. Can someone suggest something. I have spent hours on this one. curtisb -- "Becareful what you wish for, you might get it!!"

Replies are listed 'Best First'.
RE: Setting a path for a sub function
by Fastolfe (Vicar) on Nov 13, 2000 at 04:52 UTC
    I'm kind of confused. The code you gave should do exactly what you're wanting to do.
    &scandir("c:/mydocu~1"); sub scandir { my $workdir = shift; # "c:/mydocu~1" is in $workdir chdir($workdir) || ... # chdir("c:/mydocu~1").. why not?
    You may be interested in using warn or die instead of print when you're printing an error message (such as after your chdir).

    Perhaps if you could elaborate on what problems you're having? You ask us how to do something and then show us how you're trying to do it, and that looks correct. I don't see what the problem is. If you're getting an error message, please pass that message along to us.

Re: Setting a path for a sub function
by the_slycer (Chaplain) on Nov 13, 2000 at 08:31 UTC
    I tried the exact same thing. It works, no prob at all.. I have only one thought, part of the problem with using 8 character names for the directory (vs long name) is that it is not ALWAYS going to be "mydocu~1".

    It may be that this is the case here (I have never seen it for My Documents but I have seen cases where the 8 character name is not what you think it is :-) ). Like the others have mentioned, really need the error message from warn or die..

    You could try passing the dir in single quotes with the whole name, see if this makes a difference (ie: 'c:\my documents')
Re: Setting a path for a sub function
by AgentM (Curate) on Nov 13, 2000 at 03:24 UTC
    I a bit confused by what you mean. Chdir will change the current path in the current program (not the shell), which is not overly useful. Use opendir,readdir,closedir to do stuff within the directory. The chdir is meaningless. Perhaps you are also asking about permissions? (opening, reading, and closing) In this case you should use chmod. I believe you are getting something confused here- or I am confused- what is the purpose of chdir here? You can open the dir passed as the argument (fully qualified) and open, read, and close it without chdir.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.