On Wed, Jul 7, 2010 at 02:27, Uri Guttman <u...@stemsystems.com> wrote: >>>>>> "SCT" == Srinivasa Chaitanya T <tschaitanya....@gmail.com> writes: > > SCT> How can I write functions which accept block as an argument? > SCT> Basically I want to create a function with like which just > SCT> executes the block and doesn't create an array. > > this is one of the few places where perl's prototypes are useful. read > perldoc perlsub and look for the section on prototypes. it shows example > prototypes that parse like perl's builtin functions. there is one for > grep which has the same prototype as map. snip
Neither map nor grep have a prototype: perl -E 'say prototype "CORE::map"' perl -E 'say prototype "CORE::grep"' This is because you cannot create a code block with a prototype. There is the & prototype, but it just makes it so you don't have to type the sub in front of the anonymous function you are passing to the function. That tiny bit of syntactic sugar is not worth all of the [headaches][1] prototypes bring with them. The fact that the & prototype still uses an anonymous function has all sorts of repercussions from the syntactical (you must put a comma after the "block") to subtle interactions (blocks don't create closures, but anonymous subroutines do). When it comes to prototype other than () or ($), just say no. [1]: http://www.perl.com/language/misc/fmproto.html -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/