Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Operator overloading with returning lists?

by renormalist (Sexton)
on Nov 27, 2008 at 11:33 UTC ( [id://726376]=perlquestion: print w/replies, xml ) Need Help??

renormalist has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I want to overload an operator (ideally '~~') but want the result to be a list. It seems like that's not possible, the list is only handled like in scalar context.

Example:

perl -MData::Dump -E 'package Foo; use overload q{~~} => sub { return +(111, 222, 333) }; $o = bless {}; Data::Dump::dump($o ~~ "whatever") +; say "But I want: 111, 222, 333"'

The only operator that naturally seems to handle lists is <>, but that doesn't visually/semantically fit to my problem. And returning array-refs is my strategy of last resort.

An answer, that operators generally cannot return lists would also be ok. I'm not able to find such a sentence in perldoc overload or the Camel book.

Thanks.

Kind regards,
Steffen

Replies are listed 'Best First'.
Re: Operator overloading with returning lists?
by JavaFan (Canon) on Nov 27, 2008 at 12:00 UTC
    I want to overload an operator (ideally '~~') but want the result to be a list. It seems like that's not possible, the list is only handled like in scalar context.
    Indeed. There were some messages on p5p about this quite recently. I think the outcome was "No, that's not possible. The implementor (Ilya) of overloading deemed it not necessary to be able to ask the context - that is, the operands are always in scalar context".

    Note that when overloading was implemented, '~~' wasn't an operator, and only a few overloadable operators would behave differently in list context anyway. (<> being the obvious on. x behaves different in list context as well, but only if its LHS has parens, making it an odd one).

      x behaves different in list context as well

      Not a very good example. It's more the LHS operand than the context which determines what is returned.

      >perl -le"$x = 'x' x 5; print $x" xxxxx >perl -le"$x = ( 'x' x 5 )[0]; print $x" xxxxx >perl -le"$x = ( ('x') x 5 )[0]; print $x" x

      The only time context matters, x behaves normally.

      >perl -le"$x = ('x') x 5; print $x" xxxxx
        x behaves different in list context as well
        Not a very good example. It's more the LHS operand than the context which determines what is returned.
        That's not all I said. I immediately followed it (not even stopping to start a new sentence) with
        but only if its LHS has parens, making it an odd one
        Both context and the LHS matter, just as I said:
        my $w = 'x' x 5; say "$w"; # Scalar context, no parens. my $x = ('x') x 5; say "$x"; # Scalar context, parens. my @y = 'x' x 5; say "@y"; # List context, no parens. my @z = ('x') x 5; say "@z"; # List context, parens. __END__ xxxxx xxxxx xxxxx x x x x x
Re: Operator overloading with returning lists?
by ysth (Canon) on Nov 27, 2008 at 12:04 UTC
Re: Operator overloading with returning lists?
by LanX (Saint) on Nov 27, 2008 at 12:51 UTC
    Contextual::Return should help you! AFAIS the principle of returning an object which transforms later (to a list in your case) seems to be the same like in the already mentioned Quantum::Superpositions

    Cheers Rolf

    UPDATES:
    + fixed links.
    + after a quick look at Quantum::Superpositions I think Contextual::Return fits much better into your needs.
    + PS: If this works, I'd be glad to get feedback about it 8 )

      Thanks for all your answers. Contextual::Return was a very nice idea. Unfortunately it did not work for the operator. Although C::Return allows more precise detection than the normal wantarray it's still the wrong context in which the expression is evaluated, so I could not get to to use in anything else than SCALAR.

      I heard that '~~' should slightly change its semantics in 5.10.1, maybe it would be possible to even more enhance it with context awareness, e.g. in order to harmonize it with the Perl6 operator (do those allow context detection?). I should now ask in p5p ...

      Thanks again.

        I don't understand exactly what you your aim is. Could you give us more use cases? The LHS is a scalar or is it an array/list???

        Cheers Rolf

Re: Operator overloading with returning lists?
by diotalevi (Canon) on Nov 30, 2008 at 08:13 UTC

    If your overloaded function was written in XS, I could imagine it might be possible to ignore the context and foricibly return multiple things. Just push everything onto the stack even though you're always in scalar context.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      I don't think that will work if the caller expects just one return value.

        I'm just saying it's theoretically possible to violate your caller this way even if it wasn't expecting it.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726376]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-19 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found