Yes -- i see now that i'm going about this whole thing the wrong way.  I'm
trying to make the module do too much.  It's a bad habit i'm trying to
break.

I suppose if the luser who uses my package doesn't like what 'bark' does
s/he will just have to overload it themselves.  ;)

Interestingly enough i took a class last year where we each wrote a massive
program in C++.  I kept asking the professor how to tell what one object
should do and what it should leave for another one to do.  He never really
gave me a straight answer, but he would always get on my case for trying to
make my modules god-like.

I guess it's just an art--one i have yet to master.  ;)

-----Original Message-----
From: Jos I. Boumans [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 30, 2001 10:14 AM
To: Mooney Christophe-CMOONEY1; [EMAIL PROTECTED]
Subject: Re: ref problems


Hi,

I'm not sure you're quite understanding the logic of modules and packages
yet...
the idea of a module is that you use methods (subroutines belonging to a
package) to execute code
however, you're making your 'methods' attributes to an object.
This quite defeats the purpose and provides nothing in the form of
encapsulation.
(concider some ev0l subroutine you pass your object to says:
    sub foo {
        my $self  = shift;
        $self->{'speak'} = undef;
    }

That would cause serious dying of your script when calling the 'method'

Maybe you should raed up on modules a bit first and OO programming in
general

I wrote a tutorial on http://japh.nu that you might find very interesting,
since it deals with jsut these things
also, the following perldocs are very usefull
perlboot
perltoot
perltootc

hth,

Jos Boumans



> Hello, all -- i have come across an interesting problem.  When i run the
> following script, i get that '$this' in dog::bark is undefined.  When i
> think about it, this makes sense, since call $a_dog->{'speak'}() is like
> saying "dog::bark".
>
> Saying
> $a_dog->{'speak'}($a_dog)
> on the last line would solve this problem, but it would be nice not to
have
> to worry about passing itself to the function.  Does anyone see any
> alternatives to this?
>
> Thanks!
>
> #!/local/perl/bin/perl -w
> use strict;
>
> package dog;
> sub new
> {
> my $class=shift;
> my %this=@_;
> $this{'speak'}=\&bark;
> bless \%this;
> }
>
> sub bark
> {
> my $this=shift;
> $this || die "foo: it's undefined!\n";
> print "$this->{'name'} says woof!\n";
> }
>
> package main;
>
> my $a_dog=dog->new(name => 'bart');
> $a_dog->{'speak'}();
> ************************
>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to