Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
package Class::FlyweightWrapper; $VERSION = 0.01; use strict; use Carp; my $BASE_PACKAGE = <<'EOT'; # line 1 "'Flyweight wrapper PUBLIC for PRIVATE'" package PUBLIC; my %object = qw(PUBLIC PRIVATE); sub DESTROY { delete $object{$_[0]}; } sub AUTOLOAD { my $meth = $PUBLIC::AUTOLOAD; $meth =~ s/.*:://; my $self = $object{shift(@_)}; return $self->$meth(@_); } # Make sure things cleanup properly END { %object = (); } EOT my $BASIC_CONSTRUCTOR = <<'EOT'; sub CONSTRUCTOR { my $self = bless \ my $scalar, "PUBLIC"; my $class = shift; $object{$self} = $object{$class}->CONSTRUCTOR(@_); $self; } EOT sub import { shift; # Not interested in my package my $public = shift || croak("Usage: use Class::FlyweightWrapper 'Public::Package';"); my $private = caller(); my @constructors = @_ ? @_ : 'new'; my $template = $BASE_PACKAGE; $template =~ s/PUBLIC/$public/g; $template =~ s/PRIVATE/$private/g; foreach (@constructors) { my $piece = $BASIC_CONSTRUCTOR; $piece =~ s/CONSTRUCTOR/$_/g; $piece =~ s/PUBLIC/$public/g; $piece =~ s/PRIVATE/$private/g; $template .= $piece; } eval $template; if ($@) { confess("Template\n$template\ngave error $@"); } } 1; __END__ =head1 NAME Class::FlyweightWrapper - wrap a class with a flyweight wrapper. =head1 SYNOPSIS package Private::Package; use Class::FlyweightWrapper 'Public::Package' [, 'constructors', 'he +re']; # Implement Private::Package here 1; # Then in user code my $obj = Public::Package->new(@args); # $obj has all of the same methods as something of Private::Package # but is securely encapsulated. =head1 DESCRIPTION The flyweight pattern is a way of encapsulating an object in such a way that it is impossible to get access to its private data. The way you do it is have the public object be a reference to a scalar which is a key into a hash that contains the data. This module takes an existing class and creates another which looks the same, but which is securely encapsulated. As long as the class does not call caller, the proxying process should be completely transparent. As a bonus, the proxied objects are all destroyed before global destruction. If you are using a version of Perl before 5.8, this can give reliable destruction mechanics which otherwise would be hard to come by. When you use this module you need at to provide it with the name of the package you want to wrap your current one, and an optional list of all of the names of constructors people can use with your package. If you do not provide that list then it will assume that your constructor is called 'new'. =head1 AUTHOR AND COPYRIGHT Ben Tilly ( Copyright 2001. This package is free software. It can be modified and distributed on the same terms as Perl.

In reply to Class::FlyweightWrapper by tilly

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2023-03-29 12:53 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (71 votes). Check out past polls.