CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
cd to a directory so I can make a system call

 



perlFun
User

Apr 20, 2015, 6:52 PM

Post #1 of 4 (1410 views)
cd to a directory so I can make a system call Can't Post

I am setting up a cronjob which updates a git repository. In order to update my repository, I need to execute a 'git rebase master' from within my repository.

I was hoping to use perl system calls, something along the lines of: system('git rebase master'); but again, thish as to be from within my repository.

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.


FishMonger
Veteran / Moderator

Apr 21, 2015, 7:22 AM

Post #2 of 4 (1404 views)
Re: [perlFun] cd to a directory so I can make a system call [In reply to] Can't Post

Use Perl's chdir function.

See: perldoc -f chdir
http://perldoc.perl.org/functions/chdir.html


FishMonger
Veteran / Moderator

Apr 21, 2015, 7:23 AM

Post #3 of 4 (1403 views)
Re: [perlFun] cd to a directory so I can make a system call [In reply to] Can't Post

Instead of doing system calls, you could use one or more of the Git modules on cpan.
http://search.cpan.org/search?query=git&mode=all


Laurent_R
Veteran / Moderator

Apr 21, 2015, 10:34 AM

Post #4 of 4 (1398 views)
Re: [perlFun] cd to a directory so I can make a system call [In reply to] Can't Post


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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives