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

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.