the base class we'll decorate

package Print; # Print class to print a string use warnings; use strict; use Carp; sub new { my $type = shift; my $class = ref $type || $type; # TEXT is private! my $self = { TEXT => undef, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure,$class); return $closure; } # a public accessor to set and get TEXT sub text { &{ $_[0] }("TEXT", @_[1 .. $#_]) } # a public method to print TEXT sub print { my $self = shift; print $self->text . "\n"; } 1;

A HTML version

package PrintHtml; # Print class to print a string in html format use warnings; use strict; use Carp; use Print; sub new { my $type = shift; my $class = ref $type || $type; # itsPrint is private! my $itsPrint = shift; my $self = { itsPrint => $itsPrint, }; my $closure = sub { my $field = shift; return $self->{$field}; }; bless ($closure,$class); return $closure; } sub print { my $self = shift; print "<p>\n"; &{ $_[0]}("itsPrint")->print; print "</p>\n"; } 1;

A Latex version

package PrintLatex; # Print class to print a string in latex format use warnings; use strict; use Carp; use Print; sub new { my $type = shift; my $class = ref $type || $type; # itsPrint is private! my $itsPrint = shift; my $self = { itsPrint => $itsPrint, }; my $closure = sub { my $field = shift; return $self->{$field}; }; bless ($closure,$class); return $closure; } sub print { my $self = shift; print "\\begin{paraf}\n"; &{ $_[0]}("itsPrint")->print; print "\\end{paraf}\n"; } 1;

The caller...

#!/usr/bin/perl use warnings; use strict; use Print; use PrintHtml; use PrintLatex; print "Text...\n"; my $print = Print->new(); $print->text("hello"); $print->print; print "\nHTML...\n"; my $html = PrintHtml->new($print); $html->print; print "\nLatex...\n"; my $latex = PrintLatex->new($print); $latex->print;

In reply to The decorator pattern by gargle

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.