in reply to Printing the result of calling an instance method

Subroutine calls don't interpolate in strings unless you do a bit of particularly kludgy magic. Try this:

print qq(<TD>@{[$obj->attr1()]}</TD>\n);

Very nasty. But it works :)

--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re (tilly) 2: Printing the result of calling an instance method
by tilly (Archbishop) on Apr 02, 2001 at 13:27 UTC
    I know why it works, but I have always wondered why people do that trick. Personally I find it works just fine to use string concatenation, and that is a lot less syntax as well. Besides which, the trick puts the function call in array context which is usually what I didn't mean.

    Now you know better. But most people shown the above are going to get an interesting surprise when comparing:

    print "The current date is @{[localtime()]}.\n"; print "The current date is ".localtime().".\n";
    So, even though using that shows you are an uber-cool Perl hacker, I don't...

      Oh, you're absolutely right. As I said above, it's very nasty - for all kinds of reasons.

      As for the list/scalar context problem, you could do something like:

      print "The current date is @{[scalar localtime]}.\n";

      But I really wouldn't recommend it :)

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

Re: Re: Printing the result of calling an instance method
by ColtsFoot (Chaplain) on Apr 02, 2001 at 12:47 UTC
    davorg, Thanks your a star, I really just wanted to output the attribute from within a HERE doc. Your solution is perfect ++ to you