Hi all, I am writing a database management program, and I make extensive use of IO::Prompt. If a user is making a database entry but decides to change his mind, the database needs to be saved to a file. This is because the database is read into memory when the user begins manipulating it, but the user has the option of making multiple entries in one sitting. I think it's wasteful to constantly write the database out to a file just to read it back in literally one second later, so I just keep adding to the database in memory, then write it all out later.

The thing is, the user needs to be able to quit data entry and go back to do other things by hitting the ESC key. I can't just return out of the add_to_db() subroutine without first doing a write_to_db() first.

What I've been doing thus is:

my @database = read_from_db(); while (1){ my $id = prompt "Enter the product ID: ", -escape; return write_to_db($database_type, @database) if $id eq "\e"; my $comments = prompt "Enter any comments about this product: ", - +escape; return write_to_db($database_type, @database) if $comments eq "\e" +; print "Confirm the following data:\n"; print "Product ID: $id\n"; print "Comments: $comments\n\n"; my $yn = prompt "Is this information correct? y/n: ", -yn; #no esc +ape here since this info will get lost if we escape redo if ! $yn; push(@database, [$id, $comments]); $yn = prompt "Add another entry? y\n: ", -yn, -escape; last if !yn || $yn eq "\e"; } write_to_db($database_type, @database);
For entries that have lots of fields, it gets unwieldy to write out that return if "\e" statement. I wrote a subroutine "clean_return" that writes the database to a file when called, but that doesn't change the fact that I can have up to 16 different return-if-ESC statements.

What I want to do is set up an interrupt-type thing: so if at any time the ESC key is pressed, it should do a clean_return. This way I only need to write that out the one time instead of up to 16 different explicit clean_return statements.

If you could help me out, I'd greatly appreciate it. Thanks.


In reply to Custom interrupt? by azredwing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.