Probably the parser parses sort sortby->() as "sort LIST" instead of "sort BLOCK LIST" or "sort SUBNAME LIST", see "perldoc -f sort". After that the parser expects either a ';' or a control statement like 'if', 'while', 'for'... but instead sees an array.

I get the expected error messages in this sample code:

#!/usr/bin/perl use strict; use warnings; my $sortby = setsort(1); for my $rec ( sort $sortby->() (2,6,1) ) { print $rec, "\n"; } sub setsort { my $type = shift; return sub { $a <=> $b } if ( uc($type) eq "PTR" ); return sub { $a cmp $b }; } Use of uninitialized value in string comparison (cmp) at ./t7.pl line +14. Use of uninitialized value in string comparison (cmp) at ./t7.pl line +14. Can't use string ("0") as a subroutine ref while "strict refs" in use +at ./t7.pl line 6.

By the way, using a closure in your code would make no sense. When you call setsort() it doesn't need to remember any values from previous calls to it, its return value depends solely on the parameter $_


In reply to Re: Syntax question about closures and Perl 'sort' by jethro
in thread Syntax question about closures and Perl 'sort' by dwm042

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.