Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Accessing a file from the perl

by abcdefg (Acolyte)
on Jan 11, 2012 at 22:43 UTC ( [id://947447]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Accessing a file from the perl
by abcdefg (Acolyte) on Jan 11, 2012 at 22:53 UTC

    I have a client and server application.MY client is sending the username(which it gets through getlogin()) to server.I want server to save all the registered username in a file called username.On receiving the request from client,it checks the username of client in the file and if it founds it ,its gives him permission to go ahead otherwise rejects it. here is what i did:

    $user = <$client_socket>; chop($user); #remove the \n print "$prefix USER = $user\n"; unless (open (USERNAME, "username")) { print "ERROR:Cannot open username file.\n"; exit(1); # ERROR } while(<USERNAME>) { my @newusername = <USERNAME>; foreach $name (@newusername) { if($name ne $user) { print $client_socket "ERR\n"; print "$prefix ERROR: user $user not permitted to use this mode +. Exiting.\n"; close(USERNAME); exit(1); } } } #else, continue print $client_socket "ACK\n"; # approve the user. Limit this in + the future. close(USERNAME);

    this isnt working as even when the file username has client name it is giving "user $user not allowed".

      Your main problem is that you're checking ne

      if($name ne $user) {

      so, unless every entry in the USERNAME file holds the name in question, this will match and quit with ERR.

      Also, your way of reading the file

      while(<USERNAME>) # reads first entry { my @newusername = <USERNAME>; # reads remaining entries foreach $name (@newusername) ...

      skips the first entry for the comparison...

      Try something like this:

      ... my $found; while (my $name = <USERNAME>) { if ($name eq $user) { $found = 1; last; } } unless ($found) { print $client_socket "ERR\n"; ... exit; } print $client_socket "ACK\n";

      And either chomp both $user and $name, or none.  As you have it, $name has a trailing \n, while $user hasn't.

        $user = <$client_socket>; chomp($user); #remove the \n print "$prefix USER = $user\n"; unless (open (USERNAME, "username")) { print "ERROR:Cannot open username file.\n"; exit(1); # ERROR } my $found; while(my $newusername = <USERNAME>) { chomp($newusername); if($newusername eq $user) { $found = 1; last; } unless ($found) { print $client_socket "ERR\n"; print "$prefix ERROR: user $user not allowed Exiting. +\n"; close(USERNAME); exit(1); } } #else, continue print $client_socket "ACK\n"; # approve the user. #print "user $user is allowed."; close(USERNAME);

        I tried what you said as above but still it got same problem..still its not goin on else part where user is approved.

Re: Accessing a file from the perl
by tobyink (Canon) on Jan 11, 2012 at 22:55 UTC

    Your code isn't properly formatted, so it's not easy to see what you're trying to post. But you don't seem to be stripping the line break characters from the lines you read from the file.

      Did you mean to say adding a chomp function after reading the file,like below:

      $user = <$client_socket>; chop($user); #remove the \n print "$prefix USER = $user\n"; unless (open (USERNAME, "username")) { print "ERROR:Cannot open username file.\n"; exit(1); # ERROR } while(<USERNAME>) { my @newusername = <USERNAME>; chomp($newusername); foreach $name (@newusername) { if($name ne $user) { print $client_socket "ERR\n"; print "$prefix ERROR: user $user not permitted to use +this mode. Exiting.\n"; close(USERNAME); exit(1); } } } #else, continue print $client_socket "ACK\n"; # approve the user. close(USERNAME);
Re: Accessing a file from the perl
by AnomalousMonk (Archbishop) on Jan 12, 2012 at 14:58 UTC
    unless (open (USERNAME, "username")) { print "ERROR:Cannot open username file.\n"; exit(1); # ERROR }

    Just a parenthetic note: The code quoted above is more idiomatically (and, IMHO, better) written as below, which uses both a lexical filehandle and the three-argument form of open. (See also perlopentut.)

    my $filename = 'foo'; open my $filehandle, '<', $filename or die "opening '$filename': $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://947447]
Approved by Eliya
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-03-28 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found