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



PeterSShenkin wrote:

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.

...

(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.)


I fiddled around with your test code, and I found that, while the
code:

my @ar = $ca1 + ca2;

is not called in list context, the following three functionally
equivalent lines:

my @ar = $ca1->combine($ca2);
my @ar = combine $ca1 $ca2;
my @ar = ClassAct::combine($ca1, $ca2);

do return an array as you want -- without the need to change the
combine() method.

(I don't know if this information is useful to you or not, but I
thought I'd share it anyway).

-- Jean-Luc

--
perl -le "print(pack'B*','0'.unpack'B*',pack'w*',
5592691776,37562575106519616,25926642752,354130435682904)"

.



Relevant Pages

  • Re: Parsing a string into a hash
    ... documents what happens when m// is used in a list context: ... You have chosen an extremely poor data structure for your strings. ... an infathomable one-liner. ... A => "Array ...
    (comp.lang.perl.misc)
  • Re: script to find most common last names in a file
    ... splitreturns a list in list context so you need list context on the *left* ... store its results in the @_ array. ... Unlike in normal regular expressions * is *not* greedy in split's regular ... > # then creat a hash table of first and last names, ...
    (perl.beginners)
  • Re: script to find most common last names in a file
    ... John W. Krahn wrote: ... > splitreturns a list in list context so you need list context on the ... > splitwill store its results in the @_ array. ... >> # then creat a hash table of first and last names, ...
    (perl.beginners)
  • Re: beginners-help@perl.org
    ... The output from the system command may be better stored in a scalar and then ... split into an array. ... the qx// operator in list context returns the output as a list ... This is an extremely bad idea (for one thing, strict would ...
    (perl.beginners)
  • Re: Is that I can do something like that ?
    ... the parenthesis provide list context to the right hand side. ... to assign the nth list entry to the nth variable (in the left hand side ... while I am asking %u_info, the user_detail will return a hash, ... Simply return a list, or an array, from the function. ...
    (perl.beginners)