http://www.perlmonks.org?node_id=542820


in reply to Specifying a shell for use with Backticks

First I want to thank everybody for the comments.

We know for certain that it is picking up the .cshrc settings because when we remove the PATH statements all of the alternate commands are working properly, and hence my assumption that they are using csh when backticking commands in perl. Unfortunately some of the people here running the perl scripts need to have both special PATH and ENVIRONMENT settings for the perl tools as well as other tools, and they both conflict with each other. It is very much a chicken and egg problem.

Since we are getting both sets of tools from a corporate source repository, and we really are not at liberty to make changes to them that is why I was just hoping for a quick one change of code to change the backtick behavior that I could send of to HQ and hope that it would get implemented some day. Even though using the IPC modules is a good alternative, I doubt I'll be able to get somebody to change the implementation under the hood of these scripts.

Again, thank you all for posting. Even though I cannot do much in this case I will definitely check out IPC::Open3 and IPC::Run at least for my own scripts. Given how touch and go the environment is here I don't want to be adding to this very huge mess.

  • Comment on Re: Specifying a shell for use with Backticks

Replies are listed 'Best First'.
Re^2: Specifying a shell for use with Backticks
by bluto (Curate) on Apr 12, 2006 at 15:29 UTC
    If you really can't get backticks to use a reasonable shell, perhaps you can trick it. For example in ksh, you can do something like this...

    my $output = `PATH=$ENV{PATH} command arg1 arg2 argn`;

    ... which sets PATH just for the command. Just be careful you know what is in $ENV{PATH} (e.g. no spaces; not user supplied). I don't know if this syntax will work for csh also, so perhaps something ugly like...

    my $output = `setenv PATH "$ENV{PATH}"; command arg1 arg2 argn`;

    An alternative, if you are only worred about the PATH, might be to supply a fullpath to backticks (i.e. dont give the shell a chance to locate it - just tell it)...

    my ($cp) = grep { -x $_ } map { "$_/cp" } grep { length $_ } split /:/ +, $ENV{PATH}; die "cant find cp" unless defined $cp; my $output = `$cp source dest`;

    IMO these seem like band-aids for the real problem. Using csh is just unsafe, outside of an interactive shell.