Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Is there a clever way to interpolate a method?

by cosmicperl (Chaplain)
on Mar 05, 2009 at 00:07 UTC ( [id://748363]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
  I only know to methods to achieve this, both seem a bit long winded.
Either:-
my $string = 'start ' . $obj->middle() . ' end';
Or:-
my $return = $obj->middle(); my $string = "start $return end";
The latter obviously uses more memory, but is useful if parsing the variable into a lot of strings.
Is there a better way I'm unaware of?


Lyle

Update: my method returned a hash ref, so I ended up with
my $string = "start ${ \$obj->middle() }->{text} end";

Replies are listed 'Best First'.
Re: Is there a clever way to stringify a method?
by GrandFather (Saint) on Mar 05, 2009 at 00:13 UTC
    my $string = "start @{[$obj->middle()]} end";

    If it's obvious how it works use it, otherwise beware that an axe wielding psychotic maintenance programmer that knows where you live may cause your life to be unhappy in the future.


    True laziness is hard work
Re: Is there a clever way to stringify a method?
by Lawliet (Curate) on Mar 05, 2009 at 00:13 UTC

    "${ \$obj->middle }" perhaps? (Search for interpolating methods.)

    And you didn't even know bears could type.

Re: Is there a clever way to stringify a method?
by johngg (Canon) on Mar 05, 2009 at 00:14 UTC

    This should also work.

    my $string = qq{start @{ [ $obj->middle() ] } end};

    Cheers,

    JohnGG

Re: Is there a clever way to interpolate a method?
by wfsp (Abbot) on Mar 05, 2009 at 03:47 UTC
    When interpolation gets messy, and it often does, I reach for sprintf.
    my $string = sprintf( q{start %s end}, $obj->middle(type => q{text}), );
    Perhaps your method could take an argument, e.g. a hash, and return what it is you want rather than go through hoops to get at it.
Re: Is there a clever way to stringify a method?
by cosmicperl (Chaplain) on Mar 05, 2009 at 00:39 UTC
    Hmm... Tough decision. Use these useful methods and lose a some readability or do it the usual way...

    Lyle
      Seems like an easy choice to me. You had it right the first time.
Re: Is there a clever way to interpolate a method?
by kyle (Abbot) on Mar 05, 2009 at 04:22 UTC

    Here's a TIMTOWTDI for join.

    my $string = join q{ }, 'start', $obj->middle(), 'end';

    This way you don't have to remember to put spaces at the borders between literals and interpolateds.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-05-21 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found