-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.

Buddy
  • 10,874
  • 5
  • 41
  • 58
driedupsharpie
  • 133
  • 2
  • 10

2 Answers2

6

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

my $output = qx{echo schwifty};
die unless $output =~ /schw/;
choroba
  • 231,213
  • 25
  • 204
  • 289
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";
}
thonnor
  • 1,206
  • 2
  • 14
  • 28