RE: interpolating $obj->varname in strings
hmmm - and I'm guilty of not reading your message properly! Laughing at myself... -Original Message- From: Bennett Haselton [mailto:[EMAIL PROTECTED]] HUH? $res->code is not a variable, it's a call to a method. Is that what you mean? Because if you really mean a variable, than it should be print "Value of code is: $res->{code}\n"; which works. Sorry for being pedantic, but it's unclear what you mean, and I don't think the other respondents have noticed the contradiction in your question. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: interpolating $obj->varname in strings
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
RE: interpolating $obj->varname in strings
-Original Message- From: Bennett Haselton [mailto:[EMAIL PROTECTED]] >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"; HUH? $res->code is not a variable, it's a call to a method. Is that what you mean? Because if you really mean a variable, than it should be print "Value of code is: $res->{code}\n"; which works. Sorry for being pedantic, but it's unclear what you mean, and I don't think the other respondents have noticed the contradiction in your question. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
Re: interpolating $obj->varname in strings
> 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"; Its more effecient to pass arguments to print() using commas: print 'Hello ', $res->code(), ' World'; Than trying to make Perl interpolate them inside of the string. ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: 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. you may want to read the information located here: http://perl.plover.com/#identity i stumbled on to this and thought it was interesting mark ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
RE: interpolating $obj->varname in strings
A third option: print "Value of code is @{[$res->code]}"; -Paul > -Original Message- > From: > IMCEANOTES-Ian+20Stewart_Great+20Lakes_AirTouch+40AIRTOUCH+5FC > ONNECT@BAM > .com > [mailto:IMCEANOTES-Ian+20Stewart_Great+20Lakes_AirTouch+40AIRT > OUCH+5FCON > [EMAIL PROTECTED]] > Sent: Thursday, February 22, 2001 4:24 PM > To: [EMAIL PROTECTED] > Cc: [EMAIL PROTECTED] > Subject: Re: interpolating $obj->varname in strings > > > Two options I can think of: > > 1) Pass a list to the print statement: > > print "Value of code is: ", $res->code; > > 2) use printf(): > > printf("Value of code is: %s", $res->code); > > > > HTH, > Ian > > > > From: Bennett Haselton <[EMAIL PROTECTED]> on > 02/22/2001 03:57 > PM > > To: > [EMAIL PROTECTED]@SMTP@JVEXCHANGE > cc: > bcc: > > 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 ___ Perl-Win32-Users mailing list [EMAIL PROTECTED] http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users