(This is not really Perl 6 specific, but it was Perl 6 syntax that made me think about the matter.)

In Perl 6, arrays are now objects, namely inheriting from Array, as in any self-respecting object-oriented language. As a trivial case example, suppose you want to grep for something in an arbitrary array. In Perl 5 style, you can do it as a function call to grep, giving the array as a parameter:

# Function call style. if (grep $something, @array) { ... } my @result = grep $thingie, @array;

In Perl 6, one of the many ways to do this is to use the method call syntax on the array itself:

# Method call style. if @array.grep($something) { ... } my @result = @array.grep($thingie);

In the former, it is clear, at least to a Perl 5 programmer, that one has to have an array or at least something acting exactly like an array (implementing the same interface) as an argument to grep, since this is really what we are doing: grepping an array.

In the latter, the array acts as an object that happens to have the method grep, which, as it happens, will grep the array for us.

I'm not against object-oriented programming, which certainly has its place. I'm not against abstract data structures, which are certainly very useful. However, I find the latter, method call style mentally more taxing than the former, function call style. This is because in the general case, or even in this particular case, in order to find out what methods the data structure supports, one has to find out the exact type of the data structure. Perhaps it's an Array, but is it also a Frobnicator, and does it implement the role JackInTheBox? What will grep do with this data structure (or object), anyway? To find out for sure, one has to know where grep has been defined or overloaded.

This sounds silly, but it is what my unconscious has been giving me warnings about. Consider any "standard methods" for standard datatypes, such as push, pop, and grep for Array. What if the datatype we are using overloads any of those, but we want to be sure we always use the standard grep? Perhaps this is a non-issue, as one can always use the function call syntax to force interpreting grepping as grepping on an Array, or in some other way force the variable to be interpreted as an Array.

However, the other issue stands: For some reason, I find it much harder to (imagine) finding out which methods a datatype has instead of looking up built-in function definitions from perlfunc, and then weeding out those that won't work for this particular datatype I'm using (Array, Hash, etc.). This is because there can be potentially an infinite number of datatypes, all with their own set of methods. True, there can also be a potentially infinite list of (built-in) functions that operate on certain kinds of data, but at least it is always clear from the function signature what it expects the data to be and from the description what it does.

Maybe I just need to write more OOP code and leave the procedural and functional worlds for a while.

(As a sidenote, there are many other ways in which to grep in Perl 6, such as:
# Feed style. my @result <== grep $thingie <== @array; @array ==> grep $thingie ==> my @result; # Another method call style. my @result = grep @array: $thingie;
The feed style doesn't suffer from this problem, but the alternative method call style does.)

--
print "Just Another Perl Adept\n";


In reply to [Perl 6] Method call versus function call on a data set by vrk

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.