Howdy Monks,

A Format object creates and holds an instance of its parent object, Format::Real. AUTOLOAD is then used to propagate method calls up to the parent. More of a wrapper than real inheritance I guess.

Seems clean to me, but if I do this, will I be sorry later? Should I be more conventional and use ISAs and SUPERs?

YuckOo

#!/usr/bin/perl use strict; my $as_float_e = Format->new('%8.4e'); my $as_float = Format->new('%8.4f'); my $as_integer = Format->new('%d'); my $as_string = Format->new('%20.20s'); my $var = 1234.5678; print $as_float_e ->format($var), "\n"; print $as_float ->format($var), "\n"; print $as_integer ->format($var), "\n"; print $as_string ->format($var), "\n"; #----------------------------------------------------------- package Format; #----------------------------------------------------------- sub new { my ($pack, $spec) = @_; my $r = {}; $r->{real} = Format::Real->new(); $r->{spec} = $spec; bless $r, $pack; } #----------------------------------------------------------- sub AUTOLOAD { my $r = shift; my $func = (split('::', $Format::AUTOLOAD))[-1]; if (Format::Real->can($func)) { $r->{real}->$func($r->{spec}, @_); } } #----------------------------------------------------------- package Format::Real; #----------------------------------------------------------- sub new { my ($pack) = @_; bless {}, $pack; } #----------------------------------------------------------- sub format { my $r = shift; my ($spec, $num) = @_; sprintf $spec, $num; }

In reply to What kind of OO is this? by YuckFoo

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.