
"no" not allowed in sort subroutine
[moderator please note, this post is being re-submitted after I discovered
our internal news server's moderated groups are broken. I apologize for the
duplicates, if any--clinton]
This is more of a "why" question than anything else. When I use the "no"
keyword inside of a sort BLOCK, perl responds with:
"no" not allowed in expression at /tmp/foo.pl
BEGIN not safe after errors--compilation aborted
For example in this largely nonsensical program:
use strict;
Why doesn't this work? Is the BLOCK for a sort a real block or no? If I
move the 'no strict refs' pragma outside of the sort block, everythings
peachy.
What I'm really using this for is to allow someone to pick sort methods on
the fly in a CGI program. A sample of the code follows (not all there):
#
# Sort subs
#
sub FREQ { $users{$a}->{count} <=> $users{$b}->{count} };
sub CDSID { $users{$a}->{cdsid} cmp $users{$b}->{cdsid} };
sub DATE { timelocal($users{$a}->{date}) <=> timelocal($users{$b}->{date})
Quote:
};
# And then later on...
foreach my $user (
sort {
no strict 'refs'; # This line doesn't work!
&{$cgi->param('FIRST')}
||
&{$cgi->param('SECOND')}
} keys %users
) {
# Do something with the data...
}
Of course there's other ways around this, I'm just suprised that the sort
BLOCK is being treated like a second-class BLOCK.
--
Clinton A. Pierce "If you rush a Miracle Man, you
http://www.*-*-*.com/ The Princess Bride