Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: object oriented Perl advice / constructor creation in child class

by kcott (Archbishop)
on Jul 10, 2018 at 09:13 UTC ( #1218230=note: print w/replies, xml ) Need Help??

in reply to object oriented Perl advice / constructor creation in child class

G'day smarthacker67,

While I assume you're just using "A", "B", etc. as examples, this generally isn't a good idea as you can run into name collisions; for instance, is a core module which you'll already have installed.

I don't understand where "" sits in your hierarchy. It seems to be both a file and directory which is a subdirectory of another file (""). Obviously that can't be the case but I don't know what you want: "lib/C/"?, "lib/"?, something else?

Here's some skeleton code to show how you might achieve this sort of thing with Moose. This is intended as an academic exercise: there's no suggestion that this is production-grade code; nor that Moose is the best choice for your specific application.

$ cat lib/ package A; use Moose; has bar => ( is => 'rw', default => 'A-bar', ); __PACKAGE__->meta->make_immutable;
$ cat lib/ package C; use Moose; sub whos_who { my ($self) = @_; print "whos_who() is in package: ", __PACKAGE__, "\n"; print "\$self is in package: ", ref($self), "\n"; return; } __PACKAGE__->meta->make_immutable;
$ cat lib/ package D; use Moose; has 'bar' => ( is => 'rw', default => 'D-bar', ); __PACKAGE__->meta->make_immutable;
$ cat lib/ package C1; use Moose; extends 'C'; use A; use D; has a => ( is => 'ro', default => sub { A->new } ); has d => ( is => 'ro', default => sub { D->new } ); sub foo { my ($self) = @_; print $self->a, "\n"; print $self->a->bar, "\n"; print $self->d, "\n"; print $self->d->bar, "\n"; $self->whos_who; return; } __PACKAGE__->meta->make_immutable;
$ cat ./ #!/usr/bin/env perl use strict; use warnings; use lib 'lib'; use C1; my $c1 = C1::->new(); $c1->foo();
$ ./ A=HASH(0x25db7c0) A-bar D=HASH(0x25db880) D-bar whos_who() is in package: C $self is in package: C1

Update (typo fix): s{lib/C/}{lib/C/}

— Ken

Replies are listed 'Best First'.
Re^2: object oriented Perl advice / constructor creation in child class
by smarthacker67 (Beadle) on Jul 11, 2018 at 06:45 UTC
    Hi Sir,

    Thanks for this brief explanation I would definitely give a try.

    dir structure looks like
    lib/ lib/ lib/ lib/C/ lib/
    Wanted to achieve deep inheritance where object of C1 should inherit methods of A,B,C, & D

      dir structure looks like

      ... lib/ ... lib/C/ ...

      As previously stated, will already exist on your system. Attempting to write new code with "package B;" will cause you all sorts of problems. You should avoid this name.


      That's fine. The class name will become "C::C1". My previously posted "lib/" should be moved to "lib/C/", and the first line should be changed to:

      package C::C1;

      In my "./", you should change

      use C1; my $c1 = C1::->new();


      use C::C1; my $c1 = C::C1::->new();

      After making those changes, note the difference in the last line:

      $ ./ A=HASH(0x9897d0) A-bar D=HASH(0x989890) D-bar whos_who() is in package: C $self is in package: C::C1

      The hexadecimal values in "X=HASH(0xhhhhhh)" will likely be different: that's not significant; you could get different values on separate runs using the same code.

      — Ken

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1218230]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2023-09-25 08:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found