It looks like you've implemented everything as a class method.

Here's a jot of example code.

# ClassName->method( @args ); # class method # $object_ref->method( @args ); # instance method # calling your constructor. my $o = MyOptions->new( Usage => 'Do what I say, not what I do!', Options => [ { Name => 'Foo', Type => 'Bar', Etc => 'Blah' }, { Name => 'Bar', Type => 'Baz', Etc => 'Blah' }, ], ); $o->print_usage; MyOptions->debug(1); # having problems here. Enabling debug mode. my $chickens = $o->count_your_chickens($eggs); MyOptions->debug(0); my $parsed_ok = $o->parse_args( @ARGV ); package MyOptions; use const OPTIONS => 0, my $DEBUG; sub debug { my $old = $DEBUG; if ( @_ ) { my $old = $DEBUG; $DEBUG = shift; $Carp::Verbose = $DEBUG; } return $old; } sub new { my $class = shift; my %args = @_; my $self = []; bless $self, $class; $self->set_usage( $args{Usage} ); $self->add_options( @{ $args{Options} || [] } ); return $self; } sub set_usage { my $self = shift; check_args(@_,1); my $old_usage = $self->[USAGE]; $self->[USAGE] = shift; return $old_usage; } sub add_options { my $self = shift; foreach my $opt (@_) { my $opt_obj = $self->OptionFactory( $opt ); next unless $opt_obj; my $name = $opt_obj->get_name; $self->[OPTIONS]{ $name } = $opt_obj; } return keys %{$self->[OPTIONS]||{}}; } sub OptionFactory { my $self = shift; my $opt = shift; # make an option object from the specification passed in. # unless $opt is already an option object. Then return. # if opt is inavalid spec - return undef my $obj = UNIVERSAL::isa( 'MyOption', $opt ) ? $opt : MyOption->new(); return $obj; }

I can't recommend thedamian's Object Oriented Perl enough. Very good for the OOP newbie. It covers concepts of OO and interesting Perl techniques, mostly along the lines of classical Perl OO.

If you want to do really fancy OO stuff check out Moose. It's all the rage these days. I recommend learning to do the classical style first.


TGI says moo


In reply to Re: A first attempt.. at OO perl by TGI
in thread A first attempt.. at OO perl by why_bird

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.