Not this way. You say use foo, then you call Foo->new - case matters.

It looks sort of like you're trying to do inheritance, which is usually done more like:

#!/usr/bin/perl -w package foo; sub new { my ($class, @stuff) = @_; my $self = {}; # do stuf with $self and @stuff... bless {}, $class; } sub do_something { my ($self, @parms) = @_; print "in do_something: $self, @parms\n"; } package bar; # if foo is in another file, also say "use foo;" here @ISA = qw/foo/; # "use base qw( foo );" can be used in place of the preceding 2 lines +in perl >= 5.004_04 (I think), certainly by 5.6.0. sub do_something_else { my ($self, @parms) = @_; print "in do_something_else, $self, @parms\n"; } package main; my $b = new bar; $b->do_something('stuff'); $b->do_something_else('stuff2'); __END__
Results in:
in do_something: bar=HASH(0x804ca30), stuff in do_something_else, bar=HASH(0x804ca30), stuff2
new and do_something, not defined in bar, still work because they are inherited from foo. do_something_else is defined in bar. so it works too.

All code given here is UNTESTED unless otherwise stated.

--Bob Niederman, http://bob-n.com

In reply to Re: Objects in Objects by bobn
in thread Objects in Objects by Angel

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.