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

hi, I need to get the passwords from the predefined file say for example file name passwordfile, in that i have like username scott tiger by the following code it should give proper format like
open (PSSWD, "<passwordfile") or die "could not able to open the file" + ; while ($line=<PSSWD>) { chomp($line); } ($uname,$password) = $line = ~ m/username\s+(\S+)\s+(\S+)\s*\S*/; close(PSSWD); printf("uname %c= $uname"); printf("\npassword=$password");
but it is give an unpredictable output
hspw = 18446744073709551615 password=
Please let me know where the error is

Edited (davorg): Moved to root node. Added code tags

Replies are listed 'Best First'.
Re: How Do I Read Data From A File?
by Melly (Chaplain) on Dec 05, 2006 at 12:33 UTC

    Assuming that your password file has a format like (since this matches your regex):

    username tommelly opensesame some optional junk

    Then...

    open (PSSWD, "<passwordfile") or die "could not able to open the file" + ; while ($line=<PSSWD>) { chomp($line); ($uname,$password) = $line = ~ m/username\s+(\S+)\s+(\S+)\s*\S*/; print "username: $uname, password: $password\n"; }

    A problem with your version is that you only handled the last username/password in the file - printf is not required, but may account for the lack of password in the output shown (I don't often use printf and am not sure what it does in the context you use), or the format of your password file might not match your regex.

    There are other problems with your script that will make it harder for you to debug - mainly you don't check whether your regex actually matched against the line... I'd suggest...

    open (PSSWD, "<passwordfile") or die "could not able to open the file" + ; while ($line=<PSSWD>) { chomp($line); if($line = ~ m/username\s+(\S+)\s+(\S+)\s*\S*/){ print "username: $1, password: $2\n"; } else{ print "$line did not match regex\n"; } }
    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk
Re: How Do I Read Data From A File?
by davorg (Chancellor) on Dec 05, 2006 at 12:05 UTC

    When posting a new question, please post it as a new discussion, not as a reply to an existing question. I have moved this node so it is no longer a reply to a completely unrelated question.

    Please use <code> tags to mark code and make your question easier to read. I have added code tags to this node.

    You might also consider renaming this node. Your current title isn't very helpful and people might ignore it.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

passwords in Re: How Do I Read Data From A File?
by ww (Archbishop) on Dec 05, 2006 at 16:13 UTC
    You may wish to note that your question and your regex appear to rely on passwordfile actually containing passwords in a human readable format.

    Some do.

    But that's an exceedingly insecure (read: almost useless) way to keep a password file. Anyone who can access it can read the passwords. Mere encoding or even encrypting isn't much better.

    OTH, if the system being protected by the p/w file hashes the p/w (with an unreverseable -- well "unreversable"-so-far-as-we-know-right-now -- algorithm and stores the value of the hash rather than the p/w itself in the p/w file, future ID checks can be performed by hashing the p/w entered at the challenge (login) screen and seeing if it matches the hash in the p/w file. Of course, that doesn't begin to solve all the possible problems (interception between keystroke and transmission, interception en route, etc., etc., etc....).

    As to the insufficient info in your question and questionable regex technique, see the replies above.

Re: How Do I Read Data From A File?
by madbombX (Hermit) on Dec 05, 2006 at 16:00 UTC
    If you are trying to work with specific password files, then you may want to have a look at any of the vast number of perl modules that already have this functionality created. If the password is a *nux password file, then take a look at Passwd::Linux, Passwd::Solaris, or Unix::PasswdFile to name just a few. If it's for apache, check out Apache::Htpasswd. It would also be helpful if you could have provided us with some example data.