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

Dearest perl Monks, I wrote this simple subroutine in order to add users to a small network program to which I needed to add authentication . When I call the subroutine, the user enters both username and password , the password is encrypted, and the script prints the combination.Or, that is what is SUPPOSED to happen; The problem is that the entire combination string ,stored in $out,does not print. As it is below it prints ".:encryptedpassword" instead of "username:encryptedpassword" .If I set $out to $username only it just prints the username. if I set $out to $username\n, it prints $username MINUS the first two chars.. I have tried turning off line buffering on the output filehandle, PASSWD, and this does not appear to be the problem. The code is below, any help would be appreciated, thanks :
sub adduser{ $passwd = shift; my ($out,$pw); my @salt = ("A".."Z","a".."z","0".."9","/",",","."); USER:{ print CLIENT "New UserName:"; my $username=<CLIENT>; if ($username =~ /\S+/){ chomp $username;} else{goto USER; } $out = "$username" ; } PASS:{ print CLIENT "New User Password:"; my $password=<CLIENT>; if ($password =~ /\S+/){ chomp $password;} else{goto USER; } my $salt = $salt[rand @salt].$salt[rand @salt]; my $pw = crypt($password,$salt); $out = "$out:$pw\n"; } open PASSWD, ">> $passwd" || die" problem appending to $passwd :$! \n"; print PASSWD "$out"; close PASSWD; }

Replies are listed 'Best First'.
Re (tilly) 1: print() wierdness
by tilly (Archbishop) on Apr 04, 2001 at 21:20 UTC
    You appear to be reading and writing to a filehandle named CLIENT which you are assuming is already open and you are pulling in by a global. That probably is not going to work, and if it does it likely is not doing what you think it is.

    Aside from that you are using globals everywhere, goto rather than redo, so on and so forth. For your sanity I strongly recommend that you never again use a goto (if you have any construct that you feel demands it then ask here for how to do it more sanely without), use strict, declare variables lexically with my, and so on.

Re: print() wierdness
by dws (Chancellor) on Apr 04, 2001 at 21:19 UTC
    This works for me (with Perl 5.005 on Win32) if I use STDIN/STDOUT. I don't see anything obviously wrong.

    By chance are you putting CLIENT into binmode()? If so, chomp() may be leaving a \r at the end of your input strings, and this might be screwing up your printed presentation.

Re: print() wierdness
by isotope (Deacon) on Apr 04, 2001 at 21:23 UTC