Gautam Kumar
Gautam Kumar

Reputation: 1170

Issue with "#" in perl

I am facing an issue which I am not able to understand with the subroutine below:

sub password()
{
$StdIn = new Win32::Console(STD_INPUT_HANDLE);
my $Password = "";

$StdIn->Mode(ENABLE_PROCESSED_INPUT);
print "Enter Password: ";

    while (ord(my $Data = $StdIn->InputChar(1)) !=10) 
    { 

               if("\r" eq $Data )
            {
            last;
        }
         elsif ("\ch" eq $Data)
         {
            if( "" ne chop( $Password ))
             {
                print "\ch \ch";
             }
            next;
        }
    $Password .=$Data;
    print "*";
    }

 return $Password;

}

The above script works fine with everything unless password contains "#". If it contains "#" the main subroutine which calls the above subroutine does not get connected with the tool, that I need to automate. Although the tool has no problem with # — "it works fine with #" when I use it manually. So I think there is some issue with Perl itself. Can you please help?

Upvotes: 1

Views: 130

Answers (2)

Sinan Ünür
Sinan Ünür

Reputation: 118158

First off, use Term::Prompt instead of messing around with the console yourself.

#!/usr/bin/env perl

use strict; use warnings;
use Term::Prompt;

my $pass = prompt P => 'Password: ', undef, undef;
print "$pass\n";

As for your problem, I am assuming the problem is in the part you don't show. However, note

  1. Use Win32::Console->new rather than indirect object syntax.

  2. Do use strict and warnings.

  3. The * characters you're printing will not appear until after the password has been entered. Use local $| = 1 before your while if you insist on writing C in Perl.

Upvotes: 3

Struppi
Struppi

Reputation: 163

Did you try to show the input? Just write print $Data; to see if it accept the input.

Upvotes: 0

Related Questions