Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

by pemungkah (Priest)
on Nov 08, 2006 at 01:56 UTC ( #582759=categorized answer: print w/replies, xml ) Need Help??

Q&A > object-oriented programming > Calling a method within a double-quoted string? - Answer 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.

Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-03-17 19:00 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (225 votes). Check out past polls.