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

This is probably a simple question but how do I execute a command from a diferent directory, other than the parent directory? (ie CD /Foo run bar.exe)

Replies are listed 'Best First'.
Re: CD and EXE
by mrbbking (Hermit) on May 29, 2002 at 16:32 UTC
    Use the full path to 'bar.exe'
    system( `/Foo/bar.exe` );
Re: CD and EXE
by cjf (Parson) on May 29, 2002 at 16:18 UTC
      Sorry, I should have been more clear. I want to cd to a differnt directory and start a command from there all in one line (ie system "cd /foo $bar.exe";)

        On Unix, you can separate the two commands by a semi-colon system("cd /foo; $bar.exe");. It would probably be better to use chdir or the full path as suggested since using the semi-colon in that command doesn't take into account what could happen if the "cd" fails (nor is it cross-platform compatible if the need arises). I would suggest using something like:

        #!/usr/bin/perl use strict; use warnings; use Cwd qw/chdir/; # comes in standard Perl distro my $bar = "someprogramname"; chdir("/foo") or die "Couldn't cd to /foo!\n"; system("$bar\.exe") or die "Couldn't execute $bar\.exe!\n";

        Rich36

        Using the full path like so: qx('/usr/bin/perl'); should do the trick.