PDA

View Full Version : stupid problems with strings


Whiteknight
05-13-2004, 12:52 AM
I'm having some stupid problems with some string functions, for some reason, and i just have been staring at this code for too long now to find my mistakes. here is the code:


open(FILE, $file_location);
$FILE = <FILE>;
print $FILE; #first test
close(FILE);
if ( !($FILE == $test_variable ) )
{
print "value incorrect";
exit;
}
...


now, what i am trying to do is set up a rudimentary password protection here. the $file_location points to a text file with a single line of text, that is a password. the value $test_variable is input from the command line, and is the password that is being tested.

the $file_location is tested for existance prior to this routine.

now, the problem is that every password gets counted as correct, and the program never exits. the only time the program exits is if there is no password, ie the entered string is blank.

if i change the "==" in the if statement to the string operator "eq" then it says that the password is always wrong, and the correct password gets counted as wrong.

any ideas?

iDxMan
05-13-2004, 10:04 AM
Any CR/LF's on the end of the line in your password file or $test_variable?


Maybe try chomping them.


chomp($FILE);
chomp($test_variable);

Whiteknight
05-13-2004, 10:17 AM
well crap, that worked like a charm.

althought, i think as soon as i implemented your suggestion, i decided it might be easier to use some sort of IP address authentication instead of a password.

haha, thanks though, i cant beleive i forgot that ****.

iDxMan
05-14-2004, 01:09 AM
Yeah, that one has bitten me a number of times as well. These days try to make it a strict habit of clensing the vars before using them.

Just something simple like:


while(<FILE>) {

chomp;
s/\r//g;


@line = split(/\s+/);
}


can make a difference when someone [for example] ends up saving your input file with DOS CR's in it.

-r