Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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";
Output:
g1: g2: rootx00root/root/bin/bash

Comment on getpwnam(@_) ???
Select or Download Code
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 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 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-08-01 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (50 votes), past polls