in reply to Syntax question about closures and Perl 'sort'
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 $_
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Syntax question about closures and Perl 'sort'
by dwm042 (Priest) on Apr 03, 2009 at 22:40 UTC | |
by shmem (Chancellor) on Apr 03, 2009 at 23:55 UTC | |
by jethro (Monsignor) on Apr 03, 2009 at 23:38 UTC | |
by tilly (Archbishop) on Apr 04, 2009 at 04:41 UTC | |
by jethro (Monsignor) on Apr 05, 2009 at 18:43 UTC | |
by Marshall (Canon) on Apr 05, 2009 at 05:28 UTC |