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

Hello guys I need my script to die if the user hits carriage return or the enter keys. I have tried verious ways but can't seem to get it to work.
die "The number is not valid\n" if $number =~ /\D/; die "Enter is not valid" if $number =~ /\r/; die "Enter is not valid" if $number =~ /^M/;

Replies are listed 'Best First'.
Re: die on carriage return
by Perlbotics (Archbishop) on Dec 31, 2008 at 11:23 UTC

    The problem is probably located a few lines earlier. You could chomp $number before performing the tests. Then check for an empty input, e.g. like:

    die "Enter is not valid" unless length $number;

      Or even:
      die "Enter is not valid" unless $number;
      Since an empty string is false.

      Update: Thanks to Perlbotics for pointing that this would mark 0 as invalid. Doh!
        I thought zero was a number.
        Hello Thanks that works quite well now, quite simple really :)
Re: die on carriage return
by linuxer (Curate) on Dec 31, 2008 at 11:22 UTC

    How do you read the user's input? Is it a command line or a network tool, is it a GUI tool (TK, Gtk)?

    As \D includes all non-numerical characters, the newlines [\r\n] should already be included in that.

    I would avoid sequences like ^M and use \r and/or \n instead for newlines (or the real hex-values).

Re: die on carriage return
by Bloodnok (Vicar) on Dec 31, 2008 at 11:17 UTC
    die "The number is not valid\n" if $number =~ /\n/; ??

    die "The number is not valid\n" if $number =~ /^\s*\n/; ??

    Update:

    Looks like Perlbotics was busy proof reading a few responses - thanx to him for pointing out that the RE as I posted would invalidate all text - amended accordingly.

    A user level that continues to overstate my experience :-))
Re: die on carriage return
by cdarke (Prior) on Dec 31, 2008 at 11:19 UTC
    Most terminal drivers convert carriage return to a newline (on UNIX, see stty(1)), so test eq "\n". There should be no need for a regular expression.
      Wouldn't that have a tendency to remove some of the platform agnosticism - assuming that's a design/implementation goal ... as it seems to be in most of the projects I've worked on ??

      A user level that continues to overstate my experience :-))
Re: die on carriage return
by zentara (Cardinal) on Dec 31, 2008 at 13:00 UTC
    You might find using a thread handy. Also, depending on what your code needs to do, you might find Readkey with timer using Glib useful.
    #!/usr/bin/perl use warnings; use strict; use Term::ReadKey; use threads; $|++; ReadMode('cbreak'); # works non-blocking if read stdin is in a thread my $count = 0; my $thr = threads->new(\&read_in)->detach; #do other stuff while thread watches keyboard while(1){ print "test\n"; sleep 1; } ReadMode('normal'); # restore normal tty settings sub read_in{ while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "\t\t$char->", ord($char),"\n"; #process key presses here if($char eq 'q'){exit} #if(length $char){exit} # panic button on any key :-) } } } __END__

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: die on carriage return
by ww (Archbishop) on Dec 31, 2008 at 13:07 UTC

    TIMTOWTDI

    #!/usr/bin/perl -w # die on input eq <CR> ("Enter") # [id://733443] while (<STDIN>) { $in = $_; chomp ($in); unless (length($in)) { print "Enter is a command to quit" ; exit; } else { print "\$in is $in\n"; } }
Re: die on carriage return
by Marshall (Canon) on Jan 01, 2009 at 04:33 UTC
    This sounds to me like a very, very strange UI request. Normally a blank input from the user should be a no op or a re-prompt. But I think this will do what you you have asked for although I'm not sure that this is what you need:
    while ( (my $inputLine =<STDIN>) !~ /^\s*$/ ) { print $inputLine; } print "WOW null line (only spaces or nothing) - I've bailed!\n";

    \s matches: space,\n,\r,\t,\f.