Re: Can overloaded '+' return an array? If so, how?



"PeterSShenkin" <shenkin@xxxxxxxxx> wrote:
The following sample code prints:

combine: result= [1 2 3 4]
main: ar = [4]

I expect it to print:

combine: result= [1 2 3 4]
main: ar = [1 2 3 4]

The issue is that "combine", which is the function that '+' maps to,
returns an array. When, in the main program, I say:

my @ar = $ca1 + ca2

I expect the "combine" function to be called in list context. But
evidently it is called in scalar context, because my @ar variable
receives the cardinality of the "result" array, rather than a copy of
the array.

Looking at Ch 13 of the Camel book, I don't see any restriction on
return values of overloaded operators; but maybe I'm just missing it.

If there's a way to do it, but I'm doing it wrong, I'd like to know
that. Either way, please advise.


Your understanding seems to be correct (all overloaded operators are
called in scalar context), and you seem to be correct that this is not
well documented. The closest thing to it seems to be, under overload's
section on <> operator:

BUGS Even in list context, the iterator is currently called
only once and with scalar context.

(This fact seems to be true for all operators, but apparently is only
considered to be a bug for the <> operator.)

(If, instead of what I do in the example, I return a reference to the
array, and alter my main routine correspondingly, then, of course,
everything works.)

Yep. And, in fact, I would usually expect an overloaded addition
operator to turn something of the same class as (at least one of) it's
operands, so I suggest that not only should it return a ref, but a ref
that is blessed into ClassAct.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.



Relevant Pages

  • Re: Perl calculate and average problem
    ... The way I understand this "You _do_ know that @params in scalar context is the number of elements in @params? ... I was told to get a list of numbers from STDIN or a list or arguments, is my array not the correct way to go? ... Josef Möllers ...
    (comp.lang.perl.misc)
  • Re: Special variable "@$"
    ... "do" is a poor name for a subroutine. ... You are using the array @_ in scalar context. ... Using an array in scalar context returns the number of elements in the ... sub deref_test { ...
    (comp.lang.perl.misc)
  • Re: While query
    ... Sinan Unur wrote on MMMML September ... }} @array in this context returns the number of elements in the array. ... The assignment is done in scalar context, ... lists in scalar context. ...
    (comp.lang.perl.misc)
  • Re: How can I sort files by timestamp without slurping?
    ... I had to put the files in an array. ... The reason is that you cannot sort a scalar [or scalar context such ... If you want from newest to oldest, switch the ...
    (perl.beginners)
  • Re: extract matching vales
    ... Ref no spaces in list & unique ... Name No spaces in list but has duplicates but not within the same package ref ... I used a "psuedo" error trap that effectively does the same thing but is ... This will return an array of TRUE's or FALSE's. ...
    (microsoft.public.excel.misc)