Prev: How to generate random number without replacement?
Next: FAQ 4.76 How do I verify a credit card checksum?
From: Peter Makholm on 1 Jun 2010 05:40 Peng Yu <pengyu.ut(a)gmail.com> writes: > 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) makholm(a)makholm:/tmp$ perl ./example.pl <(cat example.pl) <(tr 'a-zA-Z' 'n-za-mN-ZA-M' < example.pl) 001: #!/usr/bin/perl 001: 001: use strict; 001: use warnings; 001: 001: my $fileno; 001: for my $file (@ARGV) { 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: sbe zl $svyr (@NETI) { 002: $svyrab++; 002: bcra zl $su, "<", $svyr 002: be arkg; 002: 002: cevags "%03q: %f", $svyrab, $_ juvyr <$su>; 002: } 002: 002: __RAQ__ makholm(a)makholm:/tmp$ //Makholm
From: John Bokma on 1 Jun 2010 09:48 Peng Yu <pengyu.ut(a)gmail.com> writes: > $ cat main.pl > #!/usr/bin/env perl Also add this one: use strict; > 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
From: Martijn Lievaart on 1 Jun 2010 13:22 On Tue, 01 Jun 2010 02:26:59 -0700, Peng Yu wrote: > On Jun 1, 3:24 am, Martijn Lievaart <m...(a)rtij.nl.invlalid> wrote: >> On Mon, 31 May 2010 20:47:22 -0700, Peng Yu wrote: >> > 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. [martijn(a)cow t]$ perl -e 'print "@ARGV\n"' <(cat t.pl) <(cat t.pl~) /proc/self/fd/63 /proc/self/fd/62 [martijn(a)cow t]$ HTH, M4
From: C.DeRykus on 2 Jun 2010 06:35 On Jun 1, 2:26 am, Peng Yu <pengyu...(a)gmail.com> wrote: > On Jun 1, 3:24 am, Martijn Lievaart <m...(a)rtij.nl.invlalid> wrote: > .... > > #!/usr/bin/env perl > > use warnings; > > open(IN1, $ARGV[0]); > open(IN2, $ARGV[1]); > > while(<IN1>) { > > } > > print "------\n"; > > while(<IN2>) { > > } > > 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
From: chad on 2 Jun 2010 11:08 On Jun 1, 10:22 am, Martijn Lievaart <m...(a)rtij.nl.invlalid> wrote: > On Tue, 01 Jun 2010 02:26:59 -0700, Peng Yu wrote: > > On Jun 1, 3:24 am, Martijn Lievaart <m...(a)rtij.nl.invlalid> wrote: > >> On Mon, 31 May 2010 20:47:22 -0700, Peng Yu wrote: > >> > 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. > > [martijn(a)cow t]$ perl -e 'print "@ARGV\n"' <(cat t.pl) <(cat t.pl~) > /proc/self/fd/63 /proc/self/fd/62 > [martijn(a)cow t]$ > Why do you use the brackets in '<(cmd)'? Ie, why can't you just do something like '<cmd' ?
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How to generate random number without replacement? Next: FAQ 4.76 How do I verify a credit card checksum? |