Bart Lateur said:
Not if the input contains quotes.
That's why the standard way to write this is
chop( my $string = eval "<<end_of_data\n$userinput\nend_of_data\n");
This this technique is totally inappropriate if $userinput is ever
going to come from someone to whom you wouldn't grant the right to run
their own scripts. So for the OP it is useless.
It also fuzzes the line between data and program. This can be bad and
is something you shouldn't do without careful consideration. On the
other hand it's not nearly as bad as some people would have you
believe.
I've been trying to get this explaination into the FAQ for years. The
_question_ is in the FAQ but the current version of FAQ patronisingly
withholds the answer. So that when people "discover" it for themselves
as the OP did in this case:
1) They don't come up with the here-doc trick.
2) They are not alerted to the downsides.
3) They loose trust in the FAQ.
Worse still, the advice in the FAQ is "roll your own templating
system" without so much as a mention of the fact that there are one or
two templating systems out there already.
Then perhaps String::Interpolate can be of use. See CPAN.
No, String::Interpolate is useful if you _do_ want to allow executions
but want to attempt to corral them a bit.
Note however that the security of String::Interpolate relies on Safe
and Safe is known to have security holes. String::Interpolate plugs
several of them but I (who wrote it) would not trust
String::Interpolate as protection against anything but a causual
attacker.