Loading ...
Sorry, an error occurred while loading the content.

Struck while capturing "backspace".

Expand Messages
  • Sreeram B S
    Hello, I am writing an password entry script in Perl. When the user enters the password, the asterisk (*) will be printed on the screen when he types an
    Message 1 of 3 , Aug 31, 2005
    • 0 Attachment
      Hello,
      I am writing an password entry script in Perl. When the user enters the password, the asterisk (*) will be printed on the screen when he types an character. The character which he has actually typed will be internall stored in a string. But when he presses "backspace" (if he has done a mistake), the * is getting printed. I intend to erase one asterisk, when the "backspace" key is hit. But I am not able to capture/handle the stroke of the backspace key. I dont know as to what conditional must be used to check whether the backspace was hit.

      -----------------------------------------------------------------------------------
      Code snippet:

      #!/usr/bin/perl
      use strict;
      my $key;
      my $word;
      system "stty", "-icanon", "eol", "\001";
      system "stty -echo";
      print "Password: ";
      while ( ($key = getc(STDIN)) ne "\n" ) {
      print "*";
      $word .= $key;
      }
      print "Word entered is $word\n"; ## This will be crypted later.
      system "stty","icanon", "eol", "^@"; # ASCII null
      system "stty echo";

      -------------------------------------------------------------------------------------------------

      Now, with in the while loop above, I need to make an arrangement to see if the backspace is hit and take up appropriate action. How can I achieve this?

      Kindly suggest.

      Thanks,
      Sreeram



      ---------------------------------
      Start your day with Yahoo! - make it your home page

      [Non-text portions of this message have been removed]
    • Shawn Corey
      ... use warnings; ... if( $key eq b ){ if( length( $word )){ print b b ; $word = substr( $word, 0, -1 ); } }else{ ... } ... print n ; ... NOTES: b ,
      Message 2 of 3 , Sep 1, 2005
      • 0 Attachment
        Sreeram B S wrote:
        > #!/usr/bin/perl
        > use strict;

        use warnings;

        > my $key;
        > my $word;
        > system "stty", "-icanon", "eol", "\001";
        > system "stty -echo";
        > print "Password: ";
        > while ( ($key = getc(STDIN)) ne "\n" ) {

        if( $key eq "\b" ){
        if( length( $word )){
        print "\b \b";
        $word = substr( $word, 0, -1 );
        }
        }else{

        > print "*";
        > $word .= $key;

        }

        > }

        print "\n";

        > print "Word entered is $word\n"; ## This will be crypted later.
        > system "stty","icanon", "eol", "^@"; # ASCII null
        > system "stty echo";

        NOTES:

        "\b", like "\n", is a special character. It means a backspace character
        (ASCII \x08). You cannot use this in a regular expression since \b means
        a word boundary in regular expressions. Use \x08 instead.


        --- Shawn
      • Sreeram B S
        Shawn Corey wrote: ... use warnings; ... if( $key eq b ){ if( length( $word )){ print b b ; $word = substr( $word, 0, -1 ); }
        Message 3 of 3 , Sep 1, 2005
        • 0 Attachment
          Shawn Corey <shawn.corey@...> wrote:

          Sreeram B S wrote:
          > #!/usr/bin/perl
          > use strict;

          use warnings;

          > my $key;
          > my $word;
          > system "stty", "-icanon", "eol", "\001";
          > system "stty -echo";
          > print "Password: ";
          > while ( ($key = getc(STDIN)) ne "\n" ) {

          if( $key eq "\b" ){
          if( length( $word )){
          print "\b \b";
          $word = substr( $word, 0, -1 );
          }
          }else{

          > print "*";
          > $word .= $key;

          }

          > }

          print "\n";

          > print "Word entered is $word\n"; ## This will be crypted later.
          > system "stty","icanon", "eol", "^@"; # ASCII null
          > system "stty echo";

          NOTES:

          "\b", like "\n", is a special character. It means a backspace character
          (ASCII \x08). You cannot use this in a regular expression since \b means
          a word boundary in regular expressions. Use \x08 instead.

          Hi friends,

          I am extremely glad to say that this program has worked!! This was one of my aims (goals), ie to write a password program which would print "*" when an user types his password's characters, but I could not achieve this inspite of my efforts. But when I resorted to Perl, this has worked. I think Perl has solutions to everything !!

          This was possible only because of the support extended by the people in this team. I am greatful to this team !!

          Thanks a lot,

          Sreeram




          ---------------------------------
          Yahoo! Mail
          Stay connected, organized, and protected. Take the tour

          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.