DanH
DanH

Reputation: 5818

Send perl variables to a command and capture the output

I'm attempting to pass perl variables into a system command and then capture the output for later usage, here's my current code:

my $updatedCmd = "|svn diff --summarize $svnOldFull $svnNewFull";
my $updatedUrls = '';
open UPDATES, $updatedCmd or die "Can't get updates";
while(<UPDATES>) {
  print $_;
}

print "THIS_SHOULD_OUTPUT_AT_THE_END\n";

The problem with this is that I get the output:

THIS_SHOULD_OUTPUT_AT_THE_END
A       /test
A       /test2
A       /deployment.txt

I would like to be able to capture all of the command output before allowing my perl script to go any further however.

Upvotes: 3

Views: 364

Answers (2)

ruz
ruz

Reputation: 492

More modern way to do this is the following:

   my @cmd = qw(svn diff --summarize), $svnOldFull, $svnNewFull;
   open my $pipe, '-|', @cmd or die "oops: $!";
   while (<$pipe>) { ... }

Advantages

  • no globals

  • open mode separated from file/command

  • command as array, so there is no need in shell quoting

Upvotes: 5

mitchnull
mitchnull

Reputation: 6331

You placed the pipe on the wrong end of your command. Try this:

my $updatedCmd = "svn diff --summarize $svnOldFull $svnNewFull|";

Upvotes: 3

Related Questions