CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need help in inheritance

 



naven8
Novice

Jul 5, 2012, 3:26 AM

Post #1 of 6 (1162 views)
Need help in inheritance Can't Post

Hi,
I have 3 file in my program to check the inheritance.


Code
--------------------------File.pl------------------ 
#!/home/utils/perl-5.8.8/bin/perl -w
use B;
use C;
my $self= new C();

--------------------------C.pm-------------------
package C;
use B;
our @ISA = qw(B);
#use base 'B';
sub new {
my $class = shift;
print "$class @ISA\n";
my $self = $class->SUPER::new();
bless $self,$class ;
return $self;
}
1;

----------------------------B.pm------------------
package B;

sub new {
my $self =shift;
my $hash = {};
bless $hash,$self;
return $hash;
}
1;


The above code is giving following error.
Can't locate object method "new" via package "C" at C.pm line 8.

Can someone help me out here?

Thanks


naven8
Novice

Jul 5, 2012, 4:30 AM

Post #2 of 6 (1157 views)
Re: [naven8] Need help in inheritance [In reply to] Can't Post

There is no problem with my code but it is working when i renamed B.pm to some other name


FishMonger
Veteran / Moderator

Jul 5, 2012, 6:11 AM

Post #3 of 6 (1153 views)
Re: [naven8] Need help in inheritance [In reply to] Can't Post


Quote
There is no problem with my code

The majority of the Perl community would say that not using the strict and warnings pragmas is a problem.


Quote
it is working when i renamed B.pm to some other name

That's because B is one of Perl's core modules. http://search.cpan.org/~rjbs/perl-5.16.0/ext/B/B.pm


(This post was edited by FishMonger on Jul 5, 2012, 6:12 AM)


naven8
Novice

Jul 5, 2012, 7:30 AM

Post #4 of 6 (1143 views)
Re: [FishMonger] Need help in inheritance [In reply to] Can't Post

Thanks FishMonger.
The problem is i have copy pasted the code from a site and there they used A,B. I am not sure how that worked for them.


FishMonger
Veteran / Moderator

Jul 5, 2012, 7:59 AM

Post #5 of 6 (1142 views)
Re: [naven8] Need help in inheritance [In reply to] Can't Post

It couldn't have worked for them. Obviously, they just wrote it as an example without actually running to code.


(This post was edited by FishMonger on Jul 5, 2012, 7:59 AM)


BillKSmith
Veteran

Jul 5, 2012, 8:51 AM

Post #6 of 6 (1137 views)
Re: [naven8] Need help in inheritance [In reply to] Can't Post

Your code (as posted) works if you run it with the command line option -I (refer: perldoc perlrun) to force your modules into @INC before the standard modules. Perhaps they did the same thing.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives