Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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,

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
      I know that , but I just wanna know is there any way to achieve this using system function , please answer specifically!

        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.
        is there any way to achieve this using system function, please answer specifically!


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

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://827734]
Approved by Corion
and the monks are mute...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-06-28 20:15 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (646 votes). Check out past polls.