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.
Since you are defining this subroutine to always return an object of type Own::Dir, I think it'd probably be fine to just call that constructor directly: return Own::Dir->new($parent) (no $ before Own).
One thing to think about would be whether you want to make Own easily subclassable, such that this method on a subclass of Own would return a subclass of Own::Dir. In that case, one way to do that might be to abstract the creation of the object out into another method on Own, so that this method could be easily overridden by a subclass.
In reply to Re: Calling subclass’ method from superclass
by haukex
in thread Calling subclass’ method from superclass
by Fendo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |