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 (ben_tilly@operamail.com). 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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.