Although the code below is quite similar to what BrowserUk has posted, I post here two versions of the "my_map" function that I wrote a couple of weeks ago for a tutorial that I am in the course of writing in French on functional programming in Perl. The first one works similarly to Perl's map function in the sense that if the code block modifies $_, then the original array is modified:
sub my_map (&@){
my $code_ref = shift;
my @d ;
push @d, $code_ref->($_) for @_;
return @d;
}
The second one is more like a pure functional version of map having no side-effect on the original array:
sub my_map (&@){
my $code_ref = shift;
my @d = @_;
$_ = $code_ref->($_) for @d;
return @d;
}