Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2016-10-20 21:49 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (283 votes). Check out past polls.