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

cd ../../;. setup-env This code works from the command line. how to add this in perl script

Replies are listed 'Best First'.
Re: Command line works
by Corion (Patriarch) on Mar 04, 2009 at 08:27 UTC

    What part of the answers you got to Run shell script in perl do you have problems with? I expect people to read and understand the answers I give.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Command line works
by lakshmananindia (Chaplain) on Mar 04, 2009 at 05:56 UTC

    Use system("cd ../../; . setup-env")

    But remember that this cd won't affect other processes.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Command line works
by eye (Chaplain) on Mar 04, 2009 at 06:20 UTC
    Consider what we want to do:

    cd ../../;. setup-env

    In this snippet, the "cd" only serves to locate the file "setup-env". The dot (".") says to read the file and execute its commands in the current shell. Since we don't have a current shell, we can specify one explicitly ("sh" in this case):

    my $retval = qx{ sh < ../../setup-env };

    or use the default shell:

    my $retval = qx{ . ../../setup-env };

    Keep in mind that the default shell may not be your login shell.

Re: Command line works
by Your Mother (Archbishop) on Mar 04, 2009 at 06:07 UTC

    The following example only covers the first half of your requirement and you'll probably get already got more direct answers since Perl evolved from the shell and they will probably be fine but there are advantages to doing it quite formally. This is how I do things like that now-

    use strict; use Cwd "cwd"; use Path::Class (); use File::Spec (); my $dir = Path::Class::Dir->new( File::Spec->rel2abs( cwd() ) ); print "OH HAI, IZ IN $dir!\n"; my $target = eval { $dir->parent->parent } or die "Couldn't find the grandparent of '$dir': $@"; chdir($target) or die "Couldn't chdir($target)"; my $cwd = cwd(); print "OH HAI AGIN, IZ IN $cwd!\n";

    It's more verbose and more complex (needs three modules) but it is (I think) more robust and leaves open many avenues and offers ancillary power-tools for working with dirs and files. It's also easier to debug and put in sensible tests.

Re: Command line works
by ig (Vicar) on Mar 04, 2009 at 08:24 UTC

    I am guessing from the name and the way you run it from the command line that setup-env is a script that sets environment variables. When you run it from your perl script, a new process will be started to run a shell to interpret your script. The environment variables will be set in this shell which will then exit. No environment variables will be set in your perl process.

    If you want the environment variables set in your perl process, you can either run setup-env before starting perl or within your perl program you can set environemnt variables using %ENV.

Re: Command line works
by boom (Scribe) on Mar 04, 2009 at 06:05 UTC

    You can also do like this `cd ../../;. setup-env`