First I will explain how this program works : When a user wants to create a new user, he needs to type his username. Then this loop will check the file users.txt to see if that username already exists. If so, the user has to type a new username. The file users.txt has entries like this :
test:lamda beta:alpha
test = username and lamda = password, seperated by a : However, this loop works perfectly locally. When I try to do this in a client server kind of way, the loop only checks the first 2 lines, and wont continue after that :( The problem lies in this line at the beginning of the entire script:
$/ = "\015\012";
I had to define this to make sure I would accept proper data from $client. However, when the script checks the file it messes up with the spaces and newlines, because of this defined variable. So I thought I could easily alter that variable before it checks the file like this :
$/ = "\012";
And change it back later. Now it only checks the first 2 lines of the users.txt file, and not the others.. :( Iam totally stuck. Can anyone help me? The loop Iam talking about is this :
$/ = "\012"; open (FILE1, "users.txt") || print "Error occured while opening file"; while (<FILE1>) { ($first,$second ) = split /:/; print $first; if ($login eq $first) { print $client "Sorry this username has been taken, try again!\n\r"; &login; }
This is the entire script so far:
#!/bin/perl use IO::Socket; use IO::Select; $/ = "\015\012"; $nconnections_new = 0; $login = ""; $maxconn = SOMAXONN; $local=IO::Socket::INET->new(Proto=>"tcp", LocalPort=>"1234", Listen=> +$maxconn, Reuse=>1) or die "Can't open socket\n"; while ($client = $local->accept) { $client->autoflush(1); &start; sub start { print $client "This is a test BBS!\n\r"; print $client "(C)reate new user (L)ogin"; print $client "\n\r"; my $option=<$client>; chomp $option; $option="\U$option\E"; if ($option eq "C") { print $client "<<<Create new user>>>\n\r"; print $client "Login must be at least 6 characters.\n\r"; &login; } elsif ($option eq "L") { &logon; } else { &start; } sub login { $/ = "\015\012"; print $client "username:"; $login=<$client>; chomp $login; if ($login eq "") { print $client "Must enter a username!\n\r"; &login; } $/ = "\012"; open (FILE1, "users.txt") || print "Error occured while opening file"; while (<FILE1>) { ($first,$second ) = split /:/; print $first; if ($login eq $first) { print $client "Sorry this username has been taken, try again!\n\r"; &login; } $/ = "\015\012"; } close (FILE1); if (length($login) > 7) { print $client "Your username was longer than 6 characters!\n\r"; &login; } $pass = password(); sub password { print $client "Password:"; $password=<$client>; chomp $password; print $client "\n\rRe-enter password:"; $password2=<$client>; chomp $password2; if ($password ne $password2) { print $client "Passwords do not match, try again!"; &password; } return $password; } open (FILE, ">>users.txt") || print "Error occured while opening file" +; print FILE "$login:$pass\n"; close (FILE); print "File written..\n\r"; exit; } } sub logon { print $client "test"; } close $client;

In reply to Variable messes up loops. by thran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.