Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!sun-barr!newstop!texsun!convex!convex.com!tchrist From: tchrist@convex.com (Tom Christiansen) Newsgroups: comp.lang.perl Subject: inlining your sort subroutine Message-ID: <109155@convex.convex.com> Date: 22 Nov 90 16:11:14 GMT Sender: news@convex.com Reply-To: tchrist@convex.com (Tom Christiansen) Organization: Convex Computer Corp, Richardson, TX Lines: 50 Instead of having to doing this: sub numerically { $a <=> $b; } @out = sort numerically @in; wouldn't this be nice: @out = sort { $a <=> $b; } @in; You can *almost* do it right now. $func = 'func0001'; sub makefun { eval "sub $func { $_[0]; }"; $func++; } $f1 = &makefun('print "hello $a\n"'); $f2 = &makefun('$a++'); for (1..3) { do $f1(); do $f2(); } That works just fine. So then I did this: @a = (3, 5, 12, 3, 9, 23, 93, 1, 445, 33, 12); $sortsub = &makefun('$a <=> $b'); @a = sort $sortsub @a; print join(", ", @a), "\n"; And again it worked just fine. However, I could NOT do this: @a = sort &makefun('$a <=> $b') @a; Which is too bad. Of course, I would still like things like @a = sort { $a <=> $b; } @a; @a = sort { $x{$a} cmp $x{$b}; } keys %x; to work. You run the risk -- if you want to call it that -- that people would start writing stuff like this: @a = sort { # lots of code # might intervene here, taking # a long time to find the list } @b; But I still like the idea. I don't see how it could break any scripts. --tom