Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to exit only when enter is pressed by user

so I am doing something like this

print "Please press enter to quit \n"; my $fi = <STDIN>; chomp $fi; if ($fi eq \n ) { exit; } else { while ($fi) { print "Please press enter to quit \n"; $fi = <STDIN>; chomp $fi; if ($fi eq \n ) { exit() } } }

but it is not working because i presume this command

if ($fi eq \n )
is wrong , so can anyone tell me its alternative

Replies are listed 'Best First'.
Re: Press enter to exit
by McA (Priest) on Jul 27, 2013 at 06:26 UTC

    Hi

    as always, put this on top of your script:

    use strict; use warnings;

    and you get the first hints for your error.

    Anyway: When you chomp the input, the line seperator is removed. So, when someone is hitting enter without providing some text, the string is empty. So check against an empty string.

    exit if $fi eq '';

    Best regards
    McA

Re: Press enter to exit
by kcott (Archbishop) on Jul 27, 2013 at 08:52 UTC

    I see ++McA has provided pointers to problems with your code: all good advice and probably resolves your current issues.

    While it's good that you've shown a minimal example for describing your problem, I wonder if it's a little too minimal in the sense that it masks what you're really trying to do. In the context of your actual application, is "press enter to quit" really prompting the user with something closer to one of these:

    1. "when you're finished reading the output displayed, press enter to quit (which will remove the window with the output and return you to the commandline)"
    2. "enter more input or, when ready to process your supplied data, press enter to quit data entry"
    3. "press enter and no other key: the program is trying to determine if your enter key works properly"

    Obviously, those are somewhat wordy, probably not the actual text you'd use and really only meant to highlight context and intent. Also, there may be other reasons for this prompting of the user.

    For 1: you really don't care what the user enters and something as minimal as this is probably fine (untested):

    print 'Hit enter when done: '; <>; exit;

    For 2: perhaps code more like this (untested):

    print "Type input (hit just enter when done):\n"; while (<>) { chomp; last unless length; # validate/store input here }

    For 3: your original code is possibly closer to the mark in this scenario.

    See also: IO::Prompt::Tiny (for simple prompting) and IO::Prompt::Hooked (for prompting with validation).

    Finally, in the two instances of printing the prompt in your code, you don't want a newline at the end of the prompt string.

    With print "Prompt:\n"; the user sees

    Prompt:
    _
    

    With print 'Prompt: '; the user sees

    Prompt: _
    

    Update: Added chomp; to "For 2:" code.

    -- Ken

Re: Press enter to exit
by 2teez (Vicar) on Jul 27, 2013 at 10:19 UTC

    Hi Anonymous Monk,
    Just one 'kobo' note in addition.

    but it is not working because i presume this command if ($fi eq \n )
    Yes, you presume right, for

    1. Though there is \n, but it usage is not literal. Am sure you must have seen that it is used in a double quote like this "\n" in this case. See this for more.
    2. After you have use chomp, your variable is no longer "\n".See McA's wisdom above.
    3. kcot showed wonderful and better alternative for doing what you wanted. However, if you still want to follow through on what you initially wrote then, DRY i.e Don't Repeat yourself!!!. i.e Instead of writing this
      print "Please press enter to quit \n"; my $fi = <STDIN>; chomp $fi
      as many times as possible, put it in a sub, then call the sub, each time you need it.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Press enter to exit
by hippo (Archbishop) on Jul 27, 2013 at 09:31 UTC

    A quick tip to improve the legibility of your code is to avoid doing this:

    if ($fi eq "\n") { exit; } else { # some other code here }

    There is no need for the else when the first branch terminates, so you can just write

    if ($fi eq "\n") { exit; } # some other code here

    This applies for any terminating action just as exit, die, croak, return, etc. I find that it especially adds clarity if you have several such branches in succession.

      if ($fi eq "\n") ## WRONG in this case { exit; }
      The value of the variable $fi is not "\n" but an empty string, though in the script provided by the OP, if ($fi eq "\n") { .. still works. Note that "chomp", already removes "\n".
      A simple test, shows this clearly like thus:
      use warnings; use strict; use Test::More 'no_plan'; print "Please press enter to quit \n"; my $fi = <STDIN>; chomp $fi; is $fi, "\n",'should fail'; if ( $fi eq "\n" ) { exit; }
      result shows this:
      Please press enter to quit not ok 1 - should fail # Failed test 'should fail' # at textme.pl line 9. # got: '' # expected: ' # ' 1..1 # Looks like you failed 1 test of 1.
      If you look at the got and expected in the above, you get the point.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
Re: Press enter to exit
by zork42 (Monk) on Jul 28, 2013 at 05:50 UTC
    A modification to kcott's case 1:

    This will always prompt you to press enter, no matter how the script exits, even if an error is encountered.
    This can be useful if the script is running in its own window which closes when the script exits.
    use strict; use warnings; # ... my $x = 1 / 0; # ... END { print "\nHit enter when done: "; <>; exit; }
    Output:
    Illegal division by zero at D:\stuart\play\temp\pp.pl line 6. Hit enter when done: