Gossamer Forum
Home : General : Perl Programming :

need urgent help on the ff. school assignment:

Quote Reply
need urgent help on the ff. school assignment:
hi.

we currently have a school assignment on PERL that goes something like the following:

package A;
use base qw(C);

sub increment{
my $self = shift;
return $self->{count}++;
}

package B;
use base qw(C);

sub increment{
my $self = shift;
return $self->{count}++;
}

packages A and B are used in the main program which looks like this:

#!/usr/bin/perl -w
use strict;
use A;
use B;

my $A1 = A->new();
print $A1->increment(); # output is '0'

my $A2 = A->new();
print $A2->increment(); # output is '1'

my $B1 = B->new();
print $B1->increment(); # output is '0'

my $B2 = B->new();
print $B2->increment(); # output is '1'

our task is to implement package C. in my implementation, i made count to be a class variable, like:

package C;

my $this;
$this->{count} = 0;

sub new{
$C = shift;
bless $this, $C;
return $this;
}
1;

however, my problem now is that all objects (A1, A2, B1, B2) refer to the same count variable. thus, instead of outputting 0101, i get 0123. i guess this is because packages A and B both use C, where the count is a class variable.

we cannot change packages A or B, or even the main .pl file. we can only create package C. my implementation works only in that it outputs 0123, not 0101.

would anyone happen to know how to solve this?
Quote Reply
Re: [deryk] need urgent help on the ff. school assignment: In reply to
Hi,

I'm a bit confused :/

Did you provide the routines in A/B.pm for making the new() element?

With your exact codes (plus the 1; at the end of the .pm files), I get this error;


linkssql@schopen test $ perl test.pl
Can't locate object method "new" via package "B" at test.pl line 12.

Looks like it can't find the new() routine .. :/

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] need urgent help on the ff. school assignment: In reply to
that's just it. A and B dont have a new() function. but, since they do use C.pm, i figured the new() function should be in that.
Quote Reply
Re: [deryk] need urgent help on the ff. school assignment: In reply to
Mmm.. the only way I could think of it, is by calling it by

my $var = new B;

..but that wouldn't really make sense.. would it? Unsure

So you're not allowed to edit A.pm or B.pm, to add in a "new" routine? (that would make more sense, as you could set/pass the variables back/forth then, rather than relying on one global variable).

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [deryk] need urgent help on the ff. school assignment: In reply to
You're on the right track with the class variable. You just need to make it specific to the calling class now. Remember, your first argument in the new method will be the calling class.

Code:
package C;

my $this;
$this->{count} = 0;

sub new{
$C = shift;
bless $this, $C;
return $this;
}
1;

In this case $C will be either 'A' or 'B' depending on who called new. Try creating a cache for each package. You're also correct in that both A and B will inherit the new method if they are derived from C. I think that's about the best I can explain it without giving you the answer :)

~Charlie