Thanks Paul!


                                                                                       
                               
                    "Gowers, Paul                                                      
                               
                    (CRTLDN)"              To:     'Philip Morley' 
<[EMAIL PROTECTED]>                        
                    <Paul.Gowers@co        cc:                                         
                               
                    ncert.com>             Subject:     RE: Pattern matching null 
strings                             
                                                                                       
                               
                    29/04/02 15:16                                                     
                               
                                                                                       
                               
                                                                                       
                               



Phil


  Try this:


print "Please enter the string: ";
    $Response = <STDIN>;
    chomp $Response;
    if ($Response !~ /^\w+$/) {
      warn "Error - string not valid.\n";
    }


The regexp is looking for one or more word characters between the start and
end of the value held in $Response. This disallows an empty string as well
as any value containing whitespace, punctuation etc. If you wanted to be
more specific, you could use a character class instead of \w (eg [A-Za-z]
to allow only alphabetic characters).


Cheers
Paul


-----Original Message-----
From: Philip Morley [mailto:[EMAIL PROTECTED]]
Sent: 29 April 2002 15:05
To: [EMAIL PROTECTED]
Subject: Pattern matching null strings





Hi,


Can someone show me a more elegant way of validating user input than this:


    print "Please enter the string: ";
    $Response = <STDIN>;
    chomp $Response;
    if (($Response =~ /\W/) || ($Response eq "")) {
      warn "Error - string not valid.\n";
    }


Basically I want the $Response to be only a word character and not null
(i.e. warn if the user just presses return).  Is there a pattern match I
can use so that I don't have to explicitly check if eq ""?


Thanks in advance,


Phil Morley





_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs




_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to