String Issues

Posted by Sb on Mon 11 Jul 2005 12:40 AM — 4 posts, 21,323 views.

#0

$name = $world->GetVariable("MyName");
$world->send('gtell {g[{R'.$name.'{g]{R Blind!!!');


That works but...
$name = $world->GetVariable("MyName");
$world->send("gtell {g[{R$name{g]{R Blind!!!");
does not. It barks about {g], but that makes no sense...
USA #1
It's probably looking for a hash named 'name' (and it's going to return a scalar), and then trying to find the element of 'g]' (since the next { makes it no longer a good name). It's probably giving an error (rather than just creating the hash, with an empty value for g]) because it doesn't like the bracket.

Actually, It is, except obviously the bracket can't be part of a variable name, and it warns us about the g being a future keyword.

Another thing that is also important to keep in mind when dealing with brackets is that they can also be misconstrued as regular expression delimiters. It obviously didn't come into play here though, it just popped into my head when I first glanced over the code.
USA #2
If I remember correctly, you can use single quotes to prevent Perl from doing evaluation like that.
USA #3
What you can do is enclose it in brackets (I know, more brackets, lovely, eh?) like this:
$world->send("gtell {g[{R${name}{g]{R Blind!!!");