Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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 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?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2015-10-07 00:34 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (166 votes), past polls