On Sun, Apr 13, 2008 at 2:38 PM, Nelson Castillo <[EMAIL PROTECTED]> wrote: > Hi :-) > > I wrote this binary search function. I wrote it so that I could pass > a comparison function as the last parameter. But I have to write > "sub" and I noticed that the built in sort function doesn't need it. > So I have to write: > > sub { shift <=> shift} > instead of: > {$a <=> b}. > > This might be a silly question, but I'd like to know if I can modify the > binary search function to receive a function similar to the one that > "sort" receives. snip
Short answer, no, code blocks are not accessible from normal Perl. You must use an anonymous function. Medium answer, yes, but you must write your library code in XS. Normal Perl can't access code blocks, but I believe you can do it in XS, look at perlguts and perlapi for more info. Long answer, you can use $a and $b just like the sort function does: my_sort(sub { $a <=> $b }, @list); sub my_sort { my $compare = shift; local ($a, $b); #set $a and $b from @_ some how my $r = $compare->(); if ($r < 0) { } elsif ($r == 0) { } else { } } -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/