Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-04-26 12:38 GMT
Find Nodes?
    Voting Booth?