I've finally wrapped my mind around objects. Sort of. My first real OO project is to develop a set of utilities for working on some of my company's proprietary tables. One of the things that my nascent module has is two packages - one is a bunch of low-level methods for interacting directly with the table data, and the other is a higher-level set of methods for doing more interesting things using (you guessed it) the low-level methods; sorting, deduping, that kind of stuff.

One of the things that I've been bitten by in my test programs is that my low-level package has the object constructor, not the high-level package. The 'bite' is that sometimes I forget, and instead of typing

my $tool = LowUtils->new ( file => 'foo' )

I type

my $tool = Utils->new ( file => 'foo' )

Today I realized that in the high-level package I can just add a new method which calls the low-level package's new method. Here's some sample code to show what I ended up doing:

#!/usr/bin/perl BEGIN { use strict; use warnings; } package Utils; sub new { # discard my class... shift(@_); # ...and invoke LowUtils::new as though # it were called by our caller. LowUtils::new ( 'LowUtils', @_ ); } sub get_numlines { my $self = shift; return scalar @{$self->{FILEDATA}}; } sub DESTROY { print "\tthis is the Utils destructor\n"; } 1; # end package Utils package LowUtils; @LowUtils::ISA = qw ( Utils ) ; sub new { my $proto= shift(@_); my %args= @_; my $class = ref($proto) || $proto; my $self = {}; $self->{FILENAME} = $args{file}; $self->{FILEDATA} = []; bless $self, $class; $self->init; return $self; } sub init { my $self= shift(@_); my %args= @_; open DAT, $self->{FILENAME} or return undef; @{$self->{FILEDATA}} = <DAT>; close DAT; return $self; } sub DESTROY { print "\tthis is the LowUtils destructor\n"; } 1; # end package LowUtils my $utils = Utils->new ( file => $0 ); print 'Utils: lines = ', $utils->get_numlines, "\n"; undef $utils; my $low = LowUtils->new ( file => $0 ); print 'LowUtils: lines = ', $low->get_numlines, "\n"; undef $low; exit;
Output:
Utils:    lines = 72
        this is the LowUtils destructor
LowUtils: lines = 72
        this is the LowUtils destructor

I arrived at this solution after deciding that it probably wouldn't be smart to set @Utils::ISA to refer back to LowUtils. My questions:

Thanks -

blyman
setenv EXINIT 'set noai ts=2'


In reply to Faking new() method in high-level package by belden

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.