Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visually identical strings not matching against 'ne' ...?

Status
Not open for further replies.

PerlIsGood

Programmer
Jan 24, 2002
154
US
Here's an excerpt of the script:

Code:
use strict;
use CGI;

my $q = new CGI;
my %query = $q->Vars();
my $userdir = "./ns_forum/profiles";

if ( -e "$userdir/$query{'user'}.dat" )
{
  open USR, "$userdir/$query{'user'}.dat" ||
    &err_sub('Error', "Failed to open user file: $!");
  chomp ( my @user_file = <USR> );
  close USR;

  foreach ( @user_file )
  {
    my ( $key, $value ) = split /\|/;
    $user{$key} = $value;
  }

  if ( $user{'pass'} ne $query{'pass'} )
  {
    &err_sub('Error', 'Password invalid.',
      &quot;\$query{'pass'}=$query{'pass'}&quot;,
      &quot;\$user{'pass'}=$user{'pass'}&quot;);
  #the added strings passed to sub for debugging
  }

} else {
  &err_sub('Error', &quot;Complain 'cause it's fun: $!&quot;);
}

So if a user file has the passwd abc123, and the user enters abc123 into the form field, the script dumps into the $err_sub routine for 'Password Invalid'. I printed the values for debugging and they're the same. What am I missing?

(I tried using != just to see what happens, but that has a bad habit of letting incorrect passwds through...I also tried tossing an extra chomp around $query{'pass'} and $user{'pass'} without luck.) Notorious P.I.G.
 
Yeah, != is for numeric only.
Code:
abc123 != 0
123abc != 123
are both false. It converts the scalar string to a number left-to-right. The extra chomp is a good call, would have been my first suggestion. Only things I can think of now is to first print out the length of each string, make sure there aren't any non-printable hidden charcters. Then next would be to dump them out with specific formatting using perl's pack function. Maybe each character as an integer? Just some thoughts. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Ok, I spit out the length of the 2 strings and found that there was indeed a newline in there (i overlooked it when reviewing the source spit out by the $err_sub routine).

After inserting a s/\n|\r// line before the user check everything ran fine. My question is: why did chomp miss this newline?

Anyway, thx for the suggestions. That pointed me in the right direction. Notorious P.I.G.
 
It might have been out of scope...
Code:
my @user_file = <USR>; chomp @user_file;
Or perhaps chomp only takes a string as input...
Code:
my $user_file; while (<USR>) {chomp; $user_file .= $_;}
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top