Muttley Meen wrote:
>
> Hi!
> Sorry if I double post, but the maillserver seems to have something against
> perl scripts attachements, so my first email was droped by the MTA : /
> Maybe if I put the code inline will be ok
>
> In the attached script I implement two packages:
> Package1 and Package2.
>
> Package2 is derived from Package1, which I guess I dont well.
> Now Package1 has a method called IncErr which increments a variable
> named $err.
>
> If I call something like:
>
> $a = Package1::new();
> $a->IncErr();
> print "ERR1: $a->{err}\n"; # should print 1
>
> all goes well, but if I call something like:
>
> $a = Package1::new();
> $a->Package2->Create(); # this should call IncErr too
> print "ERR1: $a->{err}\n"; # should print 1
> doent't work as I expected.
>
> Is there something wrong with the way I `bless`-ed the class Package2 ?
Your thinking is unclear in a number of areas, and you've made the classic
mistake of writing far too much before you start testing. It's also clearer and
easier for you to put your class definitions into modules and 'use' them into
the main program; it will compartmentalize your program and protect you from
making some simple mistakes. It is also the usual way in which classes are
implemented in Perl.
I've had trouble understanding your thinking in some areas, but here are some
comments which I hope will help you on your way.
> ------>8-----------
> #! /usr/bin/perl
Always, always, always
use strict;
use warnings;
especially if you're on unfamiliar territory and especially if you have bugs
that you can't fix. It's free, computerized debugging!
> package Package1 ;
> sub new {
> my ($class) = @_;
> $class = __PACKAGE__ ;
You've clearly found that the package name isn't in @_ as you expected so you've
put it in explicitly. Bad idea. If something isn't as you expect then find out
why and fix it. It may be that your expectations are wrong, but just as often,
as in this case, something is at fault somewhere else. You are calling the
package method new() as
$a = Package1::new();
which isn't a method call at all it's just a subroutine call, so Perl won't do
its magic of adding the package name as an implicit parameter. Use
$a = Package1->new;
and you won't have to 'fix' what Perl passes to you.
> print "Call new [".$class."]\n" ;
> $r = [];
What's $r, and what's wrong with using @r? I'm afraid I don't understand what
you're trying to do here, and there's some nasty stuff going on later on
involving $r too. Don't forget you're writing a class here, not main code.
> $this = {};
> $class->{err} = 0 ;
Perl has allowed you to do this as you haven't used 'strict', but it's a very
bad idea and clearly doesn't do what you think it does. $class holds the name of
the current package, 'Package1', and you're now using it as a symbolic reference
to a hash, so you're manipulating a package hash element, and this line is
equivalent to
$Package1::Package1{err} = 0;
What you mean is
$this->{err} = 0;
and 'use warnings' and 'use strict' would have helped you find this on your own.
> $r->[0] = Package2::new($this->{sock_fd} );
Hmm. Package2 depends on Package1 and also vice-versa. We could have a lot of
fun here! Remember that you're manipulating package variable $Package1::r, which
almost certainly isn't what you want.
> bless $this, $class;
> return $this ;
> }
Fine. Your blessing is correct, despite your misgivings!
> sub IncErr {
> my $this = shift ;
> $this->{err} += 1 ;
> }
Also correct.
> sub Package2 {
> my $this = shift ;
> @_ ? ($r->[0] = shift) : $r->[0];
> }
Spooky! Package1 not only builds an array of Package2 objects but also has a
method called Package2! I want to stop you doing this, but doesn't the fact that
you're assigning to $this and never using the value give you a clue? An object
method should normally manipulate only object data, so you need an element of
%$this to provide your $r thing. But, like I said, don't do that anyway!
> package Package2 ;
>
> @ISA = ("Package1");
Correct again.
> sub new {
> my ( $this, $sock ) = @_ ;
my ($class, $sock) = @_;
> $class = __PACKAGE__ unless @_;
Same thing as before. Two wrongs don't make a right, especially here where you'r
expecting an explicit parameter to the method ($sock). Defaulting the class name
won't happen here unless no parameters at all are passed in.
> $this->{sock_fd} = $sock ;
> bless $this;
Presumably you left the package name off the bless because you found it wasn't
being set up properly? bless() will bless the reference into the current package
unless you say otherwise, which in this case is what you want.
> return $this ;
> }
>
> sub Create {
> my $this = shift;
> print "Call Create\n" ;
> print "ERR2: $this->{err} ( this should print 2 )\n" ;
> $this->IncErr() ;
> print "ERR2: $this->{err} ( this should print 3 )\n" ;
> }
You should write an accessor method in Package1 to retrieve the value of the
error field, rather than using the hash element directly:
sub err {
my $this = shift;
$this->{err};
}
and then
my $err = $this->err;
print "ERR2: $err ( this should print 2 )\n";
or the ugly
print "ERR2: @{[$this->err]} ( this should print 2 )\n";
whcih avoids the temporary variable.
You also need
package main;
here, but better still put the class definitions in separate Perl module files
as I said before.
> my $a = Package1::new();
Why use 'my' when you have no strict pragma?
> $a->IncErr();
> $a->IncErr();
> $a->Package2->Create();
Perhaps the light is dawning. I think you ment to subclass Package2 in Package1
instead of what you've done. But Package1 isn't essentially a subclass of
Package2 any more than a zoo is a type of animal - it's just a class that
contains objects of another class.
> print "ERR1: $a->{err} ( this should print 3 )\n" ;
>
> ------------------->8--------------------
The reason this doesn't work as you expect is that you have three separate
error counts:
- In Package1::new you are initialising $Package1::Package1{err} to 0
- In the main code you are calling $a->IncErr (the only real object method
call around here) which first autovivifies $a->{err} (since the constructor
didn't create it) and then adds one to it. There are two calls so the end
result is two, which is printed in your last line of code as
ERR1: 2 ( this should print 3 )
- In package2::Create you are working on the object returned by $a->Package2,
which is $Package1::r->[0]. So you are printing $Package1::r->[0]->{err}
(which is undefined at first since, again, the constructor didn't create it)
and calling $Package1::r->[0]->IncErr() which creates it and gives it a value
of one. These values are printed from the Package2::Create method as
ERR2: ( this should print 2 )
ERR2: 1 ( this should print 3 )
I'm not sure where to send you from here, as you've dug yourself such a deep
hole! Perhaps start by using 'strict' and 'warnings' as I said and get your
program to clear those hurdles before you try to get your classes working
properly. Perhaps others can suggest a direction?
I hope this helps, and good luck!
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>