How to take two input streams? 
Author Message
 How to take two input streams?

diff can take two input streams in the following example (if my
interpretation is correct).

diff <(gunzip <a.gz) <(gunzip b.gz)

I'm wondering how to take two streams in a perl program.



Sat, 17 Nov 2012 04:47:22 GMT  
 How to take two input streams?


Quote:
>diff can take two input streams in the following example (if my
>interpretation is correct).

diff *requires* two input streams. With only one, it's hard to figure what it
would do.

Quote:

>diff <(gunzip <a.gz) <(gunzip b.gz)

>I'm wondering how to take two streams in a perl program.

The <(cmd) syntax is handled by your shell, which substitutes a filename
which may be opened to gain access to the stream. diff doesn't know anything
abou it. diff just finds 2 filenames in argv, opens them, and reads them. You
can do the same in any language, including perl.

--
Alan Curry



Sat, 17 Nov 2012 05:12:26 GMT  
 How to take two input streams?

Quote:

> diff can take two input streams in the following example (if my
> interpretation is correct).

> diff <(gunzip <a.gz) <(gunzip b.gz)

> I'm wondering how to take two streams in a perl program.

This has nothing to do with diff or with perl, it's a function of your
shell. So it works the same for diff as for perl.

HTH,
M4



Sat, 17 Nov 2012 09:24:03 GMT  
 How to take two input streams?

Quote:

> > diff can take two input streams in the following example (if my
> > interpretation is correct).

> > diff <(gunzip <a.gz) <(gunzip b.gz)

> > I'm wondering how to take two streams in a perl program.

> This has nothing to do with diff or with perl, it's a function of your
> shell. So it works the same for diff as for perl.

I don't quite understand how this works. Would you please write a
small perl program which can print the two streams (with the following
command) to help me understand it?

example.pl <(cat a.txt) <(cat b.txt)



Sat, 17 Nov 2012 10:16:25 GMT  
 How to take two input streams?

Quote:

> > diff can take two input streams in the following example (if my
> > interpretation is correct).

> > diff <(gunzip <a.gz) <(gunzip b.gz)

> > I'm wondering how to take two streams in a perl program.

> This has nothing to do with diff or with perl, it's a function of your
> shell. So it works the same for diff as for perl.

I think that I understand what you mean. <(cmd) is just like a
filename, right?

$ cat main.pl
#!/usr/bin/env perl

use warnings;

open(IN1, $ARGV[0]);
open(IN2, $ARGV[1]);

while(<IN1>) {
  print

Quote:
}

print "------\n";

while(<IN2>) {
  print

Quote:
}

$ ./main.pl <(cat main.pl) <(cat main.pl)
#!/usr/bin/env perl

use warnings;

open(IN1, $ARGV[0]);
open(IN2, $ARGV[1]);

while(<IN1>) {
  print

Quote:
}

print "------\n";

while(<IN2>) {
  print

Quote:
}

------
#!/usr/bin/env perl

use warnings;

open(IN1, $ARGV[0]);
open(IN2, $ARGV[1]);

while(<IN1>) {
  print

Quote:
}

print "------\n";

while(<IN2>) {
  print

Quote:
}



Sat, 17 Nov 2012 10:26:59 GMT  
 How to take two input streams?

Quote:

> I don't quite understand how this works. Would you please write a
> small perl program which can print the two streams (with the following
> command) to help me understand it?

> example.pl <(cat a.txt) <(cat b.txt)


001: #!/usr/bin/perl
001:
001: use strict;
001: use warnings;
001:
001: my $fileno;

001:     $fileno++;
001:     open my $fh, "<", $file
001:         or next;
001:
001:     printf "%03d: %s", $fileno, $_ while <$fh>;
001: }
001:
001: __END__
002: #!/hfe/ova/crey
002:
002: hfr fgevpg;
002: hfr jneavatf;
002:
002: zl $svyrab;

002:     $svyrab++;
002:     bcra zl $su, "<", $svyr
002:         be arkg;
002:
002:     cevags "%03q: %f", $svyrab, $_ juvyr <$su>;
002: }
002:
002: __RAQ__

//Makholm



Sat, 17 Nov 2012 10:40:59 GMT  
 How to take two input streams?

Quote:

> $ cat main.pl
> #!/usr/bin/env perl

Also add this one:

use strict;

Quote:
> use warnings;
> open(IN1, $ARGV[0]);

Use the 3 argument version of open, and it's often a very good idea to
report if the file actually couldn't be opened for reading:

open my $fh, '<', $ARGV[0]
    or die "Can't open '$ARGV[0]' for reading: $!";
                                               ^--- explains why

--
John Bokma                                                               j3b

Hacking & Hiking in Mexico -  http://johnbokma.com/
http://castleamber.com/ - Perl & python Development



Sat, 17 Nov 2012 14:48:02 GMT  
 How to take two input streams?

Quote:



>> > diff can take two input streams in the following example (if my
>> > interpretation is correct).

>> > diff <(gunzip <a.gz) <(gunzip b.gz)

>> > I'm wondering how to take two streams in a perl program.

>> This has nothing to do with diff or with perl, it's a function of your
>> shell. So it works the same for diff as for perl.

> I think that I understand what you mean. <(cmd) is just like a filename,
> right?

It actually gets passed to your program as a filename, although it really
is a pipe to the command between the brackets.


/proc/self/fd/63 /proc/self/fd/62

HTH,
M4



Sat, 17 Nov 2012 18:22:06 GMT  
 How to take two input streams?

Quote:

...

> #!/usr/bin/env perl

> use warnings;

> open(IN1, $ARGV[0]);
> open(IN2, $ARGV[1]);

> while(<IN1>) {
> ? print

> }

> print "------\n";

> while(<IN2>) {
> ? print

> }

Perl provides a handy command line shortcut
if that's all you need (perldoc perlrun):

perl -pwe 'print "------\n" if eof' file1 file2 ...

--
Charles DeRykus



Sun, 18 Nov 2012 11:35:40 GMT  
 How to take two input streams?

Quote:



> >> > diff can take two input streams in the following example (if my
> >> > interpretation is correct).

> >> > diff <(gunzip <a.gz) <(gunzip b.gz)

> >> > I'm wondering how to take two streams in a perl program.

> >> This has nothing to do with diff or with perl, it's a function of your
> >> shell. So it works the same for diff as for perl.

> > I think that I understand what you mean. <(cmd) is just like a filename,
> > right?

> It actually gets passed to your program as a filename, although it really
> is a pipe to the command between the brackets.


> /proc/self/fd/63 /proc/self/fd/62


Why do you use the brackets in '<(cmd)'? Ie, why can't you just do
something like '<cmd' ?


Sun, 18 Nov 2012 16:08:13 GMT  
 How to take two input streams?

Quote:




>>>>> diff can take two input streams in the following example (if my
>>>>> interpretation is correct).

>>>>> diff<(gunzip<a.gz)<(gunzip b.gz)

>>>>> I'm wondering how to take two streams in a perl program.

>>>> This has nothing to do with diff or with perl, it's a function of your
>>>> shell. So it works the same for diff as for perl.

>>> I think that I understand what you mean.<(cmd) is just like a filename,
>>> right?

>> It actually gets passed to your program as a filename, although it really
>> is a pipe to the command between the brackets.


>> /proc/self/fd/63 /proc/self/fd/62

> Why do you use the brackets in '<(cmd)'? Ie, why can't you just do
> something like '<cmd' ?

Because the shell would look for a data file named 'cmd' in the current
directory and would not execute it as a command.

$ wc -l <ls
-bash: ls: No such file or directory

$ wc -l <(ls)
     105 /dev/fd/63

--
RGB



Sun, 18 Nov 2012 16:55:53 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. : Taking input

2. Working with two streams at once: ack pth!

3. One output stream to two dests

4. Streaming Media Streams

5. Input text strings from two separate files into a single file

6. Controlling TWO input files (merging)

7. Comparison of two fields in two text files

8. passing two arrays two a sub

9. Combining two files into a two-column file

10. How to check for Null input or spaced input

11. User input vs. machine input for validation

12. Is there symbols in input stream ? (question to coraperl guru)

 

 
Powered by phpBB® Forum Software