In your simple case of printing several values with a constant separator, there's either join, or $,:

print "Values: ", join ", ", $self->get_value1, $self->get_value2, $se +lf->get_value3"; print "\nValues: "; { local $, = ", "; print $self->get_value1, $self->get_value2, $self->get_value3"; print "\nValues: ".$self->get_value1, $self->get_value2, $self->get_ +value3"; # notice the . after "\nValues: " }
All three of those solutions have their downside though.

For the most general case of printing method calls inside double quotes, all I can think of is using a tied hash to call the methods when accessing it. It works but it kind of defeats the purpose of writing accessor methods in the first place:

use v5.14; use strict; use warnings; package Test { sub new { my $scalar = $_[1]; bless \$scalar, shift } sub value { ${ $_[0] } = $_[1] if @_ > 1; ${ $_[0] } } } package CallerHash { sub TIEHASH { return bless \$_[1], $_[0]; } sub FETCH { my $obj = ${ +shift }; my $method = shift; return $obj->$method(); } sub STORE { my $obj = ${ +shift }; my $method = shift; $obj->$method(@_); } } my $obj = Test->new(4); tie my %objAsHash, CallerHash => $obj; say "$objAsHash{value}"; $objAsHash{value} = 42; say "$objAsHash{value}"; say $$obj;

Edit: BTW, the fact that $method = "method_name"; $obj->$method(); works even under strict might help you come up with another solution to print the output of method calls without concatenating bits of strings. Maybe something like: print replace $self, "Values: {self:get_value1}, {self:get_value2}, {self:get_value3}"; where replace uses s///ge;
(so now that I think of it, the tied hash isn't the only solution I can come up with)

Edit2: haukex is right. Using a templating engine is probably the best idea


In reply to Re: Method for reducing tedium of transferring object properties to scalars by Eily
in thread Method for reducing tedium of transferring object properties to scalars by nysus

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.