in reply to Re^3: Point me in the right direction with OO inheritance and static variables
in thread Point me in the right direction with OO inheritance and static variables

Thanks again, it all makes sense now!

I've since found that i have another problem though. I was running with an older version of perl(5.8.8) and it didn't like some of the code. I've since upgraded locally to 5.20 and i've had to recompile my local modules but i think i have it working now.

The problem that i'm having is that my script can't find the parent class file.

In my main script i have the following:

use ChildObjects; use ParentObjects;

I then have the 2 files: ChildObjects.pm and ParentObjects.pm

Inside the ParentObjects.pm file i have:

package ParentObjects;

Inside the ChildObjects.pm file i have:

package ChildObjects; use parent -norequire, "ParentObjects";

When i then try in my script:  my $obj=ChildObjects->new(); But i get the following error from my script: Can't locate package ParentObjects for @ChildObjects::ISA at ./script.pl line 54

Can you help? Am i naming/calling my package files wrong?

Replies are listed 'Best First'.
Re^5: Point me in the right direction with OO inheritance and static variables
by tobyink (Canon) on Sep 02, 2014 at 19:11 UTC

    I perhaps should have pointed out that in general, you don't want to use parent's -norequire option.

    -norequire is a hint to parent that it shouldn't try to load the file which the base class lives in. In my example, both classes were in the same file, so trying to load a separate file for the base class would not lead to success. Once you've split the parent into its own file, you should do something like:

    use parent "ParentClass";

      Thanks very much for all this, it's great info

      My program is coming along really well now. I found the hardest part isn't so much the coding but the architecture of the classes and when to inherit or just modify the existing class. As a learning experience it's been really good.

      I have one more "problem" though. I'd like to be able to make sure, that when the object is first created, the data it is created with is in the correct format.

      I think this is similar to Moose's 'isa' keyword after a bit of googling. But again, i'd like to do it in pure perl

      I've no problems writing the actual code, but my problem is where to put it?

      Any help or pointers? Thanks again!

        Try downloading and installing my Type-Tiny distribution from the CPAN, which offers various type checks and is strongly modelled on the Moose type constraint system.

        You can do type checks like:

        use Types::Standard qw( Int ); if ( not Int->check($var) ) { die "It's supposed to be an integer!\n"; }

        The check method shown above will return a boolean. There's also an assert_return method. This will spew out its own error message if the value doesn't pass its type check, so you don't need to do the if not ... die stuff. (And if the value does pass, it simply returns it.)

        use Types::Standard qw( Int ); Int->assert_valid($var);

        Here's how you could do the type checks for a function:

        use Types::Standard qw( Object Num ); sub transfer_money { my $from_account = Object->assert_return( shift ); my $to_account = Object->assert_return( shift ); my $amount = Num->assert_return( shift ); ...; }

        One of the other modules that is bundled along with it is called Type::Params, and simplifies type checks for functions even more, and is pretty damn fast.

        use feature qw( state ); # enable the `state` feature from Perl 5.10+ use Types::Standard qw( Object Num ); use Type::Params qw( compile ); sub transfer_money { state $check = compile( Object, Object, Num ); my ($from_account, $to_account, $amount) = $check->(@_); ...; }

        Even if you decide against using it in your final code, it might help you learn how to do various type checks. For example, if you want to know how to test if $val is an integer or not, you can do:

        perl -MTypes::Standard=-all -le'print Int->inline_check(q($val))'

        This will print out a string of Perl code that shows you how Types::Standard does the check.

Re^5: Point me in the right direction with OO inheritance and static variables
by tangent (Parson) on Sep 02, 2014 at 19:10 UTC
    If you have your parent and child classes in separate files then you need to get rid of the '-norequire' switch:
    package ChildObjects; use parent qw(ParentObjects);
    See the docs for parent