perl PIPE - need an example of piping in

S

seth

My question is about pips.

I have the following code:


EXAMPLE 1:
#-----------------------------------------------------------
#!/usr/bin/perl -w

use strict;

my $command = "ls -al";
my @failureMsg;

open(OUTPUT, "$command |") or die "can't do the command\n";

my $lines = <OUTPUT>;

@failureMsg = grep(/\ssp.*\.pl$/, <OUTPUT>);

print "@failureMsg";
#-----------------------------------------------------------



In this code I'm piping my the output of my command to a file
handle(FH).

I understand this and am happy with it.

What I don't understand is the following syntax:


EXAMPLE 2:
open(OUTPUT, " | $command") or die "can't do the command\n";


**note: the example of what I don't understand has no realtion to my
code snippet above. My example 2 might not be ideal.. but here is what
I don't undestand.


Example 1 shows how to "pipe" *into* a FileHandle.

So is doing something like Example 2 piping *from* a FileHandle?

Can someone point me to a quick simple example. I've checked most of
the Orielly series and can't find an example to help me understand what
the meaning is for prepnding "$command" with the pipe char.

-thanks
seth
 
S

seth

okay so I didn't look carefully I did find the following examples in
the "FileHandle" section of the "programming perl" book

open(SESAME, "filename") # read from existing file
open(SESAME, "<filename") # (same thing, explicitly)
open(SESAME, ">filename") # create file and write to it
open(SESAME, ">>filename") # append to existing file
open(SESAME, "| output-pipe-command") # set up an output filter
open(SESAME, "input-pipe-command |") # set up an input filter

... umm but I still don't know to use this.. a little help please?


-tks
seth
 
P

Paul Lalli

seth said:
okay so I didn't look carefully I did find the following examples in
the "FileHandle" section of the "programming perl" book

open(SESAME, "filename") # read from existing file
open(SESAME, "<filename") # (same thing, explicitly)
open(SESAME, ">filename") # create file and write to it
open(SESAME, ">>filename") # append to existing file
open(SESAME, "| output-pipe-command") # set up an output filter
open(SESAME, "input-pipe-command |") # set up an input filter

.. umm but I still don't know to use this.. a little help please?

Seth,

You may want to check out some of the built in perl documentation:
perldoc -f open
perldoc perlopentut

In the mean time, here are two quick examples that show how pipes are
used both for output and input:

#!/usr/bin/perl
use strict;
use warnings;

open my $input, 'ls -al |' or die "Cannot do ls: $!";
#equivalent:
#open my $input, '-|', 'ls -al' or die "Cannot do ls: $!";
while (my $line = <$input>){
print $line;
}
close $input;

open my $output, '| grep foo' or die "Cannot start grep: $!";
#equivalent:
#open my $output, '|-', 'grep foo' or die "Cannot start grep: $!";
print $output "foo bar baz\n";
print $output "hello world\n";
print $output "I want food\n";
close $output;

__END__


In the first block, we open the 'ls' command (with -al as parameters),
and pipe its output to the $input handle. We can then from that handle
as though it was any other ordinary file handle opened for reading.
When ls has given us all of its output, the read returns a false value,
and the while loop terminates.

In the second block, we open the 'grep' command (with 'foo' as a
parameter), and pipe its input from the $output handle. We can then
print to $output as though it were any other ordinary file handle
opened for writing. Once we have printed all the text we want to be
used as input to 'grep', we close the file handle, and the 'grep'
program then runs, displaying its output on the terminal (because we
didn't redirect it anywhere else.

Hope this helps,
Paul Lalli
 
X

xhoster

seth said:
okay so I didn't look carefully I did find the following examples in
the "FileHandle" section of the "programming perl" book

open(SESAME, "filename") # read from existing file
open(SESAME, "<filename") # (same thing, explicitly)
open(SESAME, ">filename") # create file and write to it
open(SESAME, ">>filename") # append to existing file
open(SESAME, "| output-pipe-command") # set up an output filter
open(SESAME, "input-pipe-command |") # set up an input filter

.. umm but I still don't know to use this.. a little help please?

Is the problem that you don't know how to use, or that you don't know why
you would want to use it?


open my $fh, "|gzip > zipped_output.gz" or die $!;
while (whatever()) {
#stuff goes here;
print $fh $my_output;
};

close $fh or die $!;

Xho
 
T

Tad McClellan

seth said:
My question is about pips.


It is actually about the open() function.

It appears that you have not read the documentation for the function
that you are using, that should have been the *first* place you looked.

Posting to Usenet should be the *last* place you look. Why take the
time of hundreds/thousands of your fellow programmers when a few
minutes of docs searching can solve the problem?

my @failureMsg;


You should limit the scope of variables to the smallest possible scope,
so delete that line and add "my " in front of your 1st usage of @failureMsg.

open(OUTPUT, "$command |") or die "can't do the command\n";

perldoc -f open

...
if the filename ends with a '|', the filename is interpreted
as a command which pipes output to us


So the OUTPUT filehandle is open for *reading* by your Perl program.

You are doing input on an fh named output, that could be confusing,
"LS_AL" would have been a much more meaningful choice of filehandle name.

my $lines = <OUTPUT>;


Here you read a _single_ line into a variable with a _plural_ name, that
could be confusing too.

@failureMsg = grep(/\ssp.*\.pl$/, <OUTPUT>);
print "@failureMsg";


What does the output from that statement look like?

Is there an extra space at the beginning of every line except the first?

If so,

perldoc -q spaces

Why do I get weird spaces when I print an array of lines?

What I don't understand is the following syntax:
open(OUTPUT, " | $command") or die "can't do the command\n";


perldoc -f open

If the filename begins with C<'|'>, the filename is interpreted
as a command to which output is to be piped
...

So the OUTPUT filehandle is open for *writing* by your Perl program.

Can someone point me to a quick simple example.

perldoc -f open

...
See perlipc/"Using open() for IPC" for more examples of this.

perlipc.pod shows code using pipe opens in each direction.

I've checked most of
the Orielly series


The first place to look for the behavior of a function is in the docs
that came with the software that you are using.

Books are nice backups, but they are not the primary resource.

Books are out of date before they even get to the bookstore.

Perl's std docs are updated each time perl itself is updated.

and can't find an example to help me understand what
the meaning is for prepnding "$command" with the pipe char.


Read the documentation for the function if you want to know what
the function is supposed to do.
 
S

seth

O this 2nd example with the output handle is so slick.. This is exacly
what I wanted.

So then with a output handle I can pass args to the command grep - in
this case. With the 3 examples you've given there will only be two
match with the first and the second print that you do.
 
S

seth

The problem is that I don't know how / where I should use the outbound
pipe to a handle. The examples provided here.. really help....

tks
seth
 
S

seth

Oh boy a lecture.. I really am sorry to take up time from the people
that responded. But I did spend time looking, where I thought were the
right places for accurate info to address my confusion.

However I do appreciate *ALL* the comments here. So I will make more
of an effort to search the perldocs first next time around.

-thanks everyone

seth
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top