#!perl -w use strict; # # put any object inside a wrapper # access the object directly # and store variables privately in the wrapper # # my $obj = InsideOutWrapper->new( # $module, $wrapper_args, @module_args # ); # my $cgi = InsideOutWrapper->new('CGI'); my $lwp = InsideOutWrapper->new('LWP::UserAgent'); $cgi->param( 'foo'=> 5 ); # store in the CGI object $lwp->agent( 6 ); # store in the LWP::UA object $cgi->iow('bar'=>7 ); # store in the CGI Wrapper $lwp->iow('baz'=>8 ); # store in the LWP::UA Wrapper print "ok!\n" if '5678' eq join '' # retrieve the values , $cgi->param('foo') , $lwp->agent , $cgi->iow('bar') , $lwp->iow('baz') ; # check lists of all the private keys # print "ok!\n" if 'bar' eq join( '', $cgi->iow ) and 'baz' eq join( '', $lwp->iow ); exit; package InsideOutWrapper; use warnings; use strict; my %built; sub new { my($wrapper_class,$other_class,$wrapper_args,@other_args)=@_; my $class = $wrapper_class . '::' . $other_class; if (!$built{$class}++) { my $class_txt = get_class_txt(); $class_txt =~ s/__WRAPPER__/$class/g; $class_txt =~ s/__MOD__/$other_class/g; eval $class_txt; die $@ if $@; } return $class->new($wrapper_args,@other_args); } sub get_class_txt { return <<''; package __WRAPPER__; use strict; use warnings; use vars qw( $vars ); use base '__MOD__'; sub new { my($class,$wrapper_args,@other_args)=@_; my $obj = bless __MOD__->new(@other_args), $class; $vars->{$obj} = $wrapper_args; $obj; } sub iow { my($self,$key,$val)=@_; return keys %{ $vars->{$self} } unless defined $key; return $vars->{$self}->{$key} unless (defined $val); $vars->{$self}->{$key} = $val; } sub DESTROY { my $self = shift; delete $vars->{$self}; } } 1; __END__