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

I am having trouble with system commands

I wrote a subroutine to deal with files in a directory

I have ascertained that the subroutine is working perfectly except in one regard. for some reason, when I execute the command $cd=`cd $directory` the command cd never actually takes place

any help as to how I can access $directory?

TIA

Replies are listed 'Best First'.
Re: trouble with system commands
by roboticus (Chancellor) on Nov 19, 2006 at 03:22 UTC
    tricolaire:

    Using the backticks creates a new process to run the command in. So you're starting a new process, it changes the directory *in that process*, and then the command exits and the process terminates.

    It doesn't change the directory in the process your Perl program is running in. For that use the chdir function.

    --roboticus

Re: trouble with system commands
by graff (Chancellor) on Nov 19, 2006 at 03:53 UTC
    To clarify what roboticus means by "the chdir function", run the command "perldoc -f chdir" and read its output. In your code, instead of doing:  $cd=`cd $directory`; you actually want to do this:
    chdir $directory;
Re: trouble with system commands
by andyford (Curate) on Nov 19, 2006 at 11:00 UTC

    Many people end up writing perl code as if it were a shell script.
    This can lead to much pain in the long term.
    Whenever you find yourself using system or backticks, stop and take a moment to research whether perl already has the capability built-in.

    non-Perl: Andy Ford

Re: trouble with system commands
by swampyankee (Parson) on Nov 19, 2006 at 19:34 UTC

    I believe system starts a subsidiary shell process to execute the command; backticks and qx behave similarly. Environment -- including directory -- changes in a sub-shell don't propogate up; child process don't change their parent's state. Something like system("cd $somedirectory")</tt) is, as far as the program is concerned, a no-op.

    You have to use chdir.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: trouble with system commands
by rerdey (Initiate) on Nov 19, 2006 at 21:57 UTC
    when you run commands via the back ticks, they are system calls that will cause a child process to be created, which does change directories, but the parent does not. I am not sure what you script wants to do, but you may want to look into the opendir Perl function. -- Raymond M. A. Erdey
Re: trouble with system commands
by Anonymous Monk on Nov 20, 2006 at 23:32 UTC
    So you're trying to process files in a directory? perldoc -f glob Combine that with a while loop and you may not need a system call or $chdir at all.
Re: trouble with system commands
by mantra2006 (Hermit) on Nov 19, 2006 at 12:32 UTC
    Hey
    You can try the following...it works in my system
    $cd=system("cd $directory");
    Thanks & Regards
    Sridhar
      Heh... usually we have to ask people: What do you mean when you say "it doesn't work"? But in your case, I have to ask: What do you mean when you say "it works"?

        It should work on DOS.