Prev: FAQ 3.14 How can I write a GUI (X, Tk, Gtk, etc.) in Perl?
Next: FAQ 1.13 Is it a Perl program or a Perl script?
From: John Kelly on 25 Jun 2010 21:28 #!/usr/bin/perl use strict; use warnings; my $data; while (<>) { chomp; if (!$data && $_) { $data = $_; } } print "data=$data\n"; This code reads STDIN and remembers the first non-empty line. That's all it cares about. But it also keeps reading till EOF, acting like the "cat" utility, to flush the extra input and avoid broken pipe errors. But reading line by line, just to throw away the unwanted garbage, is inefficient. I would like to jump out of the loop and "bulk flush" the remaining input stream. I don't think >$io->flush >flush causes perl to flush any buffered data at the perlio >api level. Any unread data in the buffer will be discarded, will work, because that it flushes the buffer, not the entire input stream. I want to flush the whole file. And keep in mind, I don't want a broken pipe either. Suggestions? -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: Ben Morrow on 25 Jun 2010 22:21 Quoth John Kelly <jak(a)isp2dial.com>: > > #!/usr/bin/perl > > use strict; > use warnings; > > my $data; > > while (<>) { > chomp; > if (!$data && $_) { > $data = $_; > } > } > > print "data=$data\n"; > > > This code reads STDIN and remembers the first non-empty line. That's > all it cares about. No, it remembers the first line that doesn't evaluate to boolean false. Since you are chomping the lines, a line containing only "0" will be considered 'empty'. You want to check length $data. > But it also keeps reading till EOF, acting like the "cat" utility, to > flush the extra input and avoid broken pipe errors. > > But reading line by line, just to throw away the unwanted garbage, is > inefficient. I would like to jump out of the loop and "bulk flush" the > remaining input stream. > > I don't think > > >$io->flush > > >flush causes perl to flush any buffered data at the perlio > >api level. Any unread data in the buffer will be discarded, > > will work, because that it flushes the buffer, not the entire input > stream. I want to flush the whole file. And keep in mind, I don't want > a broken pipe either. > > Suggestions? my $data; while (<>) { chomp; length or next; $data = $_; last; } { local $/ = \2048; 1 while <>; } Ben
From: John Kelly on 25 Jun 2010 22:47 On Sat, 26 Jun 2010 03:21:08 +0100, Ben Morrow <ben(a)morrow.me.uk> wrote: > >Quoth John Kelly <jak(a)isp2dial.com>: >> >> #!/usr/bin/perl >> >> use strict; >> use warnings; >> >> my $data; >> >> while (<>) { >> chomp; >> if (!$data && $_) { >> $data = $_; >> } >> } >> >> print "data=$data\n"; >> >> >> This code reads STDIN and remembers the first non-empty line. That's >> all it cares about. > >No, it remembers the first line that doesn't evaluate to boolean false. >Since you are chomping the lines, a line containing only "0" will be >considered 'empty'. You want to check length $data. Yeah, shot myself in the foot again. >> But it also keeps reading till EOF, acting like the "cat" utility, to >> flush the extra input and avoid broken pipe errors. >> >> But reading line by line, just to throw away the unwanted garbage, is >> inefficient. I would like to jump out of the loop and "bulk flush" the >> remaining input stream. >> >> I don't think >> >> >$io->flush >> >> >flush causes perl to flush any buffered data at the perlio >> >api level. Any unread data in the buffer will be discarded, >> >> will work, because that it flushes the buffer, not the entire input >> stream. I want to flush the whole file. And keep in mind, I don't want >> a broken pipe either. >> >> Suggestions? > > my $data; > while (<>) { > chomp; > length or next; > $data = $_; > last; > } > { > local $/ = \2048; > 1 while <>; > } > >Ben That looks interesting, I get the first part, the rest will give me something to chew on ... Thanks. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: C.DeRykus on 26 Jun 2010 01:08 On Jun 25, 7:47 pm, John Kelly <j...(a)isp2dial.com> wrote: > On Sat, 26 Jun 2010 03:21:08 +0100, Ben Morrow <b...(a)morrow.me.uk> wrote: > > >Quoth John Kelly <j...(a)isp2dial.com>: > > >> #!/usr/bin/perl > > >> use strict; > >> use warnings; > > >> my $data; > > >> while (<>) { > >> chomp; > >> if (!$data && $_) { > >> $data = $_; > >> } > >> } > > >> print "data=$data\n"; > > >> This code reads STDIN and remembers the first non-empty line. That's > >> all it cares about. > > >No, it remembers the first line that doesn't evaluate to boolean false. > >Since you are chomping the lines, a line containing only "0" will be > >considered 'empty'. You want to check length $data. > > Yeah, shot myself in the foot again. > > > > >> But it also keeps reading till EOF, acting like the "cat" utility, to > >> flush the extra input and avoid broken pipe errors. > > >> But reading line by line, just to throw away the unwanted garbage, is > >> inefficient. I would like to jump out of the loop and "bulk flush" the > >> remaining input stream. > > >> I don't think > > >> >$io->flush > > >> >flush causes perl to flush any buffered data at the perlio > >> >api level. Any unread data in the buffer will be discarded, > > >> will work, because that it flushes the buffer, not the entire input > >> stream. I want to flush the whole file. And keep in mind, I don't want > >> a broken pipe either. > > >> Suggestions? > > > my $data; > > while (<>) { > > chomp; > > length or next; > > $data = $_; > > last; > > } > > { > > local $/ = \2048; > > 1 while <>; > > } > > >Ben > > That looks interesting, I get the first part, the rest will give me > something to chew on ... > But, this'll just read/toss 2048 byte chunks till EOF. Alternatively, if you want to toss the entire stream after exiting the loop, a single statement: <>; does what you want due to the list context. I suspect there's little to gain by changing $/. -- Charles DeRykus
From: C.DeRykus on 26 Jun 2010 02:05
On Jun 25, 10:08 pm, "C.DeRykus" <dery...(a)gmail.com> wrote: > On Jun 25, 7:47 pm, John Kelly <j...(a)isp2dial.com> wrote: > > > > > On Sat, 26 Jun 2010 03:21:08 +0100, Ben Morrow <b...(a)morrow.me.uk> wrote: > > > >Quoth John Kelly <j...(a)isp2dial.com>: > > > >> #!/usr/bin/perl > > > >> use strict; > > >> use warnings; > > > >> my $data; > > > >> while (<>) { > > >> chomp; > > >> if (!$data && $_) { > > >> $data = $_; > > >> } > > >> } > > > >> print "data=$data\n"; > > > >> This code reads STDIN and remembers the first non-empty line. That's > > >> all it cares about. > > > >No, it remembers the first line that doesn't evaluate to boolean false.. > > >Since you are chomping the lines, a line containing only "0" will be > > >considered 'empty'. You want to check length $data. > > > Yeah, shot myself in the foot again. > > > >> But it also keeps reading till EOF, acting like the "cat" utility, to > > >> flush the extra input and avoid broken pipe errors. > > > >> But reading line by line, just to throw away the unwanted garbage, is > > >> inefficient. I would like to jump out of the loop and "bulk flush" the > > >> remaining input stream. > > > >> I don't think > > > >> >$io->flush > > > >> >flush causes perl to flush any buffered data at the perlio > > >> >api level. Any unread data in the buffer will be discarded, > > > >> will work, because that it flushes the buffer, not the entire input > > >> stream. I want to flush the whole file. And keep in mind, I don't want > > >> a broken pipe either. > > > >> Suggestions? > > > > my $data; > > > while (<>) { > > > chomp; > > > length or next; > > > $data = $_; > > > last; > > > } > > > { > > > local $/ = \2048; > > > 1 while <>; > > > } > > > >Ben > > > That looks interesting, I get the first part, the rest will give me > > something to chew on ... > > But, this'll just read/toss 2048 byte chunks > till EOF. Alternatively, if you want to toss > the entire stream after exiting the loop, a > single statement: > > <>; ^^^^^^^ () = <>; -- Charles DeRykus Charles DeRykus |