Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl PIPE - need an example of piping in

Reply
Thread Tools

perl PIPE - need an example of piping in

 
 
seth
Guest
Posts: n/a
 
      06-10-2005
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

 
Reply With Quote
 
 
 
 
seth
Guest
Posts: n/a
 
      06-10-2005
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-10-2005
seth wrote:
> 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

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-10-2005
"seth" <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-10-2005
seth <(E-Mail Removed)> wrote:

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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
seth
Guest
Posts: n/a
 
      06-11-2005
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.

 
Reply With Quote
 
seth
Guest
Posts: n/a
 
      06-11-2005
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

 
Reply With Quote
 
seth
Guest
Posts: n/a
 
      06-11-2005
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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-11-2005
seth <(E-Mail Removed)> wrote:

> Oh boy a lecture.



So long!


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Aspell, perl and piping Bill H Perl Misc 1 03-27-2008 02:22 AM
[named pipe] i wanna know about validate of pipe handle of client lee, wonsun C++ 1 11-02-2004 04:29 AM
c++ named pipe example rajnish C++ 5 10-25-2004 08:29 AM
Piping ping into perl-prog bernd wegener Perl 3 09-22-2004 06:32 PM
Why does IO::Pipe::END generate an EXCEPT pipe message? lvirden@gmail.com Perl Misc 1 06-02-2004 02:17 PM



Advertisments