Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how to store the output of a 'system' call to a variable

by serf (Chaplain)
on Jan 04, 2006 at 10:53 UTC ( #520844=note: print w/replies, xml ) Need Help??


in reply to how to store the output of a 'system' call to a variable

If you are wanting to run a command specifically to capture its output and it gives more than a few lines you may find this way works well for you:
my $script = "./script1.pl"; open(SCRIPT1, "$script|") || die "Can't run '$script': $!\n"; while(<SCRIPT1>) { $results[0]{result} .= $_; } close(SCRIPT1);
or perhaps more flexibly:
my $script = "./script1.pl"; my @script_output; open(SCRIPT1, "$script|") || die "Can't run '$script': $!\n"; while(<SCRIPT1>) { chomp(); push (@script_output, $_); } close(SCRIPT1); $results[0]{result} = join($/, @script_output);
Both of which will report to you if there is a problem running it (with the reason why) and will put the output which you require into the variable you want to use.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://520844]
help
Chatterbox?
[beech]: thank you, it sure is pretty "code", yup, thats what I call it my ...
beech .oO( talking 'bout my ...)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2017-02-28 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (398 votes). Check out past polls.