Creating a piped stream and reading from it ???

S

surfunbear

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.
 
J

J. Gleixner

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: $!";
...."
 
P

Paul Lalli

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
 
T

Tad McClellan

Jim Gibson said:
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` ) {

:)
 
J

Joe Smith

Tad said:
Jim Gibson said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top