Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

storing system function output

by karthigayan (Initiate)
on Mar 10, 2010 at 09:45 UTC ( #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 .

Comment on storing system function output
Re: storing system function output
by Ratazong (Prior) on Mar 10, 2010 at 09:50 UTC
      I know that , but I just wanna know is there any way to achieve this using system function , please answer specifically!
        is there any way to achieve this using system function, please answer specifically!

        No.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        So after being told that system is not the right way to do this you still insist that it's what you want to use. And "specifically", too. Fine, here you go:

        #!/usr/bin/perl use strict; use warnings; my $r=`perl -e 'system("ls")'`;

        That uses system and will make your tutor give you 0 points for the exercise along the way.


        All dogma is stupid.
Re: storing system function output
by marto (Bishop) 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
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? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-12-28 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls