Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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 contemplating the Monastery: (6)
As of 2014-08-30 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (292 votes), past polls