package Bridge::Simple; use strict; use Carp; sub new { my $class = shift; my $object_ref = shift; my $method_map = shift; my $self = {}; $self->{$_} = $method_map->{$_} for keys %$method_map; $self->{object} = $object_ref; return bless($self, $class); } sub AUTOLOAD { my $self = shift; use vars '$AUTOLOAD'; $AUTOLOAD =~ s/.*:://; croak "no method $AUTOLOAD" unless exists $self->{$AUTOLOAD}; my $method = $self->{$AUTOLOAD}; return $self->{object}->$method(@_); } 1; #### ---------- | Session | | object | ---------- |obj ref: | | $store | ----------------- | \___|____\| Bridge::Simple | | Database| | | /| | | objects:| interface: | $map $obj --------------->| DB_File,| fetch() ---:------------ | SQLite | fetch_ids() : store() : delete() : : ----------- (maps the interface of the using object to the implementation interface, eg, the DB_File implementation) { fetch=>'get_value', fetch_ids=>'get_keys', store=>'set_value', delete=>'delete', } #### my $storage = Bridge::Simple->new( Database::DB_File->new( type=>'db_btree', dbpath=>$path_info{sessionpath}, dbname=>'sessiontree.db', ), $storage_map, ); #### my $xml_map = { serialize=>'xml_out', deserialize=>'xml_in', }; my $yaml_map = { serialize=>'Dump', deserialize=>'Load', }; my $xml_serializer = Bridge::Simple->new( XML::Simple->new(), $xml_map, ); my $yaml_serializer = Bridge::Simple->new( YAML->new(), $yaml_map, );