in reply to Re^3: Not Catching Signals While Program is Prompting with IO::Prompt
in thread Not Catching Signals While Program is Prompting with IO::Prompt

I agree. I ended up with this:
use Time::HiRes qw(usleep); my $PROMPT="What's the ticket number?\n"; my $TRY_AGAIN_PROMPT="excuse me?\n"; my $TICKET_NUMBER=prompt_until($PROMPT, $TRY_AGAIN_PROMPT, '^\d{7}$|^NOTICKET$', 1500); sub prompt_until { my $prompt_text=$_[0]; my $try_again_prompt=($_[1])? $_[1] : ''; my $until_regex=($_[2])? $_[2] : '.*'; my $speed=($_[3])? $_[3] : '0'; my $answer; if ($speed) { print_slow($prompt_text,$speed); } else { print "$prompt_text"; } $answer=<STDIN>; until ($answer=~m/$until_regex/) { if ($speed) { print_slow($try_again_prompt,$speed); } else { print "$try_again_prompt"; } $answer=<STDIN>; } chomp $answer; return $answer; } ###################################################################### +########## sub print_slow { my $speed=pop(@_); my $phrase=join('',@_); my @characters=split(//,$phrase); foreach my $char (@characters) { print $char; usleep($speed); } } ###################################################################### +##########
  • Comment on Re^4: Not Catching Signals While Program is Prompting with IO::Prompt
  • Download Code

Replies are listed 'Best First'.
Re^5: Not Catching Signals While Program is Prompting with IO::Prompt
by BrowserUk (Patriarch) on Jun 12, 2012 at 23:29 UTC

    Good for you. It is nice to have control of your code isn't it.

    One comment though. What is the point of print_slow()?

    At 1500 microseconds per, your 25 character prompt will appear completely in 0.0375 of a second. No human being will ever notice the difference.

    And if you increased it to the point where is became noticeable -- say 50000 per or there abouts -- it will just piss off your users.

    It would me anyway! When I install new versions of windows one of the first things I do is disable all those damn artificial delays to menus, pointless animations, and the like; they just irritate me.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      It is certainly noticeable. There are several prompts in the program I wrote. The print_slow is to differentiate and emphasize the new output from the output that was output previously. It is quite effective.

      I tuned the speed so the messages require very little waiting, but the user can still tell what just printed to the screen. Sounds dumb, I know, but you have to see a whole run of the program before and after the print_slow implementation in order to understand. I suspect the IO:Prompt module had this functionality for the same reason.