Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Cross reference between two classes and FIELDS

by nite_man (Deacon)
on Sep 26, 2006 at 08:14 UTC ( #574874=perlquestion: print w/replies, xml ) Need Help??

nite_man has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Recently I faced with following problem in the project which I develop. Let's say I have three classes: A, B and C. A is a base class for B and C and contains common fields:

package A; use strict; use fields qw(id name); sub new { ... }
Classes B and C inherit class A:
package B; use strict; use base qw(A); use fields qw(addr phone city);
and
package C; use strict; use base qw(A); use fields qw(model type);
As I understand when I create an object B it should inherit properties from the base class - id and name. Sure, it works fine till I need to create inside object B object C and and vice versa. But to do that I have to add statement
use C;
into class B and and vice versa. Right after that the problems start. When I create a new object B it doesn't contain thr properties from the base class. The same for object C.

I "solved" it using require instead of use. Another possibility make preloading all custom modules before use it. Any others ideas or suggestions?

Thanks in advance.

Updated: fixed class C definition.

---
Michael Stepanov aka nite_man

It's only my opinion and it doesn't have pretensions of absoluteness!

Replies are listed 'Best First'.
Re: Cross reference between two classes and FIELDS
by shmem (Chancellor) on Sep 26, 2006 at 10:10 UTC
    Hmm.. your package C isn't fit for use:
    package C; use strict; use base qw(model type); 1; __END__ $ perl -e 'use C' Base class package "model" is empty. (Perhaps you need to 'use' the module which defines that package f +irst.) at C.pm line 5 BEGIN failed--compilation aborted at C.pm line 5.

    Working code to scrutinize would help to help.

    Apart from that, you might want to have a look at Spiffy.

    package A; use Spiffy -Base; use strict; field $_ for qw(id name); sub new { bless {}, $self }
    package B; use A -Base; use strict; field $_ for qw(addr phone city); sub new { bless {}, $self }
    package C; use A -Base; use strict; field $_ for qw(model type); sub new { bless {}, $self }
    #!/usr/bin/perl use strict; use B; use C; use Data::Dumper; $Data::Dumper::Indent = 1; my $foo = new C; $foo->id(123); $foo->name("thingy"); $foo->model("Z80"); my $bar = new B; $bar->id(456); $bar->city("Lhasa"); $bar->name("kouxuan"); print Dumper($foo); print Dumper($bar); __END__ $VAR1 = bless( { 'model' => 'Z80', 'name' => 'thingy', 'id' => 123 }, 'C' ); $VAR1 = bless( { 'city' => 'Lhasa', 'name' => 'kouxuan', 'id' => 456 }, 'B' );

    Spiffy rocks ;-)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      Yes, your right. It's mistake. It should be:

      use base qw(A); use fields qw(model type);

      ---
      Michael Stepanov aka nite_man

      It's only my opinion and it doesn't have pretensions of absoluteness!

Re: Cross reference between two classes and FIELDS
by sgifford (Prior) on Sep 26, 2006 at 14:50 UTC
    Hi nite_man,

    If I use class names like ClassA and ClassB (because B is a system class), everything seems to work OK for me. I have had similar problems to those you describe when I tried to put multiple packages in the same file with nontrivial inheritance relationships.

    Here's the code I tested:

    package ClassA; use strict; use base qw(fields); use fields qw(id name); sub new { my $class = shift; $class->SUPER::new(); } sub getclass { my $self = shift; return ref($self); } 1;
    package ClassB; use strict; use base qw(ClassA); use fields qw(addr phone city); use ClassC; sub addC { my $self = shift; $self->{c} = ClassC->new(); } 1;
    package ClassC; use strict; use base qw(ClassA); use fields qw(model type); sub addB { my $self = shift; $self->{b} = ClassB->new(); } 1;
    #!/usr/bin/perl -l # Test program use ClassA; use ClassB; use ClassC; my $a = ClassA->new(); my $b = ClassB->new(); my $c = ClassC->new(); print $a->getclass(); print $b->getclass(); print $c->getclass(); $b->{name} = 'bee'; print $b->{name}; $c->{name} = 'see'; print $c->{name}; $b->{city} = 'wok'; print $b->{city}; $c->{model} = 't'; print $c->{model};
      Hi sgifford, Probably you're right. The problem with nontrivial inheritance relationships. But how to avoid it (to rebuild whole application is not appropriate way :))?

      ---
      Michael Stepanov aka nite_man

      It's only my opinion and it doesn't have pretensions of absoluteness!

        I only had this problem when I put multiple classes with inheritance relationships in the same file; putting each class in its own file solved the problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://574874]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2021-06-25 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (135 votes). Check out past polls.

    Notices?