Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

storing system function output

by karthigayan (Initiate)
on Mar 10, 2010 at 09:45 UTC ( [id://827734]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks ,
I have a doubt in system function.I want to store the system functions output to a variable.
For example,
system("ls");

Here I want all the file names in the current directory to store in a variable.I know that I can do this by redirecting the output into a file and read from that and store that to a variable.But I want a efficient way than that.Is there any way . Thanks in advance .

Replies are listed 'Best First'.
Re: storing system function output
by Ratazong (Monsignor) on Mar 10, 2010 at 09:50 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: storing system function output
by marto (Cardinal) on Mar 10, 2010 at 09:57 UTC

    You've posted in the wrong section off the forum, read Where should I post X?, as suggested every time you post something.

Re: storing system function output
by wwe (Friar) on Mar 10, 2010 at 16:13 UTC
    Hi, take a look at IPC::Run3 (http://search.cpan.org/~rjbs/IPC-Run3-0.043/lib/IPC/Run3.pm). This module allows you to run processes and redirect/capture thier output.
Re: storing system function output
by doug (Pilgrim) on Mar 12, 2010 at 14:58 UTC

    system doesn't glue in STDIN/STDOUT/STDERR, so you'll have to do that on your own. Generally use the open with a bar (pipe character) to launch programs where you need to capture the output. Try RTFM:

    perldoc -f open

    I don't think I'd ever launch ls as a child process because it is too easy to get that information on your own. opendir and readdir work will do nearly anything that you need for filenames. And for this particular case, there is a specialised tool for that:

    my @files = glob("/path/to/directory/*");

    The docs for opendir, readdir, closedir and glob are all in perlfunc so you can use perldoc -f OP to view them.

    - doug

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://827734]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found