in reply to make subroutine that takes expression as implicit block as first arg
You can accomplish your task using Prototypes. In fact, from the linked documentation,
here's a reimplementation of the Perl grep operator:sub mygrep (&@) { my $code = shift; my @result; foreach $_ (@_) { push(@result, $_) if &$code; } @result; }
Update: Your example would be called as: print join ",", mygrep {$_ % 2 == 0} 0..10; Note the deviation from the proposed use case. I'd initially missed the spec for "automatically put [ting] it in an implicit block"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: make subroutine that takes expression as implicit block as first arg
by oko1 (Deacon) on Aug 13, 2010 at 19:54 UTC | |
by kennethk (Abbot) on Aug 13, 2010 at 20:01 UTC |
In Section
Seekers of Perl Wisdom