Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Answer: Calling a method within a double-quoted string?

( #582759=categorized answer: print w/ replies, xml ) Need Help??

Q&A > object-oriented programming > Calling a method within a double-quoted string? contributed by pemungkah

The cheap but punctuation-heavy answer:
my $string = "this is an interpolated method call: @{[$obj->method]}";
What's happening here, working from the inside out:
  1. The method call happens, and it returns a list of zero or more values.
  2. Outside that, we have an anonymous array constructor, so now we've got a reference to an anonymous array containing the value(s) returned from the method.
  3. Outside that, we have @{ }, which dereferences the array reference, so we have an array.
  4. Perl now happily interpolates the dynamically-generated anonymous array into the string, because it knows how to do that.
This is essentially what Interpolation.pm does, cut down to adding a few extra characters in your string. Drawback: ugly, and possibly really confusing to the reader. Advantage: only 5 extra characters.

Comment on Answer: Calling a method within a double-quoted string?
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-20 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls