Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Unexpected difference in whitespace parsing in subroutine calls.

by broquaint (Abbot)
on Jul 10, 2005 at 10:24 UTC ( #473746=note: print w/ replies, xml ) Need Help??


in reply to Unexpected difference in whitespace parsing in subroutine calls.

If we deparse this to see how perl will it makes a certain kind of sense

## deparse output as deparsed by -MO=Deparse,-p print(sort(f(0))); # case 1 print((sort f 0)); # case 2 print(sort(&f(0))); # case 3 print((sort f 0)); # case 4 print((sort f 0)); # case 5 print(sort(&f(0))); # case 6 sub f { 1; } - syntax OK
So, cases 1, 3 and 6 all behave the same (i.e call f) and cases 2, 4 and 5 behave the same (i.e sort on 0, which, as a single element list, is already sorted, so f isn't called). And that's the end of that chapter.
HTH

_________
broquaint


Comment on Re: Unexpected difference in whitespace parsing in subroutine calls.
Download Code
Re^2: Unexpected difference in whitespace parsing in subroutine calls.
by Lexicon (Chaplain) on Jul 10, 2005 at 11:20 UTC

    It still doesn't make any sense. I see now though that f is actually getting called in both cases (or would be if there is more than one element). In the runs that return 0, it's called before the sort. In the runs that return 1, it's called as the sorting subroutine. The fact that whitespace indicates precendence really bugs me though. I feel like it should always return one or the other (probaby 1), and that you should be forced to call the subroutine with &.

    To get closer to my original code:

    #!/usr/bin/perl use strict; use warnings; my @list = (1, 2, 1); print join ',', sort (unique (@list)); print join ',', sort (unique(@list)); sub unique { my %new = (); return grep (!$new{$_}++, @_); }
    Returns:
    Chromium:~ lexicon$ perl -l little2.pl Unquoted string "unique" may clash with future reserved word at little +2.pl line 7. 1,2,1 1,2
    With a similar deparse:
    Chromium:~ lexicon$ perl -l -MO=Deparse little2.pl Unquoted string "unique" may clash with future reserved word at little +2.pl line 7. BEGIN { $/ = "\n"; $\ = "\n"; } use warnings; use strict 'refs'; my(@list) = (1, 2, 1); print join(',', (sort unique @list)); print join(',', sort(unique(@list))); sub unique { use warnings; use strict 'refs'; my %new; return grep((!$new{$_}++), @_); } little2.pl syntax OK

    I supose here, in the unique as sort-on-subroutine case, unique always returns a two element list which is probably interpreted as 1 and so leaves the list in order. This ends up looking like the unique subroutine is completely ignored, which drove me nuts for a couple hours.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-12-29 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls