Your suspicion is correct. This probably won't save you much typing but,
from perlfaq4:

How do I expand function calls in a string?

This is documented in perlref. In general, this is fraught with quoting and
readability problems, but it is
possible. To interpolate a subroutine call (in list context) into a string:
print "My sub returned @{[mysub(1,2,3)]} that time.\n";
If you prefer scalar context, similar chicanery is also useful for arbitrary
expressions:
print "That yields ${\($n + 5)} widgets\n";
Version 5.004 of Perl had a bug that gave list context to the expression in
${...}, but this is fixed in
version 5.005.
See also ''How can I expand variables in text strings?'' in this section of
the FAQ.

------------
Function calls, I guess, are just to expensive to look for.
$calars are easy to find (even li$t and ha$h elts) because they always
$tart.

----- Original Message -----
From: "Bennett Haselton" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, February 22, 2001 3:57 PM
Subject: interpolating $obj->varname in strings


> I like being able to write
>
> print "Value of var is: $var\n";
>
> but with variable names like or $res->code, if you try to do
>
> print "Value of code is: $res->code\n";
>
> you get:
> Value of rescode is: HTTP::Response=HASH(0x176517c)->code
>
> I can do
> print "Value of code is: " . $res->code . "\n";
> but that gets pretty messy and error-prone in a long string with lots of
> interpolated variables.
>
> On the other hand, if you do:
> print "Value of var is: $hash->{'var'}\n";
> then it works properly.
>
> I suspect that the reason that 'print "$res->code\n";' doesn't work is
> because "code" is not really a member variable name, and is actually a
> function call that returns a value.  But it's still annoying.  Is there
any
> easier way to interpolate those variables in strings?
>
> Maybe this sounds lazy, but if I program perl for another few years, then
> over the long run this will save me typing '" . ' and ' . "' hundreds of
> times -- almost enough keystrokes to justify writing this message :)
>
> -Bennett
>
> [EMAIL PROTECTED]     http://www.peacefire.org
> (425) 649 9024
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
>

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to