SPUG: Multiple uses of <STDIN> in a script?
Jim Traugott
jtraug at seanet.com
Fri Oct 15 12:23:57 CDT 1999
You're asking a little much of STDIN... Why not submit your filenames
as args?
Try this:
#!/usr/bin/perl -w
for (@ARGV)
{
print "this line is $_: [Y:N] ";
my $y_or_n = <STDIN>;
chomp ($y_or_n);
print "$y_or_n just for the heck of it...\n";
}
... and call it like this:
test.pl `find /tmp`
jim
>>>>> "Ryan" == Ryan Erwin <ryan at erwin.org> writes:
> Greetings once again Spuggers...
> Back to yesterdays' question about STDIN...
> I tried to issue:
STDIN-> IO::Handle::clearerr
> and
> $where = tell (STDIN);
> seek(STDIN, $where, 0);
> and a few other seek commands...
> I tried
STDIN-> POSIX::clearerr; # i think that was the syntax i used...
> To No Avail...
> fortunately,
> my $var = open(TTY,"</dev/tty") || die "can't open </dev/tty $!";
> did work, but it is very cumbersom to type what i want, press [enter] then pressCTRL+D to finish the input...
> Here is a version of the script that shows the problem:
> #!/usr/bin/perl -w
> my @rray = <STDIN>;
> chomp (@rray);
> foreach (@rray) {
> print "this line is $_: [Y:N] ";
> my $y_or_n = <STDIN>;
> chomp ($y_or_n);
> print "$y_or_n just for the heck of it...\n";
> }
> I would run the script (i'll call it /tmp/test.pl) like this:
> find /tmp | /tmp/test.pl
> I think that this should work, but it doesn't...
> Obviously, it doesn't work, so i'm doing (at least) something wrong, but i'm not sure what. Please, I'm sure one of you knows how to make this work ;)
> BTW: this is just the really, really minimized version of the script that just takes a list of files on STDIN and 2 regex's, one to filter the list sent to STDIN, one to make the new name for the file. I thought it would be useful when i need to change the names of a bunch of files at once... I would have posted it, but i forgot it at home and my PPP connection at home hasn't been working since i moved (last week).
> Thanks
> ryan [ryan at erwin.org]
> On Thu, 14 Oct 1999, Stuart Poulin wrote:
>> One thing you can do on Unix is open /dev/tty for input.
>> Then even if your program is reading STDIN from a file or a pipe it can get
>> input from the user.
>>
>> open(TTY,"</dev/tty") or die "Can't open </dev/tty : $!";
>>
>> --- Ryan Erwin <ryan at erwin.org> wrote:
>> > I hate to interrupt our fun discussion on where we are going to have our
>> > meetings but i ran into a little problem yesterday and wondered if one of you
>> > know the answer. ;) <- what do ya think of that guy...
>> >
>> > Anyways:
>> > i have a little command line script on my linux box that takes a list of file
>> > names on <STDIN>, then asks the user (me) a question about the file.
>> > Usually, to get user input I just use my $somevar = <STDIN>; but it doesn't
>> > work in this case.
>> >
>> > i hunted around last night and found that perlfaq8 had a section called "Why
>> > can't my script read from STDIN after I gave it EOF?" This sounds exactally
>> > like what is happening so I read on and the faq8 said "the POSIX module
>> > defines clearerr() that you can use." So I checked the POSIX mod docs and it
>> > says:
>> > clearerr - use IO::Handle::clearerr() instead.
>> > Now, I go over and check IO::Handle docs and find that I'm supposed to issue:
>> > $fh->clearerr
>> > I do this (STDIN->clearerr) and it works just find, but I still can't prompt
>> > the user for something on STDIN.
>> >
>> > The faq8 says that this is the technically correct way to reset the err flag
>> > so I would like to use it. Besides, seek on STDIN just seems strange to me
>> > (if it would even work, i haven't tried).
>> >
>> > Thanks!
>> >
>> > ryan [ryan at erwin.org]
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
> Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
> SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
> Email to majordomo at pm.org: ACTION spug-list your_address
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
Email to majordomo at pm.org: ACTION spug-list your_address
More information about the spug-list
mailing list