Contributed by Anonymous Monk
on May 10, 2000 at 12:55 UTC
Q&A
> input and output
Description: In my script, I would like to simulate the fact that someone presses on the "y" key on the keyboard and then presses Return.
If anyone knows how to do that or if it is even possible to do it.
Cheers ! Answer: How do you simulate the use of the keyboard ? contributed by chromatic If you prefer not changing your filehandle, you can also cat (or perhaps echo, if you're stuck in Windows land) a file into your script:
#!/usr/bin/perl -w
use strict;
print while (<STDIN>);
Invoke it like this:
cat printline.pl | ./printline.pl
This trick works really really well for debugging CGI.pm applications from the command line. | Answer: How do you simulate the use of the keyboard ? contributed by httptech Well, the keyboard is usually read using <STDIN>
which is just another filehandle.
So say you have written a script that asks a few
questions like:
print "What is your name? ";
my $name = <STDIN>; chomp $name;
print "What is your quest? ";
my $quest = <STDIN>; chomp $quest;
print "What is your favorite color? ";
my $color = <STDIN>; chomp $color;
Now, if you want to test that out without actually
having to type it into the keyboard, you can make
a textfile like:
Anonymous Monk
I seek the Wisdom of the Monastery
Yellow...no, Blue! arghhhhhhhh!
Now, you can open that file in your program and
assign it a filehandle <FOO>, ahead of where the
questions are asked. Then, use a line like this:
*STDIN = *FOO; (also before where you
first start grabbing STDIN)
That allows you to alias STDIN to FOO and the file
will be read instead of the keyboard while you are
testing your program. When you're done testing,
just remove the open statement and the *STDIN = *FOO
line and you're all set.
| Answer: How do you simulate the use of the keyboard ? contributed by merlyn Depends on what you mean by "simulate". One way to mean it is to force a "y\n" into your own tty input buffer, like so:
sub jam {
$TIOCSTI = 0x80017472;
for (split(//, $_[0])) {
ioctl(STDERR, $TIOCSTI, $_) || die "bad TIOCSTI: $!";
}
}
jam("y\n");
Note that the value of TIOCSTI can be had from some "use something" that escapes me at the moment, but it'll end up looking like this pretty close. | Answer: How do you simulate the use of the keyboard ? contributed by btrott From the Perl Cookbook "solution" at www.perl.com:
require 'sys/ioctl.ph';
die "no TIOCSTI" unless defined &TIOCSTI;
Of course, it also says that
Since sys/ioctl.h translation is so dodgy, you'll
probably have to run this C program to get your
TIOCSTI value.
% cat > tio.c <<EOF && cc tio.c && a.out
main() { printf("%#08x\n", TIOCSTI); }
EOF
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|