Skip to content

Instantly share code, notes, and snippets.

@anazawa
Created February 13, 2012 14:14
Show Gist options
  • Select an option

  • Save anazawa/1817205 to your computer and use it in GitHub Desktop.

Select an option

Save anazawa/1817205 to your computer and use it in GitHub Desktop.
transform OO interface into functional one
# $value = get_header( $blosxom::header, 'foo' );
# $bool = exists_header( $blosxom::header, 'foo' );
# set_header( $blosxom::header, 'foo' => 'bar' );
# remove_header( $blosxom::header, 'foo' );
for my $method ( qw(get set remove exists) ) {
my $slot = __PACKAGE__ . "::${method}_header";
my $code = sub {
my ( $header_ref, @args ) = @_;
__PACKAGE__->new( $heaeder_ref )->$method( @args );
};
no strict 'refs';
*$slot = $code;
}
# 逆
for my $method ( qw(get set remove exists) ) {
my $slot = __PACKAGE__ . "::$method";
my $func_ref = \&{ "${slot}_header" };
my $sub_ref = sub {
my ( $self, @args ) = @_;
return $func_ref->( $heaeder_ref, @args );
};
no strict 'refs';
*$slot = $sub_ref;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment