system("pushd $dir") doesn't work

J

jrwats

Can this not work within a perl script? I'm running the program from
a windows cmd.exe command prompt.

also tried `pushd $dir`
and system("pushd", $dir);
 
J

Jürgen Exner

jrwats said:
Can this not work within a perl script? I'm running the program from
a windows cmd.exe command prompt.

also tried `pushd $dir`
and system("pushd", $dir);

Of course it works, it's just not very useful.
Your code (any of the versions) creates a new process, runs pushd in
that process, and then terminates the process, thus throwing away the
effects of pushd.

What were you actually trying to achieve with that code?

jue
 
T

Tim Greer

jrwats said:
Can this not work within a perl script? I'm running the program from
a windows cmd.exe command prompt.

also tried `pushd $dir`
and system("pushd", $dir);

Can you explain how this isn't working for you? Can you show the
relevant portion of the code you're using this within and what you are
trying to accomplish? Have you considered looking at the File::pushd
module? (I think there is one, but I could be wrong? If so, disregard
that portion of my reply).
 
J

jrwats

Can you explain how this isn't working for you?  

It has to do with creating a new process and not affecting the
environment I'm running it from. Sounds like that simply can't be
acheived. I have a workaround for it anyhow.
 
H

Hans Mulder

Jürgen Exner said:
Of course it works, it's just not very useful.

It doesn't work if $dir does not contain any shell meta-characters.
In that case, perl will bypass the shell and try to find 'pushd' in
$ENV{PATH} itself. This fails, because 'pushd' is a shell built-in
and does not exist in $ENV{PATH}.

The situation is different for the 'cd' command (at least, on Posix-
compliant systems). 'Cd' is also a shell built-in (it has to be), but
it also exists as a binary in /usr/bin/. The authors of the Posix
standard felt that script writers might want to know if they could do
chdir($dir) if they wanted to. The Perl way to do this, would be to
try it and look at the value returned by the chdir() command.
Posix provides an alternative: you can do system("cd $dir"). This
will create a new process. This new process will try to do chdir($dir).
If this succeeds, $? is set 0 in the parent process, else it's set to a
non-zero value.

Hope this helps,

-- HansM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top