Apr 21, 2015, 10:34 AM
Post #4 of 4
Re: [perlFun] cd to a directory so I can make a system call
[In reply to]
I realize that system(('cd <my repository directory', 'git rebase master')); won't work, because it's not actually changing the directory. I wasn't certain what is the safest way to do this.
Well, just to let you understand why this does not work, you should know that when you use the system built-in, Perl does a fork, i.e. sort of creates a new sub-session. This sub-session actually change to your new directory and then dies. Then a second fork occurs and a second sub-session is created, and,of course, this second sub-session does not "know" that the first sub-session cd'ed to another dir.
I had a similar problem a few weeks ago where I needed to issue two system commands in a row in a one-liner, I had to write a small shell script, so that the system command (well is was actually back-ticks) would call that shell script, which in turn would call the two commands needed within the same session. In that case, there was no other solution such as available modules.
But in your case the chdir function can be used and I fully agree with FishMonger that using CPAN modules rather than issuing system calls is a much better solution, when available.