Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: Defining classes and inheritance using packages within a single .pl file, without creating modulesby jbert (Priest) |
on Nov 29, 2006 at 10:18 UTC ( [id://586649]=note: print w/replies, xml ) | Need Help?? |
Interesting.
The root cause is that perl has read your entire script from start to finish and compiled it, so that Class2::new exists as a method for you to call. What hasn't happened at the time you are trying to execute $max->printHello is that the line @Class2::ISA = qw(Class1) hasn't been executed yet...simply because it is further down the script. Normally, you pull in an external module as a seperate file with a use directive. This is like a require with a BEGIN block around it, which runs all the code in the module as well as compiling it. You could fix this issue by either moving the { package ... } sections before your code or putting a BEGIN { ... } block around your package definitions. However, I'd personally do neither of those. The best way these days to declare inheritance is use base, which also has the handly property of being a compile-time construct. Also - to address your other issues, perl OO gets quite a bit nicer if you start to use some of the CPAN modules. The problem is working out which ones to use :-). Class::Accessor is quite good, and Moose looks like it might be the way of the future. There is a discussion about this here from April of this year.
In Section
Seekers of Perl Wisdom
|
|