in reply to Syntax question about closures and Perl 'sort'
You cannot sort by a bare anonymous sub. See sort:
sort SUBNAME LIST
sort BLOCK LIST
sort LIST
sort wants a BLOCK or a named sub.
But you can do either of
for my $rec ( sort { $sortby->() } @{$dns_records{$_}} ) {
and
local *sortby = setsort($_); for my $rec ( sort sortby @{$dns_records{$_}} ) {
As for the error message:
Array found where operator expected at ./sample.pl line 5, at end of l +ine (Missing operator before ?)
- this means that sort treats the anonsub as pertinent to the items to be sorted, and perl complains about the missing comma between the sub and the following array.
|
|---|