Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

UID 0 and getpwnam

by c (Hermit)
on Apr 09, 2002 at 15:11 UTC ( [id://157745]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a cgi script that allows input from a user to add a username in a formfield. I was using my $uid = getpwnam("$username"); to grab the uid of the user submitted. This worked well enough, until I was testing to see what happened when "root" was placed in the user field. Since the uid for root is "0", the $uid value was coming back as false and appearing as though the user didnt exist. Is there a(nother| better) way to test for the existance of a user that will encompass 'root'?

humbly -c

Replies are listed 'Best First'.
Re: UID 0 and getpwnam
by mirod (Canon) on Apr 09, 2002 at 15:13 UTC

    Couldn't you use if( defined $uid)?

Re: UID 0 and getpwnam
by VSarkiss (Monsignor) on Apr 09, 2002 at 15:24 UTC

    You're confusing the concepts of "defined" versus "false", which are very different in Perl. The getpwnam document mentions

    If the entry doesn't exist you get the undefined value.
    Thus, you need a test as mirod has outlined above.

    The same consideration applies anytime a value that represents falsehood in Perl (0 or the empty string) can be a legitimate return result from a function. One of my favorite Perl books, Effective Perl Programming has an excellent section on the difference.

Re: UID 0 and getpwnam
by thelenm (Vicar) on Apr 09, 2002 at 15:21 UTC
    When I try your line of code on my machine using a non-existent username, I get a return value of '' (not undef). If you're testing for the existence of a user, then, you should be able to test length $uid. If the return string has any length, then you have a valid user.

    I would also recommend removing the double-quotes around $username, as this causes double-quotish processing to happen, which you may not want.

Re: UID 0 and getpwnam
by gregorovius (Friar) on Apr 09, 2002 at 15:46 UTC
    You should be able to test as follows
    if( $uid >= 0 ) { ... }
    and cover all the valid cases.
Re: UID 0 and getpwnam
by particle (Vicar) on Apr 09, 2002 at 17:09 UTC
    a nit: it's extra work putting double quotes around variables like "$username". basically, you're telling perl to build '"' . $username . '"'. in this function call, which is a waste of time. don't use double-quotes unless you need them.

    as an aside, in perl 6, you'll be able to do something like*

    module module_name; my class NoData is Exception { method warn(*@args) { die @args } } my $uid = getpwnam($username) // fail NoData;
    which will raise an exception of type NoData (a class localized to your module) if getpwnam($username) returns an undefined value. then leave it to the NoData class to handle the exception appropriately.

    *as per Exegesis 4

    ~Particle ;Þ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found