in reply to Is there a wantobject like wantarray?

You can solve the problem the other way round: always return an object, but let it overload stringification:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package MyObj; use overload '""' => \&both, fallback => 1; sub new { my ($class, $first, $last) = @_; bless { first => $first, last => $last }, $class } sub first { $_[0]->{first} } sub both { "@{ $_[0] }{qw{first last}}" } } sub makeupname { 'MyObj'->new(qw(John Smith)); } say makeupname(); say makeupname()->first;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Is there a wantobject like wantarray?
by LanX (Saint) on Sep 25, 2014 at 15:49 UTC
    I think that's more or less how want works , it returns an object¹ which unfolds according to the context.

    But I have to admit I never delved too deep into this area of Damian's creativity. :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    ¹) or tied value? (which is actually not very different)

    update

    should be mentioned that this comes with a performance penalty, because even simple scalars need to be "unfolded".

      No, Want isn't even Damian's module. I think you may be thinking of Contextual::Return. Want is far more spooky than that. It examines the OP tree to figure out how the return value is going to be used.

        Oops!

        I read the authors section too quickly, saw Damian's name and thought he gave up maintenance.

        Sorry! :)

        But inspecting the OP tree of the caller? That's possible ?

        Interesting!

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)