0

I need to run a command with variables inside of the command. I need to capture the output and save it in either one line varible or an array, it doesn't matter. It's going to be pasted into a textbox in Tk anyways.

I've tried:

my @array = '$variable_with_command'; 

I can't really use:

my $variable = 'unixcommand $variableinside'; 

because of the variable inside, i've seen that as a suggestion other stackoverflow posts.

It works for:

my $readvariable = 'ls -a';

because there are no variables inside, unless there's a way to include variables and have $readvariable catch the output? Thanks in advance.

Here is my code (this printed zero):

sub runBatch {
    my $directory = "/directpath/directoryuser/sasmodules/";
    my $command = 'sasq ' . $directory . $selectBatch; 
    my @batch = system($command);
    print "This is the output\n";
    print "-----------------------------------";
    print @batch; 
    print "-----------------------------------";
}
Jesse Hernandez
  • 307
  • 2
  • 15
  • 2
    This doesn't have much to do with variables in the command. It has to do with the [`system`](http://search.cpan.org/perldoc?perlfunc#system) command not doing what you think it does. To capture the output of an external command, you want to use backticks, the `qx()` operator, or [`readpipe`](http://search.cpan.org/perldoc?perlfunc#readpipe). – mob Sep 27 '13 at 20:55
  • Thanks, however if it wasn't for the variables i would not have a problem. I believe using back quotes be it the right way or not, works for my issue. I am sure there are many solutions as Perl seems to have. – Jesse Hernandez Sep 27 '13 at 21:26

1 Answers1

3

This is the answer in case it helps anyone based on qx comment.

sub runBatch { 
    my @batch = qx(sasq $director);
    print "This is the output\n";
    print "-----------------------------------\n";
    print @batch; 
    print "-----------------------------------\n";
}

This actually works really well--using back quotes: `

sub runBatch {
    my $directory = "/path/directory/sasmodules/" . $selectBatch;
    my $command = `sasq $directory`; 
    #my @batch = command
    print "This is the output\n";
    print "-----------------------------------\n";
    print $command; 
    print "-----------------------------------\n";
    print $selectBatch;
}
Jesse Hernandez
  • 307
  • 2
  • 15