1

Running a perl script on linux using CaptureOutput::capture_exec_combined. It doesn't seem to want to execute "source"

#!/usr/bin/env perl
use IO::CaptureOutput qw/capture_exec_combined/;
$cmd = "source test_capout.csh";
my ($stdouterr, $success, $exit_code) = capture_exec_combined($cmd);
print  "${stdouterr}\n";

(test_capout.csh just echoes a message)

I get...

Can't exec "source": No such file or directory at /tool/pandora64/.package/perl-5.18.2-gcc481/lib/site_perl/5.18.2/IO/CaptureOutput.pm line 84.

daveg
  • 1,051
  • 11
  • 24

1 Answers1

3

source causes the named script to be executed by the shell given the source command. It makes no sense to use source outside of a shell, which is why it's not a program but a built-in shell command. You'll need to spawn a shell and have the shell execute the command.

capture_exec_combined('csh', '-c', 'source test_capout.csh');  # Hardcoded
  -or-
capture_exec_combined('csh', '-c', 'source "$1"', $script);    # Variable

Of course, since the shell exits afterwards, that can be simplified to

capture_exec_combined('csh', 'test_capout.csh');        # Hardcoded
  -or-
capture_exec_combined('csh', $script =~ s{^-}{./-}r);   # Variable
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • @ysth, woops, it got eaten when I switched to braces. Fixed – ikegami Jan 29 '18 at 01:35
  • The reason the 'test_capout.csh' script was being "sourced" is to have things like the env vars it sets persist in the current shell. If I'm not mistaken, running test_capout.csh using csh will not have the same effect. – daveg Jan 29 '18 at 01:50
  • The "current shell" is `perl`. The equivalent of `source PATH` for Perl is `do PATH`. Of course, you'll need to convert the csh commands into Perl statements. – ikegami Jan 29 '18 at 01:54