Hello, kind monks of Perl! Once again I come to ask for your wisdom, for I am short of good solutions on the problem I found.
To give some background and idea of structure, I have a fairly simple program which deals with files. The object-oriented ‘Path::Class’ package is used for basic operations on files, but because I need to store some additional info about the files, and do some custom operations, I have decided to make my own subclasses, to extend what ‘Path::Class’ can do. To illustrate the structure of the classes:
Now, I want to make my own ‘parent’ subroutine, which would return an ‘Own::Dir’ object of a file's (or a directory's) parent directory. Because it should work the same for ‘Own::File’ and ‘Own::Dir’, I want to define it in ‘Own’, so that those two can inherit it. The problem is that the subroutine needs to invoke Own::Dir->new in order to make the ‘Own::Dir’ object it's supposed to return. But when I write return $Own::Dir->new ($parent), Perl gives an error saying it ‘can't call method “new” on an undefined value’.
From what I can gather, this is because the ‘Own’ package can't really access the innards of the ‘Own::Dir’ package. I didn't want to mess around with cyclic inheritances, and the best thing I could think of was implementing the subroutine in ‘Own::Dir’ and making that a superclass to ‘Own::File’, but conceptually that wouldn't make much sense, and would be rather inelegant.
Is there a nice way of allowing ‘Own’ to invoke a method of ‘Own::Dir’? Should I maybe restructure the classes in a better way? For the record, I use the ordinary, built-in class (package) system. Thank you in advance!
In reply to Calling subclass’ method from superclass by Fendo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |