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

Hello, I was wondering if there's a neater way to achive the following:
my ($uid,$pgid) = (getpwnam("nobody"))[2..3];
I vaguely remember some sort of syntax along the lines of,
my (,,$uid,$pid) = getpwnam("nobody");
but can't get it to work. Regards, Chris

Replies are listed 'Best First'.
Re: What's the neatest way to extract two items from an array returned by a function?
by FunkyMonk (Bishop) on Aug 24, 2007 at 15:29 UTC
    Use undef:

    my (undef, undef, $uid,$pid) = getpwnam("nobody");

    but I prefer the slice, anyway:)

      Thanks, now that you mention it, the slice probably is neater anyway.
Re: What's the neatest way to extract two items from an array returned by a function?
by andreas1234567 (Vicar) on Aug 25, 2007 at 06:43 UTC
    The functions like getpwnam are context sensitive, which means they will return differently in list and scalar context:
    $ cat 634897.pl use strict; use warnings; my $name = q{nobody}; print scalar ( getpwnam($name) ); print scalar ( getgrnam($name) ); print join (q{,}, getpwnam($name) ); __END__ $ perl -l 634897.pl 99 99 nobody,x,99,99,,,Nobody,/,/sbin/nologin
    --
    Andreas