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;
| [reply] [d/l] [select] |
|
|
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! | [reply] [d/l] |
|
|
I thought zero was a number.
| [reply] |
|
|
Hello
Thanks that works quite well now, quite simple really :)
| [reply] |
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).
| [reply] [d/l] [select] |
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 :-))
| [reply] [d/l] [select] |
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. | [reply] [d/l] |
|
|
| [reply] |
Re: die on carriage return
by zentara (Cardinal) on Dec 31, 2008 at 13:00 UTC
|
#!/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__
| [reply] [d/l] |
Re: die on carriage return
by ww (Archbishop) on Dec 31, 2008 at 13:07 UTC
|
#!/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";
}
}
| [reply] [d/l] |
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.
| [reply] [d/l] |