Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Password Entry using Core modules only?

by raybies (Chaplain)
on Mar 12, 2013 at 17:17 UTC ( [id://1023021]=perlquestion: print w/replies, xml ) Need Help??

raybies has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way in Perl to receive user input, but the characters don't echo to the screen using core modules (perl 5.10.1)? (Like if I wanted to have them enter a password without it being seen...)

I know about Term::ReadKey but unfortunately I can't molest the build that's on these install machines.

--Ray

  • Comment on Password Entry using Core modules only?

Replies are listed 'Best First'.
Re: Password Entry using Core modules only?
by marto (Cardinal) on Mar 12, 2013 at 17:26 UTC

      Marto, thanks for the pointer... I suppose I should hang my head in shame that it was in the FAQ... but here's a little summary of what I found...

      So the FAQ says that if you don't use Term::ReadKey you're stuck with ioctl (which all the documentation I've read thusfar seems to indicate I should avoid it like the plague... that it's nonportable, really cryptic, and difficult to use... made me dizzy...) nor does the FAQ really answer how in the world one uses ioctl() to do what i was asking (nor does the documentation on the command itself... it also mentions POSIX, but again, that's the whole POSIX system that it references, and my use case might've been in there somewhere... but sadly I lost the will to keep looking.

      Luckily the Camel book mentions I can make a system call to stty -erase if I'm lucky enough to have that function supported by my OS (which I am! Yay! so I'll be using that...)

      here's what worked for me...

      print "Enter password: "; system "stty -echo"; my $passwd = <STDIN>; chomp $passwd; system "stty echo"; print "Fool! I'm telling everyone your password is: $passwd\n";

      Of course that requires those system commands to function as they do on your system... as they do on mine.

      Anyhow thanks for the help, --Ray

      ps if anyone has done an ioctl implementation, I'd love to see what it looks like... cuz the examples in that section were pretty gnarly.

        The FAQ I linked to states in the first line:

        "There's an example of this in crypt"

        Which contains some code very similar to your solution.

        But stty is practically ioctl() in disguise??
Re: Password Entry using Core modules only?
by tobyink (Canon) on Mar 12, 2013 at 20:20 UTC

    This works on my machine. No guarantees about it working anywhere else...

    use v5.12; use POSIX; my $termios = POSIX::Termios->new(\*STDIN); $termios->getattr; my $lflag = $termios->getlflag; $termios->setlflag($lflag & ~(&POSIX::ICANON) ); $termios->setattr; my $pwd; while (my $c = getc STDIN) { last if $c eq "\n"; print "\r \r"; $pwd .= $c; } $termios->setlflag($lflag); $termios->setattr; warn "GOT: $pwd\n";
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1023021]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found