Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle backslash in user input 1

Status
Not open for further replies.

cyan01

Programmer
Mar 13, 2002
143
0
0
US
Experts,

I need to develop a perl code to catch invalid user inputs. In my case, a backslash ("\") is considered invalid.

Here is my code:

Code:
my @invalidCharList = ('-', '\+', '\.', '/', '\\');
#my @invalidCharList = ('-', '\+', '\.', '/');
print "$0..testing..., @invalidCharList\n";

print "Enter something: ";
while(1)
{
  my $input = <STDIN>;
  chop($input);
  print "\$input = $input\n";

  my $ii = 0;
  foreach my $c (@invalidCharList)
  {
    if($input =~ /$c/ && $Q::mode ne 'edit')
    {
      warn("catch!!! \$ii = $ii, \$input = $input, \$c = #$c#");
    }
    $ii++;
  } # end of foreach my $c (@invalidCharList)

  if($input eq "q")
  {
    print "exit...\n";
    exit;
  }
  print "\nEnter something again: ";
}


Here is the output from a test run:

Code:
% ./catchBackSlash.pl
./catchBackSlash.pl..testing..., - \+ \. / \
Enter something: a
$input = a
[red]Trailing \ in regex m/\/ at ./catchBackSlash.pl line 17, <STDIN> line 1.[/red]

Could someone tell me how to fix this problem? Many thanks!
 
try defining your expressiosn like this
Code:
my @invalidCharList = ('-', '\+', '\.', '/', "\\\\");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top