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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |