WeBWorK Problems

perl statements inside of BEGIN_TEXT ... END_TEXT

perl statements inside of BEGIN_TEXT ... END_TEXT

by Patti Lamm -
Number of replies: 5
I would like the flexibility of being able to replace lines like this
$a = 3;
BEGIN_TEXT
Note that 2 times $a is \{ 2*$a \}.
This is always true.
END_TEXT
which results in the desired displayed text:
Note that 2 times 3 is 6.
This is always true.
with something like this:
$a = 3;
$string = <<EOT;
Note that 2 times $a is \{ 2*$a \}.
This is always true.
EOT

BEGIN_TEXT
$string
END_TEXT
but this results in the (undesired) displayed text:
Note that 2 times 3 is \{ 2*3 \}.
This is always true.
(This is a simple example to illustrate what I'd like to do. In fact, inside \{...\} could include htmlLink, or various other perl statements).

How do I create $string so that what's inside \{...\} gets executed properly when $string is then placed inside of BEGIN_TEXT ... END_TEXT ?

Thanks in advance...
In reply to Patti Lamm

Re: perl statements inside of BEGIN_TEXT ... END_TEXT

by Michael Gage -
Try using the single quoted here document:

$string = <<'EOF';

That prevents the interpolation of $a  variables.

For the most part this should also solve issues with backslash although perl
does not seem to be perfectly consistent on when backslashes are evaluated 
and when they are not.

BEGIN_TEXT  expands to 

TEXT(EV3(<<'END_TEXT'));

-- Mike
In reply to Michael Gage

Re: perl statements inside of BEGIN_TEXT ... END_TEXT

by Patti Lamm -
Thanks, Mike. You're right though, perl isn't "perfectly consistent". The display is now

Note that 2 times $a is \\{ 2*$a \\}.
This is always true.

I actually do want perl to interpolate the $a. But I also want it to execute any statements inside the \{ ... \} once the string is inside BEGIN_TEXT ... END_TEXT. Something like the usual perl 'eval' command? (I know that 'eval' is used differently in webwork.)

-Patti
In reply to Patti Lamm

Re: perl statements inside of BEGIN_TEXT ... END_TEXT

by Patti Lamm -
I just answered my own question. "Something like the usual perl 'eval' command?" Sounds like EV3P to me!

This works:

$a = 3;
$string = EV3P(<<'EOT');
Note that 2 times $a is \{ 2*$a \}.
This is always true.
EOT

I remember reading that EV3 might go away one day. What about EV3P?

Thanks,
-Patti
In reply to Patti Lamm

Re: perl statements inside of BEGIN_TEXT ... END_TEXT

by Michael Gage -
They probably won't go away - just get upgraded. That is why BEGIN_TEXT is safer - it will translate to the latest version. EV3P handles PGML commands also.

Your problem is one of handling the escapes -- which is fragile in then PG language

I suspect that putting quotes around string in your example might also work