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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.