Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: An Object Structural Design Pattern in Perl

by hypochrismutreefuzz (Scribe)
on Dec 10, 2007 at 19:54 UTC ( [id://656208]=note: print w/replies, xml ) Need Help??


in reply to Re^3: An Object Structural Design Pattern in Perl
in thread An Object Structural Design Pattern in Perl

I feel embarrased to admit that I hadn't tested the code myself. After testing and debugging I decided to simplify the design and change the way the method calls are dispatched.
package Bridge::Simple; use strict; 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/.*:://; my $method = $self->{$AUTOLOAD}; return $self->{object}->$method(@_); } 1;
use Bridge::Simple; use XML::Simple; my $serialize_map = { serialize=>'XMLin', deserialize=>'XMLout', }; my $serializer = Bridge::Simple->new( XML::Simple->new( suppressempty=>q{}, noattr=>1 ), $serialize_map, );

This snippet of client code instantiates a Serializer object with a method_map that maps "serialize" with "XMLout" and "deserialize" with "XMLin" using the options given to override the default options of XML::Simple.

use Bridge::Simple; use YAML; my $serialize_map = { serialize=>'Dump', deserialize=>'Load', }; my $serializer = Bridge::Simple->new( YAML->new(), $serialize_map, );

This snippet of client code does the same thing using YAML as the serialization interface.

Client code for either serializer object would be identical.

$data = $serializer->serialize($data_hash); $data_hash = $serializer->deserialize($data);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://656208]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found