my @test_data = ( ['none', 'The quick brown '], ['bold', 'fox'], ['none', ' jumped '], ['ital', 'over'], ['none', ' the lazy '], ['bold', 'dog'], ['none', '.'] ); #### my %handlers = ( 'none' => sub {shift}, 'bold' => \&wrap_with_b_tags, 'ital' => \&wrap_with_i_tags, 'para' => \&wrap_with_p_tags, ); sub build_para { my ($data) = @_; my $text = ''; for (@$data) { $text .= $handlers{$_->[0]}->($_->[1]); } return $handlers{'para'}->($text); } #### sub wrap_with_html { my ($tag, $text) = @_; return "<$tag>$text"; } my %handlers = ( 'none' => sub {shift}, 'bold' => \&wrap_with_html('b', # partial funcall 'ital' => \&wrap_with_html('i', 'para' => \&wrap_with_html('p', ); #### sub curry { my ($func, @args) = @_; return sub { my (@rest) = @_; &$func(@args, @rest); } } # Currying: sub foo { my ($x, $y) = @_; print "$x + $y = ", $x + $y, "\n"; } my $plus_five = &curry(\&foo, 5); &$plus_five(3); #### sub curry { my ($func, @args) = @_; return sub { my (@rest) = @_; &$func(@args, @rest); } } my %handlers = ( 'none' => sub {shift}, 'bold' => &curry(\&wrap_with_html, 'b'), 'ital' => &curry(\&wrap_with_html, 'i'), 'para' => &curry(\&wrap_with_html, 'p'), ); sub wrap_with_html { my ($tag, $text) = @_; return "<$tag>$text"; } sub build_para { my ($data) = @_; my $text = ''; for (@$data) { $text .= $handlers{$_->[0]}->($_->[1]); } return $handlers{'para'}->($text); } my @test_data = ( ['none', 'The quick brown '], ['bold', 'fox'], ['none', ' jumped '], ['ital', 'over'], ['none', ' the lazy '], ['bold', 'dog'], ['none', '.'] ); print &build_para(\@test_data);