in reply to User Existance?

Anonymous Monk,
There are a few things in your post that make providing a solution difficult.

  • If you are worried about speed, it usually comes at the cost of space (in this case memory).
  • If you are worried about memory, it usually comes at a cost of speed.
  • You haven't indicated how long your script is going to be running. Sometimes it is worth it to spend time at the beginning of the script if subsequent lookups will outweigh that time.
  • You haven't indicated how often your script is going to be checking for users. It may be a moot point (premature optimization) to even be worried.
  • You haven't indicated if you only want to know if the user exists, or if you also want some other information such as their home directory (which you mentioned as a validation means).

    You also indicate that you would like to add users that are not on your system. It is for this reason that I suggest using a hash lookup (which is very fast) in this untested code.

    #!/usr/bin/perl -w use strict; my %User; { local @ARGV; @ARGV = qw(/etc/passwd /usr/defined/file); while (<>) { chomp; my @field = split /:/; unless (exists $User{$field[0]}) { $User{$field[0]} = \@field; } else { print "WARNING: Duplicate found for $field[0]\n"; } } } my $testuser = "blah"; if (exists $User{$testuser}) { print "$testuser exists on system\n"; print "Field 4 for $testuser is $User{$testuser}->[3]\n"; }
    Of course /user/defined/file should be set up like /etc/passwd if you care about the extra fields. Additionally, if you don't want to use /etc/passwd - I have shown the framework - you can have an "ok" %User and a "blacklist" %BadUser.

    Cheers - L~R

  • Replies are listed 'Best First'.
    Re: Re: User Existance?
    by Anonymous Monk on May 17, 2003 at 17:49 UTC
      Hi there, Thanks for that. This script is going to be run every time a new user registers (which is about 2 minutes) and the user will be waiting on the script, so however long it takes, is however long a user will be sitting at a blank webpage...
        Anonymous Monk,
        This might be a great deal more complicated than you think. It might also be a lot more simple.

        If the script is going to be called from scratch each time - then it seems moot to try and speed up a single lookup. You do have other problems to consider though.

        If the script will always be running, then it makes sense to have that information in memory if you can afford that memory. Then you run into the problem if multiple copies can be running at the same time. How do you share the memory? How do you avoid race conditions on files on the system.

        You very well may want to consider a database instead. I can't offer any suggestions for how to do this as I don't do web/database programming. Consider for instance updating your hash and your external file. What happens if the system crashes after the user has been added, but before you have had a chance to update your text file.

        There are a lot of sagely monks here that have probably done exactly what you are trying to do or something very similar. Take the time to explain the entire process and I am sure you will get the advice you need - if not working code.

        Cheers and goodluck - L~R

    Re: Re: User Existance?
    by Anonymous Monk on May 17, 2003 at 20:41 UTC
      why am i getting an error when trying to run this?
      if getpwnam($username) { $message = $message.'<p>The Chosen Username Already Exists.</p>'; $found_err = 1; }
      Also, if i run mad-hatters code and change the username to root, it does not display that the username is in use!
        What's the error? As for not complaining about root, perldoc -f getpwnam mentions that the return value is the uid corresponding to the name, or undef if the username does not exist. Root is uid 0, so you really need to check the definedness of the return value:
        if(defined(getpwnam($username))) ...
        Post the error message and we'll figure it out.

        --isotope
          I forgot about that little caveat. Thanks for pointing it out. As for the error, it is most likely the lack of parentheses around the conditional for the if statement, as I said before.