Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

getpwnam(@_) ???

by perl5ever (Pilgrim)
on Mar 31, 2011 at 18:18 UTC ( #896689=perlquestion: print w/replies, xml ) Need Help??
perl5ever has asked for the wisdom of the Perl Monks concerning the following question:

I'm wondering why g1 and g2 don't give the same results:
sub g1 { getpwnam(@_) } sub g2 { getpwnam($_[0]) } print "g1: ", g1("root"), "\n"; print "g2: ", g2("root"), "\n";
g1: g2: rootx00root/root/bin/bash

Replies are listed 'Best First'.
Re: getpwnam(@_) ???
by Fletch (Chancellor) on Mar 31, 2011 at 18:21 UTC

    getpwnam imposes scalar context on its argument.

    $ perl -le 'print prototype q{CORE::getpwnam}' $

    That means that @_ is treated as the number of elements g1 was called with and I doubt you have a user named "1" on your system.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: getpwnam(@_) ???
by ikegami (Pope) on Mar 31, 2011 at 18:19 UTC

    It has a prototype that causes the argument to be evaluated in scalar context.

    $ perl -E'say prototype("CORE::getpwnam")' $
Re: getpwnam(@_) ???
by wind (Priest) on Mar 31, 2011 at 18:21 UTC

    getpwnam assumes a scalar, so you're array is being turned into the array count.

    sub g1 { getpwnam(@_) } # equivalent to. sub g1 { getpwnam(scalar @_) }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://896689]
Approved by ikegami
[Corion]: ambrus: That looks deceptively simple, but maybe that's simply all that's needed. Which would be great! I hope I get to test it tomorrow, thanks!!
[choroba]: Hm, unrelated question.
[ambrus]: I added a timer implementation to ambrus's scratchpad as well, again the whole thing is completely untested and probably has more bugs than lines
[choroba]: Anyone using anything to validate json contents of a REST service?
[moritz]: choroba in python I use jsonschema
[moritz]: I'm sure there's a Perl implementation of it too
[choroba]: it seems Kelp and similar help you a lot with dispatching, but how to do something similar with contents?
[moritz]: like JSON::Schema
[Corion]: ambrus++ # more bugs than lines
[Corion]: That's like "works on my machine" ;-)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2016-12-08 12:46 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (141 votes). Check out past polls.