http://qs1969.pair.com?node_id=1171405

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

I need to capture the output of a bash command in perl which I've done a million times but for some reason this specific issue is hanging me up.

What I need to do it capture the output from a git fetch --tags command and parse the results. The issue is if I run the following code

#!/usr/bin/perl use warnings; use strict; my $git = system('git fetch --tags'); print "What's here? --- $git\n";
The output of the command is displayed in terminal (which is to be expected since I haven't redirected it). However, if I run the command as followed:
my $git = system('git fetch --tags &> /dev/null');
The variable is empty (which is also expected because I am redirecting it to /dev/null). The only way I can get it to work is if I redirect to a file first as such:
git fetch --tags &> /tmp/git.txt
and then parsing the output file. I am trying to avoid having to do that if at all possible meaning I do not want an extra step of checking a tmp file, would much rather do it in one step. Help would be greatly appreciated. Thanks

********** UPDATE **********

I managed to solve this issue based on some of your suggestions using Capture::Tiny. The following code is doing exactly what I wanted it to do.

#!/usr/bin/perl use warnings; use strict; use Capture::Tiny ':all'; use Version::Compare; use Sort::Key::Natural qw( natsort ); my $GIT_HOME = '/usr/local/src/Arelle_Web/DQC_Test/'; my (@git) = capture{system("git --git-dir=${GIT_HOME}.git fetch --tags +");}; @git = grep { $_ ne '' } @git; my @tags; if(scalar(@git) > 1){ print "Tags have been updated\n"; my $string = join("\n", @git); @tags = ($string =~ m/\[new tag\]\s+(.+?)\s/g); } else { @tags = split("\n",capture{system("git --git-dir=${GIT_HOME}.git t +ag")}); print "No New Tags\n"; } @tags = natsort @tags; chomp(my $running_version = capture{system("git --git-dir=${GIT_HOME}. +git describe --tag");}); $running_version =~ s/^v//; my ($ver_major) = $running_version =~ /^(\d)/; my @version = grep(/^v$ver_major.+/,@tags); if(@version){ @version = natsort @version; my $newest = pop(@version); $newest =~ s/^v//; if(&Version::Compare::version_compare($newest,$running_version) == + 1){ print "v$newest is greater than the running version v$running_ +version\n"; } }

Thanks again for all your suggestions.