Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
Re: Brian? [Was: FAQ 4.36: How can I expand variables in text strings?]
>> Ah, sorry. I didn't notice how ambiguous "Brian" is in this thread.
> Only standing at the beginning of a sentence! There'd be no
> mistaking brian anywhere else ;-) SCNR.
The style guide makes no such exception for the beginning of sentences.
"You're on dry land! Squeaky Whale can't save you now!"
- Stephen Cole
Re: FAQ 4.36: How can I expand variables in text strings?
PerlFAQ Server wrote:
> 4.36: How can I expand variables in text strings?
> Let's assume that you have a string like:
> $text = 'this has a $foo in it and a $bar';
> If those were both global variables, then this would suffice:
> $text =~ s/$(\w+)/$/g; # no /e needed
There is a much simpler and more powefull solution using eval().
There are, of course, dangers in using the eval() solution resulting
from its unbounded power.
There are much greater dangers in trying to withold the knowledge from
the masses because you consider it dangerous and the masses too stupid
to be truseted with it.
When people realise for themselves they can simply use eval():
1. They'll not do it right (use qq() rather than here-doc).
2. They'll not be warned of the dangers of eval().
3. They'll come to distrust and even resent the FAQ.
# Only do this if $text is known to come from a source that would
# be trusted to write/execute their own scripts.
chop ( $text = eval "<<__END__\n$text\n__END__" );
die if $@;