Reading from a text file: problems in removing the terminating character

Hi Experts,

I am having some problem in removing the newline character from the string that is read from a text file.  I have the source code below.  The problem is if I have the username password pair as myname:password it says that the password has 9 characters.  The chomp seems to not remove the newline character.  Please provide me with some advice as to solve the problem.  This problem preventing me from going further with my assignment.

Thanks a lot,

#!/usr/bin/perl --

use CGI qw( :standard );  

$dtd =

$theUsername = param( "username" );

$thePassword = param( "password" );

open(INFILE, "<password.txt") or die("The database could not be opened.");
$count = 0;
$userVerified = 0;
$passwordVerified = 0;

chomp $thePassword;
while (( $line =<INFILE> ) && (!$userVerified )) {
    $count = $count + 1;
    chomp( $line );
    ( $username, $password ) = split( /:/, $line );


    print( header() );
     if ( $theUsername eq $username ) {
           print("User name matches");
             $len = (length($password));
             print("Length of password is: $len");

             $theLen = (length($thePassword));
             print("Lenth of input password is: $theLen");

         if ( $thePassword eq $password ) {
                print("Password matches");
                $passwordVerified = 1;
             print("The password don't match");

print( header() );

#print( start_html( { dtd => $dtd,title => "First Perl Script" } ) );
print("thePassword: $thePassword");
print("The user name is: $theUsername\n\n");
print("The user name is verified: $userVerified\n");
print("The password is verified: $passwordVerified\n");


print("The password is: $password");
print("The user name processed: $username");
print("The count is: $count");
#print( end_html() );

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thaneshAuthor Commented:
As I investigate it further I found that the chomp does take out the newline character, but there seems to be TWO newline characters.  So, the problem is how to get rid of the second one?
thaneshAuthor Commented:
May be the second one is not a newline character but some other trailing character( non-visible)???
Brief description of chomp: chomp basically removes what is in $/.  if $/ is empty then it removes all newlines from the string.

Since you are reading the file one line at a time then there shouldn't be a problem with mutliple newlines.  Your last post is probably accurate.  It may be something like a space, tab, or any other variable.  If this is a problem for you then you may consider doing something like

$password =~ s/^([\d\w]+).*/$1/;

Basically take what are valid characters and remember then and ignore everything else.  Note that if you have control over what goes into your file then you may want to make sure information is put in correctly.  Let me know if this helps or you have problems with this.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
\d is included in \w
If  password.txt was created under DOS it may use \r\n to end a line
I agree with ozo, dos/windows puts a CR/LF (Carriage Return + Line Feed) as a new line.

Unix-like systems look only for LF and I think MACs look for the CR.

That's also why text files will have a larger size on a windows machine, yet smaller on a unix-like system if you transfer them to and fro in ascii mode.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.