@ISA should be a public package variable, not private vocabulary ( my ). Same thing for @EXPORT . Change my to our for all of these declarations.
Even better, depending on the version of perl you have, make life easier with parent or base pragma to load superclasses and to establish class relationships.
Regarding style, you will avoid significant confusion if you create paths to files that contain your module code that match their package names. You would well consider the well-established convention described in the perlmod documentation.
Module names are also capitalized if they do not function as pragmas; pragmas are valid compiler directives and are sometimes called "pragmatic modules" (or even "pragmatics" if you are a classic).
The Cal module uses the _initialize internal method, as described in the perlobj documentation , to facilitate constructor inheritance.
See below for a complete working example.
Cal.pm
package Cal; use strict; use warnings; sub new { my $class=shift; my $self=[]; bless ($self,$class); $self->_initialize(); return $self; } sub _initialize {} sub add { my $ref=shift; my $a=shift; my $b=shift; print "This is from parent class\n"; return ($a+$b); } 1;
Child1.pm
package Child1; use warnings; use strict; use v5.10.1;
test.pl
#!/usr/bin/perl use strict; use warnings; use Child1; my $obj=Child1->new(); my $sum1=$obj->add(1,2); print "$sum1=sum1\n";
Output:
This is from child class
3 = sum1
This is from parent class
3 = sum2
This is from parent class
3 = sum3
source share