Sponsored Content
Top Forums Programming Simple capturing of keyboard input without interruption Post 303014186 by jim mcnamara on Tuesday 6th of March 2018 03:40:41 PM
Old 03-06-2018
Overview:
I'm not going to figure out all of the above wall of code. This example is how MS windows and X work. A simple, fairly stupid, event (message) pump that does nothing but read stdin, then send it off to a pipe. The little one I call "tiny" your code wall is "big" and it needs to dup() stdin on startup.

What you will do is to steal a tiny bit of windowing architecture - as in a message pump:
interpose a tiny program that loops and reads each character from stdin and then sends everything it gets from stdin to a pipe -except the <return> key. It exits or whatever you need on the return key.

Start the tiny program, have it fork your large program as a child that reads input from a pipe. Big's code can live in the same physical code that tiny lives in. Call fork() on the "big" entry point.

Tiny reads everything and simply passes it on, except in your case tiny exits when the ASCII 13 character (\n) is read. Tiny has almost zero smarts, just calls signal(), read(), wait() and pipe().

If tiny wants to quit: signal big, call wait() on big, then exit().
If big wants to quit: signal tiny, then exit() Tiny's signal handler gets the signal and exits.

If you really get stuck, post your attempt. If executable code lines in the tiny program code exceeds circa 50 lines or so your logic is probably too complex. Add bells and whistles after it works.
This User Gave Thanks to jim mcnamara For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

suppressing keyboard input

Setup Info: This User Id and Password mention below are being used with the ISQL command to connect to a sybase database so they are likely to not be the same as those that were signed on from the session. Situation: Using a korn shell, the shell prompts for a User Id and Password. During the... (1 Reply)
Discussion started by: anthreedhr
1 Replies

2. Programming

Detecting Keyboard Input without return

Hi, I need a way to detect the up and down arrow key inputs for my program. I do not want to wait for the return key to be entered(so that rules out getch() and family). Also I need to process several of these inputs in parallel, by servicing each request with a thread. Is that possible? ... (4 Replies)
Discussion started by: ravneetd
4 Replies

3. Programming

Keyboard Input

Does anyone know how do you determine the user idle time of STDIN in order to log the user out for being idle too long. I would like to write a c program to do this but I it is not clear upon how to determine idle time from keyboard input. I have found that the "who.c" source file uses the last... (4 Replies)
Discussion started by: cpaquette
4 Replies

4. UNIX for Dummies Questions & Answers

Capturing Input Parameters on Shell Script

i have this basic line of code that doesn't work. i simply want to get the input parameter strings but when the script is run it appears that the first parameter is assigning the value to the second parameter. #!/bin/sh pdir=`pwd` p1=$1 p2=$2 echo "directory: $pdir\n" echo "parameter... (2 Replies)
Discussion started by: wtolentino
2 Replies

5. Shell Programming and Scripting

Keyboard input question

How would I change up a script that currently has something like: bash script echo what's 1 2 3 4? then using read 1 2 3 4 I type 1 2 3 4. so in the script i can do stuff like echo $1 $2 $3 $4 and such... i was just doing echo "1 2 3 4"|bash script But was wondering how could I... (5 Replies)
Discussion started by: biopulse
5 Replies

6. Shell Programming and Scripting

Capturing script output and input to log

Hi Is there a way that I can capture a shell script (both output and input) to a log file where I can analyze it? Thanks (6 Replies)
Discussion started by: nimo
6 Replies

7. Shell Programming and Scripting

sed execution with input from keyboard

> sed '' Hello hi Hello output How hi output ^D How > sed should take each line as input, process and output the result. In the above scenario the input is passed from keyboard and the output of 'Hello' as you can see is displayed on the screen after 'hi' is passed as input but not as... (1 Reply)
Discussion started by: loggedin.ksh
1 Replies

8. Shell Programming and Scripting

Capturing multiple values from user input

Hello, I need to capture multiple values from user input and do not know how to do it. I'm writing a script to delete old files, but want to give the option to keep some by asking the user. This is what my output looks like... Old files to be deleted... 1 file1 2 file2 Then this bit of... (3 Replies)
Discussion started by: jrymer
3 Replies

9. Shell Programming and Scripting

How to get input from keyboard with watch?

Hello, i`m trying to create an network monitoring script and i dont know how to make affect that script by pressing an key from keyboard and that script runs not in while or for or any other loop, but with bash command watch for example: i have created an file (for example check) with content... (0 Replies)
Discussion started by: bacarrdy
0 Replies

10. Shell Programming and Scripting

Read input from Keyboard, do not proceed if no input

Hi, I am working on a script, which requests users to enter input. Ex: read -p "Please enter your email id:" email I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input. I can do a check if variable $email is empty and proceed if not.But, i... (7 Replies)
Discussion started by: aravindadla
7 Replies
Object::Tiny(3pm)					User Contributed Perl Documentation					 Object::Tiny(3pm)

NAME
Object::Tiny - Class building as simple as it gets SYNOPSIS
# Define a class package Foo; use Object::Tiny qw{ bar baz }; 1; # Use the class my $object = Foo->new( bar => 1 ); print "bar is " . $object->bar . " "; DESCRIPTION
There's a whole bunch of class builders out there. In fact, creating a class builder seems to be something of a rite of passage (this is my fifth, at least). Unfortunately, most of the time I want a class builder I'm in a hurry and sketching out lots of fairly simple data classes with fairly simple structure, mostly just read-only accessors, and that's about it. Often this is for code that won't end up on CPAN, so adding a small dependency doesn't matter much. I just want to be able to define these classes FAST. By which I mean LESS typing than writing them by hand, not more. And I don't need all those weird complex features that bloat out the code and take over the whole way I build modules. And so, I present yet another member of the Tiny family of modules, Object::Tiny. The goal here is really just to save me some typing. There's others that could do the job just fine, but I want something that does as little as possible and creates code the same way I'd have written it by hand anyway. To use Object::Tiny, just call it with a list of accessors to be created. use Object::Tiny 'foo', 'bar'; For a large list, I lay it out like this... use Object::Tiny qw{ item_font_face item_font_color item_font_size item_text_content item_display_time seperator_font_face seperator_font_color seperator_font_size seperator_text_content }; This will create a bunch of simple accessors, and set the inheritance to be the child of Object::Tiny. Object::Tiny is empty other than a basic "new" constructor which does the following sub new { my $class = shift; return bless { @_ }, $class; } In fact, if doing the following in your class gets annoying... sub new { my $class = shift; my $self = $class->SUPER::new( @_ ); # Extra checking and such ... return $self; } ... then feel free to ditch the SUPER call and just create the hash yourself! It's not going to make a lick of different and there's nothing magic going on under the covers you might break. And that's really all there is to it. Let a million simple data classes bloom. Features? We don't need no stinking features. Handling Subclasses If the class you are using Object::Tiny for is already a subclass of another Object::Tiny class (or a subclass of anything else) it doesn't really work to make the class use multiple inheritance. So in this case, Object::Tiny will create the accessors you specify, but WON'T make it a subclass of Object::Tiny. Why bother when Class::Accessor::* already does the same thing? As a class builder, Object::Tiny inevitably is compared to Class::Accessor and related modules. They seem so similar, so why would I reimplement it? The answer is that for experienced developers that don't need or want hand-holding, Object::Tiny is just outright better, faster or cheaper on every single metric than Class::Accessor::Fast, which is the most comparable member of the Class::Accessor::* family. Object::Tiny is 93% smaller than Class::Accessor::Fast Class::Accessor::Fast requires about 125k of memory to load. Object::Tiny requires about 8k of memory to load. Object::Tiny is 75% more terse to use than Class::Accessor::Fast Object::Tiny is used with the least possible number of keystrokes (short of making the actual name Object::Tiny smaller). And it requires no ugly constructor methods. I mean really, what sort of a method name is 'mk_ro_accessors'. That sort of thing went out of style in the early nineties. Using Class::Accessor::Fast... package Foo::Bar; use base 'Class::Accessor::Fast'; Foo::Bar->mk_ro_accessors(qw{ foo bar baz }); Using Object::Tiny... package Foo::Bar; use Object::Tiny qw{ foo bar baz }; Further, Object::Tiny lets you pass your params in directly, without having to wrap them in an additional HASH reference that will just be copied ANYWAY inside the constructor. Using Class::Accessor::Fast... my $object = Foo::Bar->new( { foo => 1, bar => 2, baz => 3, } ); Using Object::Tiny... my $object = Foo::Bar->new( foo => 1, bar => 2, baz => 3, ); Object::Tiny constructors are 110% faster than Class::Accessor::Fast Object::Tiny accessors are identical in speed to Class::Accessor::Fast accessors, but Object::Tiny constructors are TWICE as fast as Class::Accessor::Fast constructors, DESPITE C:A:Fast forcing you to pass by reference (which is typically done for speed reasons). Benchmarking constructor plus accessors... Rate accessor tiny accessor 100949/s -- -45% tiny 182382/s 81% -- Benchmarking constructor alone... Rate accessor tiny accessor 156470/s -- -54% tiny 342231/s 119% -- Benchmarking accessors alone... Rate tiny accessor tiny 81.0/s -- -0% accessor 81.0/s 0% -- Object::Tiny pollutes your API 95% less than Class::Accessor::Fast Object::Tiny adds two methods to your class, "new" and "import". The "new" constructor is so trivial you can just ignore it and use your own if you wish, and the "import" will shortcut and do nothing (it is used to implement the "use Object::Tiny qw{ foo bar baz };" syntax itself). So if you make your own import, you can ignore the Object::Tiny one. Class::Accessor::Fast isn't quite as light, adding all sorts of useless extra public methods (why on earth would you want to add method accessors at run-time?). Here's what the classes used in the benchmark end up like. DB<1> use Class::Inspector DB<2> x Class::Inspector->methods('Foo_Bar_Tiny'); 0 ARRAY(0xfda780) 0 'bar' 1 'baz' 2 'foo' 3 'import' 4 'new' DB<3> x Class::Inspector->methods('Foo_Bar_Accessor'); 0 ARRAY(0xfdb3c8) 0 '_bar_accessor' 1 '_baz_accessor' 2 '_carp' 3 '_croak' 4 '_foo_accessor' 5 '_mk_accessors' 6 'accessor_name_for' 7 'bar' 8 'baz' 9 'best_practice_accessor_name_for' 10 'best_practice_mutator_name_for' 11 'follow_best_practice' 12 'foo' 13 'get' 14 'make_accessor' 15 'make_ro_accessor' 16 'make_wo_accessor' 17 'mk_accessors' 18 'mk_ro_accessors' 19 'mk_wo_accessors' 20 'mutator_name_for' 21 'new' 22 'set' As you can see, Object::Tiny adds 2 methods to your class, Class::Accessor adds 16 methods, plus one extra one for every accessor. Object::Tiny doesn't have any of the caveats of Class::Accessor::Fast When you call use Object::Tiny qw{ foo bar baz } it isn't treated as some sort of specification for the class, it's just a list of accessors you want made for you. So if you want to customize "foo" you don't need to get into contortions with "pure" base classes or calling alternate internal methods. Just make your own "foo" method and remove "foo" from the list passed to the "use" call. Object::Tiny is more back-compatible than Class::Accessor::Fast Class::Accessor::Fast has a minimum Perl dependency of 5.005002. Object::Tiny has a minimum Perl dependency of 5.004. Object::Tiny has no module dependencies whatsoever Object::Tiny does not load ANYTHING at all outside of its own single .pm file. So Object::Tiny will never get confused in odd situations due to old or weird versions of other modules (Class::Accessor::Fast has a dependency on base.pm, which has some caveats of its own). SUPPORT
Bugs should be reported via the CPAN bug tracker at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-Tiny> For other issues, contact the author. AUTHOR
Adam Kennedy <adamk@cpan.org> SEE ALSO
Config::Tiny COPYRIGHT
Copyright 2007 - 2008 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.10.1 2008-08-16 Object::Tiny(3pm)
All times are GMT -4. The time now is 01:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy