I am glad you are open to that argument, because that is exactly the issue.

While you can call a method of the class without having an instantiated object, the right function will be called. Ditto if you call a constructor from an instantiated object. That is because Perl is very, very lax about this.

But if you assume that either call will actually work, then you are breaking the rule that Thou Shall Not Covet thy Object's Internals.

Now if the module author wants to, they can blur the distinction between class methods (like new) and instance methods (like pretty much anything else) by making the class method work as an instance method. But (depending on the module) they likely cannot make instance methods work as class methods.

In other words you can halfway blur the distinction, but you cannot really eliminate it. If you cannot eliminate a fundamental distinction, then I think it is important to teach it. And if you are going to teach it, then what value have you added by adding code to your method to make it harder to see the distinction?

In short in my Perl code there are class methods, meaning methods that are supposed to be invoked through the class' name. There are instance methods, meaning methods that are supposed to be called from an existing instance.

But rarely, if ever, do the twain meet.

(Incidentally since I invoked merlyn's name, I feel that I should point to his post on this same topic...)


In reply to Re (tilly) 4: How to call Class method inside an object? by tilly
in thread How to call Class method inside an object? by chorg

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.