Creating a piped stream and reading from it ???

Discussion in 'Perl Misc' started by surfunbear@yahoo.com, Oct 27, 2005.

  1. Guest

    This is a problem I often have. I would like to know if there
    is a good alternative to using a temporary file.

    Suppose I have:

    $cmd = "ps -ef | grep $myuserid";

    I could then do something like:


    `$cmd > $tmpfile`;

    open(TMP, $tmpfile);
    while(<TMP>)
    {
    # various complex patterns processed etc.
    ...
    ...
    }


    It seems a little messy creating a temporary file and so on.
    That's how I usually do it, but to do it right you have to
    make it unique with pid as part of the name, make sure you delete it
    when your done etc. On some machine the path to the temporary file may
    change and so on.

    What I really want to do is somehow run these piped commands from
    inside
    my script and then read the results in memory ideally or in a temporary
    file. I read you can create a temporary file with a handle, but since
    it has no name, I can't pipe to it.

    The version of perl we have at work does not have File::Temporary, but
    even if it did, I'm not sure I could get a filename recognizable by an
    execed shell, I did read the method using IO::File, but it's the same
    thing with that of course.
     
    , Oct 27, 2005
    #1
    1. Advertising

  2. J. Gleixner Guest

    wrote:
    [...]
    > What I really want to do is somehow run these piped commands from
    > inside
    > my script and then read the results in memory ideally or in a temporary
    > file. I read you can create a temporary file with a handle, but since
    > it has no name, I can't pipe to it.

    [...]

    Check the documentation.

    perldoc -f open
    "...
    open(ARTICLE, "caesar <$article |") # decrypt article
    or die "Can't start caesar: $!";
    ...."

    perldoc perlopentut
    "
    open(NET, "netstat -i -n |") || die "cannot fork: $!";
    while (<NET>) { } # do something with input
    close(NET) || die "can't close netstat: $!";
    ...."
     
    J. Gleixner, Oct 27, 2005
    #2
    1. Advertising

  3. Paul Lalli Guest

    wrote:
    > This is a problem I often have. I would like to know if there
    > is a good alternative to using a temporary file.
    >
    > Suppose I have:
    >
    > $cmd = "ps -ef | grep $myuserid";
    >
    > I could then do something like:
    >
    >
    > `$cmd > $tmpfile`;
    >
    > open(TMP, $tmpfile);
    > while(<TMP>)
    > {
    > # various complex patterns processed etc.
    > ...
    > ...
    > }


    Uhm. That makes a tremendous lack of sense. Do you understand what
    back-ticks actually do? They execute the enclosed command - *AND
    RETURN ITS OUTPUT*. You, however, are bypassing this, in favor of
    redirecting that output to a temporary file. Do less work, and get
    better results:

    my @lines = `$cmd`;
    for (@lines){
    # various complex patterns processed etc
    }

    perldoc perlop (search for 'qx')
    for more information

    Paul Lalli
     
    Paul Lalli, Oct 27, 2005
    #3
  4. Jim Gibson <> wrote:
    > In article <>,
    ><> wrote:
    >
    >> This is a problem I often have. I would like to know if there
    >> is a good alternative to using a temporary file.
    >>
    >> Suppose I have:
    >>
    >> $cmd = "ps -ef | grep $myuserid";
    >>
    >> I could then do something like:
    >>
    >>
    >> `$cmd > $tmpfile`;
    >>
    >> open(TMP, $tmpfile);
    >> while(<TMP>)
    >> {
    >> # various complex patterns processed etc.
    >> ...
    >> ...
    >> }

    >
    > Why aren't you just capturing the output of your commands in an array?:
    >
    > my @lines = `ps -ef | grep $myuserid`;
    > foreach my $line ( @lines ) {
    > # various complex patterns processed etc...
    > }



    Why aren't you just walking over the backticks list directly?

    foreach my $line ( `ps -ef | grep $myuserid` ) {

    :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 27, 2005
    #4
  5. Joe Smith Guest

    Tad McClellan wrote:
    > Jim Gibson <> wrote:
    >
    >>In article <>,
    >><> wrote:

    >
    >>>`$cmd > $tmpfile`;

    >>
    >>Why aren't you just capturing the output of your commands in an array?:
    >>
    >> my @lines = `ps -ef | grep $myuserid`;
    >> foreach my $line ( @lines ) {
    >> # various complex patterns processed etc...
    >> }

    >
    > Why aren't you just walking over the backticks list directly?
    >
    > foreach my $line ( `ps -ef | grep $myuserid` ) {


    A note to surfunbear: There is no need to execute grep.

    foreach my $line ( `ps -ef`) {
    next unless $line =~ /\b$myuserid\b/;
    ...
    }

    -Joe
     
    Joe Smith, Oct 31, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Tucker

    Piped stream help.

    Andrew Tucker, Oct 5, 2003, in forum: Java
    Replies:
    3
    Views:
    531
    Harald Hein
    Oct 6, 2003
  2. Patrick Useldinger

    reading piped input in Windows

    Patrick Useldinger, Nov 15, 2003, in forum: Python
    Replies:
    4
    Views:
    1,283
    Georgy Pruss
    Nov 16, 2003
  3. ashen_shugar

    Reading input piped from windows vim

    ashen_shugar, Jul 22, 2010, in forum: Python
    Replies:
    0
    Views:
    246
    ashen_shugar
    Jul 22, 2010
  4. Danny Challis
    Replies:
    3
    Views:
    118
    Kaspar Schiess
    Dec 30, 2010
  5. Yuri Shtil

    Reading from piped standard input on w2k

    Yuri Shtil, Jun 30, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    113
    Tad McClellan
    Jun 30, 2003
Loading...

Share This Page