Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Perl parser gets confused with call to "sort" w/o parens

by vr (Friar)
on May 16, 2018 at 10:39 UTC ( #1214632=perlquestion: print w/replies, xml ) Need Help??
vr has asked for the wisdom of the Perl Monks concerning the following question:

IIRC the PBP advises to omit parens when unambiguously calling built-ins, because (and I agree) it removes extra-noise and improves readability. Thus, CL#1 is written as it was and it runs OK.

Then, looking at CL#2, I thought to omit a pair of parens -- see CL#3. I don't see anything becoming ambiguous, but Perl is confused -- see CL#4. And why the uniq imposes numeric context?

Also curious, if I'm explicitly imposing numeric context on sort (CL#5), Perl warns me 6 times, and not 5, as with CL#3.

>perl -lwe "sub x{@_} print sort x( qw( q w e r t y ))" eqrtwy >perl -MList::Util=uniq -lwe "print sort( uniq( qw( q w e r t y )))" eqrtwy >perl -MList::Util=uniq -lwe "print sort uniq( qw( q w e r t y ))" Argument "w" isn't numeric in sort at -e line 1. Argument "r" isn't numeric in sort at -e line 1. Argument "y" isn't numeric in sort at -e line 1. Argument "e" isn't numeric in sort at -e line 1. Argument "t" isn't numeric in sort at -e line 1. qwerty >perl -MO=Deparse -MList::Util=uniq -lwe "print sort uniq( qw( q w e r + t y ))" BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } use List::Util (split(/,/, 'uniq', 0)); print((sort uniq 'q', 'w', 'e', 'r', 't', 'y')); -e syntax OK >perl -lwe "print sort {$a <=> $b} qw( q w e r t y )" Argument "q" isn't numeric in sort at -e line 1. Argument "w" isn't numeric in sort at -e line 1. Argument "e" isn't numeric in sort at -e line 1. Argument "r" isn't numeric in sort at -e line 1. Argument "t" isn't numeric in sort at -e line 1. Argument "y" isn't numeric in sort at -e line 1. qwerty

Replies are listed 'Best First'.
Re: Perl parser gets confused with call to "sort" w/o parens
by choroba (Bishop) on May 16, 2018 at 11:09 UTC
    This is documented in sort:
    Warning: syntactical care is required...

    print sort uniq( qw( q w e r t y ));

    Is interpreted as

    print sort uniq qw( q w e r t y );

    i.e. it uses uniq as the sorting function.

    Update:

    In Perl 5.18.2., I'm not getting the numeric behaviour, it just fails with

    Sort subroutine didn't return single value at -e line 1.

    In blead perl, though, I'm getting the same warning as you.

    Update 2: The numeric context is forced even when I use uniqstr instead of uniq.

    That's probably because Perl first calls

    uniq 'q', 'w';
    which returns 'q', 'w', and sort tries to use this value as -1, 0, or 1, etc. This enforces the scalar numeric context probably.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      :( Oh, indeed, it's documented, thanks. Yet, CL#1 works OK, and the reason why Perl can be confused here and care is required is neither clear nor explained, in documentation. How CL#4 can be parsed to what's printed out, is unclear. OK, just a thought...

      Edit: OK, CL#1 was parsed specially because there was single letter identifier:

      >perl -lwe "sub x{@_} print sort x(qw(q w e r t y))" eqrtwy >perl -lwe "sub xx{@_} print sort xx(qw(q w e r t y))" qwerty
        The paragraph that choroba cites, documents four acceptable ways to sort an array returned by a function. I think it is reasonable to assume that anything else is ambiguous and should be considered invalid. In this view, all of your cases are invalid. The fact that some of them appear to do what you intend is irrelevant. It would be helpful if the document explained the reason for this restriction.
        Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1214632]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-05-21 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?