[30530] in Perl-Users-Digest
Perl-Users Digest, Issue: 1773 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 6 14:09:54 2008
Date: Wed, 6 Aug 2008 11:09:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 6 Aug 2008 Volume: 11 Number: 1773
Today's topics:
Re: .gz and .bz2 files on the Perl command line xhoster@gmail.com
Re: .gz and .bz2 files on the Perl command line <tzz@lifelogs.com>
a weird problem <zhilianghu@gmail.com>
Re: bidding advice for a contract <cartercc@gmail.com>
Re: bidding advice for a contract <Peter@PSDT.com>
Re: bidding advice for a contract <cwilbur@chromatico.net>
Re: bidding advice for a contract <vilain@NOspamcop.net>
Re: CLPM - a help group? <RedGrittyBrick@SpamWeary.foo>
Re: CLPM - a help group? <brian.d.foy@gmail.com>
Re: CLPM - a help group? <brian.d.foy@gmail.com>
Re: FAQ 4.36 How can I expand variables in text strings <brian.d.foy@gmail.com>
Re: FAQ 4.61 How can I always keep my hash sorted? <brian.d.foy@gmail.com>
Re: How to check for filetype existence quickly <fawaka@gmail.com>
Re: How to check for filetype existence quickly <justin.0805@purestblue.com>
Re: How to check for filetype existence quickly xhoster@gmail.com
Re: How to flush STDIN with getc()? <usenet@larseighner.com>
Re: How to flush STDIN with getc()? <someone@example.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 06 Aug 2008 15:42:45 GMT
From: xhoster@gmail.com
Subject: Re: .gz and .bz2 files on the Perl command line
Message-Id: <20080806114247.019$pd@newsreader.com>
Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth Ted Zlatanov <tzz@lifelogs.com>:
> > Say I have this program call
> >
> > go.pl A B C.gz D.bz2
> >
> > I'd like to just use <> in go.pl and have the compressed files
> > automatically extracted. Is there a way to do that without writing my
> > own logic, as a module I can bring in? This is a read-only task. I
> > couldn't find anything on CPAN.
>
> Heh :). See the recent thread on p5p... The logic is trivial, at least
> if your files have sane names:
>
> BEGIN {
> for (@ARGV) {
> /\.gz$/ and $_ = "gzip -dc $_ |";
> /\.bz2$/ and $_ = "bzip2 -dc $_ |";
> }
> }
>
> since <> uses 2-arg open.
cat AutoZip.pm
use strict;
use warnings;
for (@ARGV) {
/\.gz$/ and $_ = "gzip -dc $_ |";
/\.bz2$/ and $_ = "bzip2 -dc $_ |";
}
1;
__END__
perl -MAutoZip -lne 'print' thing.gz
Yep, I get uncompressed lines out. It is a thing of beauty. Thanks
Ben.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Wed, 06 Aug 2008 11:08:22 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: .gz and .bz2 files on the Perl command line
Message-Id: <86r692jfd5.fsf@lifelogs.com>
On Wed, 06 Aug 2008 09:49:21 +0100 Paul Marquess <paul.marquess@btinternet.com> wrote:
PM> If you have IO::Uncompress::Zlib & IO::Uncompress::Bunzip2, then the code
PM> below will allow you to read .gz , .bz2, .zip etc plus non-compressed files
PM> as well.
PM> use IO::Uncompress::AnyUncompress;
PM> for my $file (@ARGV)
PM> {
PM> my $fh = new IO::Uncompress::AnyUncompress $file, Transparent => 1
PM> or die "Cannot open $file: $AnyUncompressError\n";
PM> while (<$fh>)
PM> {
PM> # whatever
PM> }
PM> }
I was trying to avoid doing that work, I just wanted to use <> without
creating variables. This was my second choice :)
PM> The trick is to include the 'Transparent' option when using
PM> IO::Uncompress::AnyUncompress. That will get it to read uncompressed files
PM> if it doesn't detect any compression.
Cool, I will remember that.
On Tue, 5 Aug 2008 22:41:47 +0100 Ben Morrow <ben@morrow.me.uk> wrote:
BM> The logic is trivial, at least if your files have sane names:
BM> BEGIN {
BM> for (@ARGV) {
BM> /\.gz$/ and $_ = "gzip -dc $_ |";
BM> /\.bz2$/ and $_ = "bzip2 -dc $_ |";
BM> }
BM> }
BM> since <> uses 2-arg open.
Nice, I think that's perfect. It would be nice if I could use a module
to do this rewrite more magically using more "proper" (not depending on
gzip and bzip2 being available), but this is very nice.
On 5 Aug 2008 21:35:50 GMT jt@toerring.de (Jens Thoms Toerring) wrote:
JTT> There's the PerlIO:gzip module, to be used like this:
JTT> use PerlIO::gzip;
JTT> open FOO, "<:gzip", "file.gz" or die $!;
JTT> print while <FOO>;
JTT> And there's PerlIO::via::Bzip2, to be used like this
JTT> use PerlIO::via::Bzip2;
JTT> open my $fh, "<:via(Bzip2)", "file.bz2" or die $!;
JTT> print while <$f>;
JTT> Not that I would have used them, just found them on CPAN by
JTT> searching for 'gzip' and 'bzip'.
As with Paul Marquess' suggestion, this is a good solution but I wanted
something more magical ;)
Thanks
Ted
------------------------------
Date: Wed, 6 Aug 2008 10:49:04 -0700 (PDT)
From: Zhiliang Hu <zhilianghu@gmail.com>
Subject: a weird problem
Message-Id: <0c574d47-2376-4564-9937-18b85981c979@f36g2000hsa.googlegroups.com>
I have a perl script that takes a while to run. I inserted a few
lines in the program to print some progress messages to the console
(STDOUT) so I know where it is at, like:
#!/usr/bin/perl
print STDOUT "Please wait "; #-- This is the very first line in the
script
open(FILE,">localfile");
use DBI;
$dbh = DBI->connect("dbi:mysql:.....");
#-- omitted lines: query and fetch etc.
while (@content = $query_a->fetchrow_array) {
#-- some nested stuff omitted, etc etc
print FILE "--some stuff...\n";
print STDOUT "...";
}
print STDOUT "done\n";
$dbh->disconnect;
close(FILE);
But it prints out nothing to the screen until the very end, when it
finishes, it dumps all for STDOUT at once to the screen. The other
parts of the program runs fine. I thought I have used similar schemes
some years ago and it worked nicely but I cannot see what's wrong
here. Something new on perl 5.8.x or what else could be the cause?
Thanks in advance!
Zhiliang
------------------------------
Date: Wed, 6 Aug 2008 04:08:54 -0700 (PDT)
From: cartercc <cartercc@gmail.com>
Subject: Re: bidding advice for a contract
Message-Id: <74b61c97-8caa-42fd-bb6d-08afd9f63148@k13g2000hse.googlegroups.com>
Charlton,
I practiced law for 17 years before I changed careers to IT. I was
licensed in two states. I mostly practiced commercial and consumer
law, domestic relations and insolvency, and didn't get into the IP
area at all, so I freely admit that I am not versed in the area.
However, I ~do~ understand the legal system and the dynamics of how
this plays out in a business setting.
What you say may (note: 'may') be true from a theoretical standpoint,
but I would be very, very surprised if it became a problem. Yesterday
afternoon, I mentioned this to my boss, bigger boss, and biggest boss,
and they all thought I had gone bonkers. I write some code, borrow
some code, steal some code, move it around, use it for purposes both
public and private, official and unofficial. I DON'T(!!!) use stuff
that the RIAA or the MPA would be concerned about, don't download or
copy movies or songs, don't copy books. But when it comes to my work
product, I view that as grist for the mill. After all, what's the
difference if I write code for work, then go to a second job and write
essentially the identical code to solve the same problem, and then go
home and write essentially the same code to some the same problem for
a non-profit that I volunteer for?
You are the only person that seems to think it is a problem. Let's
leave it that way.
On Aug 6, 12:53 am, Charlton Wilbur <cwil...@chromatico.net> wrote:
> >>>>> "cc" == cartercc <carte...@gmail.com> writes:
> This is extremely unprofessional behavior, *because* of the intellectual
> property issues it creates. There should be a clear line between what
> the university owns and what you own, and the difference should be made
> clear in writing.
>
> When your employer pays you to do the work, in the absence of a contract
> to the contrary, the code you write is the intellectual property of the
> employer. If you do not want this to be the case, you need to get
> something in writing from your employer that says otherwise.
>
> In particular, if you take code that belongs to your current employer
> and use it for your new employer, you are violating intellectual
> property rights and exposing both your old employer and your new
> employer to lawsuits. You don't have the rights to the code you wrote
> for hire; your current employer would need to license the code to your
> new employer.
>
> You may find it arrant nonsense, but for the most part, it's the law,
> and you have a professional obligation to behave ethically. Playing
> fast and loose with this sort of thing is a very good way to torpedo
> your consulting career before it gets off the ground.
I think you and I probably have very different ideas of ethics. I am
very familiar with professional codes of conduct, and have even
violated a few of them, both intentionally and unknowingly. I am
familiar with the various codes that have been proposed for SWEs,
although I don't think that any have been officially adopted. Ethics
and morals are two totally different things. I don't have any
compunction about violating codes of ethics, which after all are
merely codifications of what a particular group thinks at a particular
time and frequently changes, sometimes 180 degrees. Morals never
change and are a constant in an inconstant world.
But I digress ... have a nice day, and no hard feelings.
CC
------------------------------
Date: Wed, 06 Aug 2008 13:20:41 GMT
From: Peter Scott <Peter@PSDT.com>
Subject: Re: bidding advice for a contract
Message-Id: <pan.2008.08.06.13.20.40.816561@PSDT.com>
On Tue, 05 Aug 2008 07:20:50 -0700, cartercc wrote:
> I've been there and talked to the management staff. I've seen what
> they do. They are focused on their product but their record keeping is
> mostly manual. The literally keep track of inventory and purchases on
> yellow pads. The use Excel to track testing data but have no security
> or logging, so they can't tell who made what entry.
This is wildly at odds with your assertion that the company is 'big' in
the military ordnance systems industry. In that business, 'big' means
tens of billions of dollars in annual revenue.
If you insist on sticking your hand into this bear trap then do this:
contact a lawyer and pay them to draw up a contract for your engagement,
and present it to the company with the explanation that this is your
standard boilerplate. If (when) they balk, that will tell you volumes
about their real motivations.
--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/
------------------------------
Date: Wed, 06 Aug 2008 10:48:26 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: bidding advice for a contract
Message-Id: <86hc9yqjwl.fsf@mithril.chromatico.net>
>>>>> "cc" == cartercc <cartercc@gmail.com> writes:
cc> Charlton, I practiced law for 17 years before I changed careers
cc> to IT. I was licensed in two states. I mostly practiced
cc> commercial and consumer law, domestic relations and insolvency,
cc> and didn't get into the IP area at all, so I freely admit that I
cc> am not versed in the area.
Then you need to consult a lawyer who is versed in the area.
cc> You are the only person that seems to think it is a
cc> problem. Let's leave it that way.
Hey, it's your career. Good luck.
Charlton
--
Charlton Wilbur
cwilbur@chromatico.net
------------------------------
Date: Wed, 06 Aug 2008 08:26:56 -0700
From: Michael Vilain <vilain@NOspamcop.net>
Subject: Re: bidding advice for a contract
Message-Id: <vilain-46427D.08265606082008@comcast.dca.giganews.com>
In article <pan.2008.08.06.13.20.40.816561@PSDT.com>,
Peter Scott <Peter@PSDT.com> wrote:
> On Tue, 05 Aug 2008 07:20:50 -0700, cartercc wrote:
> > I've been there and talked to the management staff. I've seen what
> > they do. They are focused on their product but their record keeping is
> > mostly manual. The literally keep track of inventory and purchases on
> > yellow pads. The use Excel to track testing data but have no security
> > or logging, so they can't tell who made what entry.
>
> This is wildly at odds with your assertion that the company is 'big' in
> the military ordnance systems industry. In that business, 'big' means
> tens of billions of dollars in annual revenue.
>
> If you insist on sticking your hand into this bear trap then do this:
> contact a lawyer and pay them to draw up a contract for your engagement,
> and present it to the company with the explanation that this is your
> standard boilerplate. If (when) they balk, that will tell you volumes
> about their real motivations.
The OP has mentioned that he's a trained attorney, albeit with specialty
in other fields. But I would hazard to guess he a big boy and can write
contract that can cover his posterior and seems to know he, his morals
and his malleable ethics are walking through the valley of death here.
I think it's time to leave this guy alone with his project and let him
figure out just how much of a lesson he's going to learn from this
fiasco. It's his business decision. We've told him he's stepping into
a hornet's nest. He seems to think the risks are worth it.
After the hard deadline has come and gone, why not check back here and
see what happened. Until then, to quote Dr. Phil "Good luck with
that..."
--
DeeDee, don't press that button! DeeDee! NO! Dee...
[I filter all Goggle Groups posts, so any reply may be automatically by ignored]
------------------------------
Date: Wed, 06 Aug 2008 11:19:58 +0100
From: RedGrittyBrick <RedGrittyBrick@SpamWeary.foo>
Subject: Re: CLPM - a help group?
Message-Id: <48997acf$0$2510$da0feed9@news.zen.co.uk>
Adam Worrall wrote:
> Michael Carman wrote:
>> Adam Worrall wrote:
>>> Yes I meant 'help desk', but you are wrong in assuming that a 'help
>>> desk' always has to be a formal service for customers. There are, in
>>> fact, many volunteer-run help desk, and this newsgroup, like many
>>> others, functions as exactly that. People volunteer their time to
>>> _help_ others.
>>
>> I believe that the aversion to calling c.l.p.misc a "help desk" is
>> because that term carries a connotation that the purpose of the group
>> is to provide a service where someone will solve your problem for you.
>> The regular contributors to this group are, on the whole, very
>> resistant to that idea. "RTFM. What have you tried? Show us your code."
>>
>> nobull phrased it very well:
>>
>> "Get real! This is a discussion group, not a helpdesk. You post
>> something, we discuss its implications. If the discussion happens
>> to answer a question you've asked, that's incidental."
>>
>> The purpose of the group is to discuss Perl: it's strengths,
>> weaknesses, idioms, quirks, dusty corners, etc. The asking and
>> answering of questions are merely a mechanism by which discussion
>> topics are identified.
>
> I understand what you are saying, but it still doesn't answer my
> question: who exactly gets to decide what is and isn't acceptable for
> everyone in a news group? AKAIK, no one simple person can claim
> ownership of a news group, so telling someone something along the lines
> of "this is not a help desk" is legislating your own beliefs, which is
> contrary to what Usenet is and how it works (at least in terms of open
> non-moderated groups.)
If I said "comp.lang.perl.misc is not a place for discussing pet
allergies" would I be legislating my own belief?
If I were to say "comp.lang.perl.misc is not a help desk" I would be
merely stating my opinion on the consensus view (as observed by me) of
the main participants as to how they choose to participate and comparing
that consensus with a scenario in which people are employed (paid) to
answer customer enquiries.
When I say "the sky is blue" it is an observation, I am not legislating
what the sky may or may not do.
Your Mileage May Vary.
--
RGB
------------------------------
Date: Wed, 06 Aug 2008 07:02:24 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: CLPM - a help group?
Message-Id: <060820080702246237%brian.d.foy@gmail.com>
In article <t5qdncHzZYs62QXVnZ2dnUVZ8trinZ2d@posted.plusnet>, bugbear
<bugbear@trim_papermule.co.uk_trim> wrote:
> Indeed; and if there are (e.g.) 5 wise people
> you need to search 5 blogs.
or one google :)
------------------------------
Date: Wed, 06 Aug 2008 07:03:38 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: CLPM - a help group?
Message-Id: <060820080703380715%brian.d.foy@gmail.com>
In article <vvbmk.16412$xZ.9861@nlpi070.nbdc.sbc.com>, Adam Worrall
<worrall+unet@cs.bris.ac.uk> wrote:
> I understand what you are saying, but it still doesn't answer my
> question: who exactly gets to decide what is and isn't acceptable for
> everyone in a news group?
The charter decides that. Beyond that, there is no clpm police that
gets to decide. So, post whatever Perl stuff you like.
This still isn't a help desk, but I'm usually happy to help people when
I can. :)
------------------------------
Date: Wed, 06 Aug 2008 07:11:34 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 4.36 How can I expand variables in text strings?
Message-Id: <060820080711349249%brian.d.foy@gmail.com>
In article <gcnmm5x06a.ln2@carpet.zombinet>, Eric Pozharski
<whynot@pozharski.name> wrote:
> C<ping-pong whatever> means a contents where participants attempt to
> find as many ways to achieve something using some tools.
Well, that seems to have nothing to do with ping pong, a game of
hitting a ball back and forth over and over again.
> Take a look at that part of answer (yes, it's taken out of context, but
> that's what reader sees first):
>
> my $foo = 'Fred';
> my $bar = 'Barney';
> $string = 'Say hello to $foo and $bar';
>
> The question states C<How can I expand variables in text strings?>. The
> unexpert reader (me?) doesn't get why C<q()> is used in place of
> C<qq()>. The latter perfectly expands I<$foo> and I<$bar>.
If we used double quotes, it would interpolate the varaibles and we
wouldn't have a text string with placeholders that we need to expand,
and we wouldn't need the answer.
> May be I've missed something again, but I failed to find in neither
> question nor answer any notion that I<$foo> and I<$bar> are undefined at
> compile time. BTW, the answer misses either discussion or redirection
> why C<$string = eval { "Say hello to $foo and $bar"; };> is bad.
This isn't a relevant question for this topic. We're not talking about
interpolation of double quoted strings. We're talking about literal
strings---those don't do interpolation.
And the block eval does nothing for you, btw.
------------------------------
Date: Wed, 06 Aug 2008 07:12:45 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 4.61 How can I always keep my hash sorted?
Message-Id: <060820080712453522%brian.d.foy@gmail.com>
In article <20080805030408.084$nG@newsreader.com>, <xhoster@gmail.com>
wrote:
> Ben Morrow <ben@morrow.me.uk> wrote:
> >
> > 'tie' is provided so you can create a hash that is built any way you
> > like, and with the appropriate application of XS and magic you can even
> > make it as efficient as the builtin hashes.
>
> Are there working examples of this? I've thought it would be nice to have
> perl's own hash implementation copied from the guts and put into a tie able
> XS module, for playing around purposes.
Huh, that would be really cool. If anyone does it please post about it
instead of silently uploading it to CPAN :)
------------------------------
Date: 06 Aug 2008 10:19:37 GMT
From: Leon Timmermans <fawaka@gmail.com>
Subject: Re: How to check for filetype existence quickly
Message-Id: <48997ab9$0$49896$e4fe514c@news.xs4all.nl>
On Wed, 06 Aug 2008 01:32:09 -0700, fidokomik wrote:
> I have directory, say "c:\documents" on Windows or "/home/petr/
> documents" on Linux. In this directory many files are stored with many
> filetypes (extensions), say *.doc, *.txt, *.zip. I need to find fastes
> way how to check if some filetype exist. Now I use routine where I use
> readdir() and return true if passed filetype is first time found but
> when I have huge number of files but passed filetype is not found then
> routine is very slow before return false. Any idea?
To find a negative match, you will have to loop through the whole list,
that is unavoidable. However, if you find yourself testing the same
directory a number of times for different extentions, you could loop
through it once and save a list of extensions you've found.
opendir my $dh, $basedir;
my %is_found;
while (my $dirname = readdir $dh) {
$dirname =~ / \. (\w+) \z /x or next;
$is_found{$1}++;
}
for my $extention (qw/exe doc txt zip mp3/) {
my $found = $is_found{$extention} ? "Found" : "Didn't found";
print "$found $extention\n";
}
Regards,
Leon Timmermans
------------------------------
Date: Wed, 06 Aug 2008 10:18:32 -0000
From: Justin C <justin.0805@purestblue.com>
Subject: Re: How to check for filetype existence quickly
Message-Id: <266a.48997a78.ec6d5@zem>
On 2008-08-06, fidokomik <fidokomik@gmail.com> wrote:
> I have directory, say "c:\documents" on Windows or "/home/petr/
> documents" on Linux. In this directory many files are stored with many
> filetypes (extensions), say *.doc, *.txt, *.zip. I need to find fastes
> way how to check if some filetype exist. Now I use routine where I use
> readdir() and return true if passed filetype is first time found but
> when I have huge number of files but passed filetype is not found then
> routine is very slow before return false.
> Any idea?
ls | egrep doc\|txt\|zip
(you need to escape the 'or' operator, and use egrep instead of grep)
I know you want to use perl, but perl won't be as fast as this... unless
there are a very, very large[1] number of files.
Justin.
1. Depending on your concept of large.
--
Justin C, by the sea.
------------------------------
Date: 06 Aug 2008 15:56:23 GMT
From: xhoster@gmail.com
Subject: Re: How to check for filetype existence quickly
Message-Id: <20080806115624.900$bD@newsreader.com>
fidokomik <fidokomik@gmail.com> wrote:
> I have directory, say "c:\documents" on Windows or "/home/petr/
> documents" on Linux. In this directory many files are stored with many
> filetypes (extensions), say *.doc, *.txt, *.zip. I need to find fastes
> way how to check if some filetype exist.
You will have to write a file system that is optimized for this (for
example, it stores directory information in some kind of tree based on the
reversed file name, so that extensions group together.) Then you have to
hack the operating system so that it can take advantage of the FS features.
Then you would have to hack perl so that it can take advantage of the OS
features.
Personally, I think I'd settle for something other than the fastest, and
just aim for good enough.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Wed, 6 Aug 2008 13:40:32 +0000 (UTC)
From: Lars Eighner <usenet@larseighner.com>
Subject: Re: How to flush STDIN with getc()?
Message-Id: <slrng9ja4p.1qmk.usenet@debranded.larseighner.com>
In our last episode, <slrng9imqi.1pfu.usenet@debranded.larseighner.com>, the
lovely and talented Lars Eighner broadcast on comp.lang.perl.misc:
> I'm using perl 5.8.8 on FreeBSD.
> I understand that getc() defaults to STDIN when an argument is not
> specified, and getc() does not work until ENTER is hit at the keyboard
> (i.e. STDIN). However, on the second call to get(), it appears I
> get the next character from the same line (which might be the
> newline from the ENTER, or might be 'e' if 'yes\n' was typed instead
> of 'y\n'.
> What's the right way to throw away the junk from the previous line?
Nevermind.
It appears getc() is not what I want (athough I am still curious about
the question posed. $answer = <STDIN> seems to be the right solution,
and works in this sub:
sub ask {
$question = shift(@_);
print "$question\n$enter ($aff/$neg): ";
$answer = <STDIN>;
chomp $answer;
$answer =~ s/^(.).*\n/$1/;
if ( $answer eq $aff || $answer eq uc($aff)){
print "Answer is yes.\n";
return 1;
}
if ( $answer eq $neg || $answer eq uc($neg)){
print "Answer is no.\n";
return 0;
}
my $count=1;
while ($count <= 3){
print "$question\n$bad_answer";
$answer = <STDIN>;
chomp $answer;
$answer =~ s/^(.).*\n/$1/;
if ( $answer eq $aff || $answer eq uc($aff)){
print "Answer is yes.\n";
return 1;
}
if ( $answer eq $neg || $answer eq uc($neg)){
print "Answer is no.\n";
return 0;
}
if ( $answer eq $quit || $answer eq uc($quit)){
die ("$quit_req\n");
}
$count = $count + 1;
}
die ("$too_many");
}
where the English values are:
$aff = 'y';
$neg = 'n';
$quit ='q';
$enter = 'enter';
$bad_answer = "No default. Please enter y for yes or n for no or " .
"q to quit\n" .
"Enter (y/n or to quit enter q): ";
$quit_req = "Quit requested. Goodbye!";
$too_many = "Too many tries. Goodbye!";
--
Lars Eighner <http://larseighner.com/> usenet@larseighner.com
Consider what might be fertilizing the greener grass across the fence.
------------------------------
Date: Wed, 06 Aug 2008 18:07:46 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: How to flush STDIN with getc()?
Message-Id: <SLlmk.61095$nD.52932@pd7urf1no>
Lars Eighner wrote:
> In our last episode, <slrng9imqi.1pfu.usenet@debranded.larseighner.com>, the
> lovely and talented Lars Eighner broadcast on comp.lang.perl.misc:
>
>> I'm using perl 5.8.8 on FreeBSD.
>
>> I understand that getc() defaults to STDIN when an argument is not
>> specified, and getc() does not work until ENTER is hit at the keyboard
>> (i.e. STDIN). However, on the second call to get(), it appears I
>> get the next character from the same line (which might be the
>> newline from the ENTER, or might be 'e' if 'yes\n' was typed instead
>> of 'y\n'.
>
>> What's the right way to throw away the junk from the previous line?
>
> Nevermind.
>
> It appears getc() is not what I want (athough I am still curious about
> the question posed. $answer = <STDIN> seems to be the right solution,
> and works in this sub:
>
> sub ask {
> $question = shift(@_);
> print "$question\n$enter ($aff/$neg): ";
> $answer = <STDIN>;
> chomp $answer;
> $answer =~ s/^(.).*\n/$1/;
chomp() removes the newline so your pattern won't match.
> if ( $answer eq $aff || $answer eq uc($aff)){
You can simplify that a bit:
my $answer = <STDIN>;
if ( uc $aff eq uc substr $answer, 0, 1 ) {
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1773
***************************************