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

Hello monks,

I am testing some commands on the command line before I incorporate them into my script. So I am trying to get userid and groupid using the getpwnam function which returns a list. Please see below

perl -e 'print (getpwnam(traveler))[2,3];' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. perl -e '@temp=(getpwnam(traveler)); print @temp[2,3]' 500500

So does perl allow printing the elements of a list with out using the temporary array to hold the list ? Is there a way to get the first print working ? Or am I doing something wrong ? Appreciate any inputs!

Regards!
Jr. Monk

Replies are listed 'Best First'.
Re: List vs array printing in perl
by NetWallah (Canon) on Mar 30, 2016 at 16:17 UTC
    perldoc -f print shows:

    ... be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all the arguments (or interpose a "+", but that doesn't look as good).

    So - the "print" is consuming the first open paren, leaving the square brackets hanging.

            This is not an optical illusion, it just looks like one.

Re: List vs array printing in perl
by Corion (Patriarch) on Mar 30, 2016 at 16:17 UTC

    If you allow Perl to tell you more by enabling warnings, the following might help you:

    > perl -wle 'print (getpwnam(traveler))[2,3];' print (...) interpreted as function at -e line 1. Unquoted string "traveler" may clash with future reserved word at -e l +ine 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

    The print uses your set of parentheses as its function call parameters. Thus, the additional square brackets make no sense to Perl. Add another set of parentheses to tell Perl what you mean:

    > perl -e 'print( (getpwnam(traveler))[2,3] );' Unquoted string "traveler" may clash with future reserved word at -e l +ine 1. The getpwnam function is unimplemented at -e line 1.
Re: List vs array printing in perl
by BrowserUk (Patriarch) on Mar 30, 2016 at 16:19 UTC

    Use:

    perl -e 'print +(getpwnam(traveler))[2,3];'

    To understand why your attempt doesn't work add -w:

    C:\test> perl -we "print (getpwnam(traveler))[2,3];" print (...) interpreted as function at -e line 1. Unquoted string "traveler" may clash with future reserved word at -e l +ine 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: List vs array printing in perl
by raghuprasad241 (Beadle) on Mar 30, 2016 at 16:30 UTC

    Thank you for everyone who responded. I should learn to use warnings for sure. Thanks again for enlightening me. Its clear to me now.

    Cheers!
        my script template begins with
        use 5.011; # implies strict + feature 'say' use warnings;
        but that doesn't help with a one-liner. There he could use
        perl -wMstrict -e ...
        (that goes without saying, though)