(Ovid) Re: How Does Interpolation Work?
by Ovid (Cardinal) on Jul 21, 2000 at 02:16 UTC
|
If you're using variable interpolation, Perl may not always understand exactly what you intend. To get your code to print "yesgoodbye", try putting braces around the variable to force Perl to recognize exactly what variable it's interpolating:
print "${hello}goodbye";
Update: I find it somewhat amusing that no one (as of this posting) pointed fx's obvious solution. So I can't help but wonder, are there inherent problems with his solution, or does one person suggesting a solution lead the PerlSheep (that would include me) down a narrow path? | [reply] [d/l] |
|
|
In this situation: print $hello, "goodbye\n"; would also work I believe, and clearly separates the variable from the string.
== fx, Infinity is Colourless
| [reply] |
RE: How Does Interpolation Work?
by Russ (Deacon) on Jul 21, 2000 at 02:26 UTC
|
my $R = 'Twenty';
print "$R_Something_[1]";
Since we are interpolating, Perl tries to find @R_Something_
and fails. ${R} works perfectly, as Ovid points out.
Russ
Brainbench 'Most Valuable Professional' for Perl | [reply] [d/l] |
|
|
I read your example, and wondered how this would interpolate:
my $R="foo";
print "${R}[0]\n";
I thought it would either interpolate $R and
print foo[0], or interpolate as $R[0],
and print an empty string. Instead, I got an unexpected
result:
[0]
Anyone know why? I have a feeling I might be missing
something in the meaning of ${R}[0].
These act in the expected way:
my $R = "foo";
my $R[0] = "bar";
print "${R}\[0]\n";
print "${R[0]}\n";
Any enlightenment?
Alan
Update:
Ah, that makes sense, a bit too subtle for me to get without some help. Thanks! | [reply] [d/l] [select] |
|
|
Okay, you made $R lexical with my. ${R} is a
symbolic reference to the scalar variable named 'R'. Just
left alone, Perl will figure out you want the lexical
variable and not a variable in the symbol table.
Looking for ${R}[0], however, perl -w says this:
Name "main::R" used only once: possible typo at - line 2.
So, when you add characters that look like a variable
specification Perl is only looking in the symbol table.
If you don't make $R lexical, it will work like you
originally expected:
$R="foo";
print "${R}[0]\n";
prints "foo[0]"
To get perl to keep interpolating, just keep adding {}'s:
use vars qw/$R @foo/;
$R="foo";
@foo = (42);
print "${${R}}[0]\n";
prints 42
:-)
BTW, I seem remember some code posted some time ago which
did this kind of "multiple interpolation." I can't find it.
Anyone know what node that is (or am I just hallucinating
again?
("Too much LDS" -- Kirk about Spock in ST:IV))
Update: Here it is: Double Interpolation of a String
Russ
Brainbench 'Most Valuable Professional' for Perl | [reply] [d/l] [select] |
Re: How Does Interpolation Work?
by jeorgen (Pilgrim) on Jul 21, 2000 at 14:35 UTC
|
This is something that bugs me a lot: Is there anyway to interpolate a method call within double quotes:
print "Name is $obj->name";
...right then and there?
/jeorgen | [reply] [d/l] |
|
|
sub yahoo()
{
return "Easy";
}
print "${\yahoo()} does it!\n";
Never mind the odd sub naming convention, it is the result of too little sleep. :)
Cheers! | [reply] [d/l] |
|
|
package Eval;
sub import
{
my ($package, $name) = @_;
$name = 'Eval' unless defined $name;
my %magical_hash;
tie %magical_hash =>Eval;
my $caller = caller;
*{caller . '::' . $name} = \%magical_hash;
}
sub TIEHASH
{
my $self = \'fake';
bless $self => Eval;
}
sub FETCH
{
my ($self, $value) = @_;
$value;
}
Looks pretty freaky, eh? Okay, now try this:
use Eval => ':';
$salary = 43_000;
print "After your raise, you will make $:{$salary*1.06}.\n";
Yeek. Have fun! | [reply] [d/l] [select] |
|
|
Thanks for the example, CMonster, but I don't get it to work on my machine (ActiveState 5.005_03).
I get:
Warning: Use of "caller" without parens is ambiguous at C:\... line 11.
syntax error at C:\... line 29, near "use Eval =>"
So I change from:
use Eval => :;
to:
use Eval ( ':');
at line 29.
Then I get:
Modification of a read-only value attempted at Eval.pm line 17
and that one I just don't know what to do about. I tried to change
my $self = \'fake'
to:
my $self = {}
at line 16, but that didn't help.
/jeorgen
| [reply] [d/l] [select] |
|
|
RE: How Does Interpolation Work?
by Anonymous Monk on Jul 21, 2000 at 15:51 UTC
|
Try print "${hello}goodbye"; ... | [reply] |
RE: How Does Interpolation Work?
by mhorvat42 (Acolyte) on Jul 26, 2000 at 21:56 UTC
|
try something like this
$hello = "yes";
$hellogoodbye = "no";
print "${hello}goodbye";
this should work
do or do not -- there is no try | [reply] |