Include Perl in Questions

You can use the <eqn> and <EQN> tags to include one or more Perl statements in your questions. The value of the last Perl statement in the <eqn> or <EQN> tag is returned by the tag when the question is used.

Understanding the <eqn> and <EQN> Tags

The <eqn> and <EQN> tags are WebAssign tags that let you put Perl statements in your questions. Most often, these statements are used to set or read the values of variables in order to make the question behave in a certain way.

For example, to change the size of the answer box for numerical or fill-in-the-blank questions, you would add this code to Answer:

<EQN $size=5; ''>

This code consists of three parts:

  1. The <EQN> tag itself, which tells WebAssign that the enclosed values should be processed as Perl statements.
  2. The first Perl statement, $size=5; which assigns the value 5 to the WebAssign variable $size, which is used to specify how wide the answer box should be.
  3. The second Perl statement, '' which is an empty string. Because the result of the last Perl statement is always included in your question, answer key, or solution (depending on where the code occurs), the empty string is often used as the last statement.

Both the <eqn> and <EQN> tags include Perl statements, but they display values differently depending on whether they are used in Question, Answer, or Solution.

<eqn> <EQN>
Intended Use Use in Question and Solution. Use in Answer.
Example code A circle of radius <eqn $r=randnum(3,7,1)> has area <eqn $a=$pi * $r**2>. A circle of radius <EQN $r=randnum(3,7,1)> has area <EQN $a=$pi * $r**2>.
Display in Question and Solution A circle of radius 5 has area 78.5398163397448. A circle of radius 5 has area 78.5398163397448.
Behavior in Question and Solution
  • Displays values in red.
  • Displays numeric values up to 15 significant digits.
  • Displays values in black.
  • Displays numeric values up to 15 significant digits.
Display in Answer A circle of radius 5 has area 78.5. A circle of radius 5 has area 78.5398163397448.
Behavior in Answer
  • Displays values in black.
  • Answer key uses 15 significant digits for evaluation with student answers.
  • In numerical mode, displays the number of significant digits specified by the answer key.
  • If the answer key does not specify significant digits, displays 3 significant digits.
  • Displays values in black.
  • Answer key uses 15 significant digits for evaluation with student answers.
  • In numerical mode, displays the number of significant digits specified by the answer key.
  • If the answer key does not specify significant digits, displays up to 15 significant digits.

Paired and Unpaired Forms

Both the <eqn> and <EQN> tags can be used in paired and unpaired forms.

  • The unpaired form consists of a single tag and is used most frequently to display values or in Answer. It has the following syntax (using either eqn or EQN):
    <EQN perl_statement; perl_statement>
  • The paired form consists of an opening tag and a closing tag and is usually used to include longer blocks of Perl statements. It has the following syntax (using either eqn or EQN):
    <eqn>
    perl_statement;
    perl_statement
    </eqn>
    Note If your Perl statements include the greater-than character (>), you must use the paired form.
  1. If needed, open your question in the Question Editor.
    If Do this
    You know the question ID or name
    1. In the search box at the top of the page, select Question.
    2. Type the question name or ID and press Enter.
    You own the question
    1. Click Questions > My Questions.
    2. Click the question name.
    You organize your questions in folders
    1. Click Assignments > Folders and navigate to the folder with the question.
    2. Click the question name.
    You want to use advanced search
    1. Click Questions > Search Questions.
    2. Enter the search criteria you want to use.

      To view only your questions, click me next to Author.

    3. Click Search.
    4. If you own the question, click Edit next to your question.

      If you do not own the question, click View, then click Open in Editor under Previewer Tools.

  2. Start the <eqn> or <EQN> tag.

    You can use either the paired or unpaired form, as described above.

    Best Practice
    • In Question or Solution, use <eqn>.
    • In Answer, use <EQN>.
  3. Type the Perl statements that you want to perform.
    Note
    • To ensure the security of the WebAssign service, some Perl functions are not supported.
    • The Perl newline character (\n) does not work in WebAssign. Instead, include the HTML <br> tag where you need a line break. You must use the paired form of the <eqn> or <EQN> tag.

    End each Perl statement except for the last one with a semicolon (;). The value returned by the last statement is displayed as part of your question or answer key.

    Tip Use two single quotes ('') as your last Perl statement if you do not want any value displayed by your code.
  4. End the </eqn> or </EQN> tag.
    • When using paired tags, the ending tag must match the start tag. You cannot, for example, start with <eqn> and end with </EQN>.
    • When using the unpaired form, do not add a slash at the end of the tag as with XML tags. Just use the closing angle bracket (>).

Example Multiple-Choice Question Using <eqn> and <EQN>

The following table summarizes an actual question.

QID
Name
Mode Multiple-Choice
Question
<eqn>
$weight = randnum(40,150,1);                # pick a random integer between 40 and 150
$venuswt = decform($weight * 0.907, 0);     # calculate weight on Venus
$marswt = decform($weight * 0.377, 0);      # calculate weight on Mars
$jupiterwt = decform($weight * 2.364, 0);   # calculate weight on Jupiter
''
</eqn>
If you weighed <eqn $weight> kg on Earth:<br>
<ul>
<li>Your weight on Venus would be <eqn $venuswt> kg</li>
<li>Your weight on Mars would be <eqn $marswt> kg</li>
<li>Your weight on Jupiter would be <eqn $jupiterwt> kg</li>
</ul>
On which planet would your mass be the least? <_>
Answer
<EQN $ORDERED=5; 'Earth'>
Venus
Mars
Jupiter
Your mass would be the same
Display to Students
Question as displayed to students