Yes, there's a good reason for it: That's how OO works in Perl. When you invoke a sub using ->, then whatever is on the left side of the arrow is passed as the first argument.

If you call My::Class->foo, then foo gets My::Class as its first argument, allowing it to know which class it was called as a method of. This isn't terribly important in most cases, but, without it, things like polymorphic constructors get a lot trickier, since they don't know whether they were called on the base class or a subclass.

If you call $some_obj->foo, then foo gets $some_obj as its first argument, which is extremely important if you want to be able to access the object's instance data.

If you call My::Class::foo, then it gets only the arguments you explicitly provide, the same as if you imported it and then called foo()1. But you probably don't really want to do that. Writing subs that can be called in both OO and non-OO fashions adds complexity and, in most cases, provides little or no benefit.


1 Note that I didn't prefix the function call with &. It's not needed in Perl 5 and has non-obvious side-effects which you almost certainly don't need. Only use the & prefix on function calls if you know exactly what the effects are and why you want those effects.


In reply to Re: Basic Class question by dsheroh
in thread Basic Class question by packetstormer

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.