-1

For example if my script was the following:

#! /bin/perl
use strict;
use warnings;

system('echo schwifty')

-how could I get the output on my terminal (schwifty) into a string that I could continue on with my script using?

NOTE: "echo" is just given as an example I'm using other commands that print to the screen.

0

2 Answers 2

6

To capture output of an external command, use backticks or qx:

my $output = qx{echo schwifty};
die unless $output =~ /schw/;
Sign up to request clarification or add additional context in comments.

Comments

0

For multi-line system calls such as ls I tend to use an @array to store the output. More flexible than a variable when there are lots of lines to process.

Very similar to choroba's example but expanded a little further.

my @output = qx{ ls -l };

foreach my $line ( @output )
{
    # process each $line here
    print "$line";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.