Miscellaneous PERL info

There were a couple of PERL questions that were left unresolved in class.

Checkbox name/value pairs

In the get_query routine, the predefined variable $; is used to join together multiple choices from a checkbox. So in order to get at them, you will need to split the value from the query string for a checkbox field.

Suppose the checkbox has the name Box. Then you can get all the values that were selected into an array by using split with the $; variable

@list = split (/$;/, $query{'Box'};

You can also use the $; variable to join data. It is a non-printing character, ASCII 28. It could be used to join data that could contain any printable characters.

Optional field in a pattern match

If you want to have an optional field in a pattern match, but to always fill a match variable, you can use alternation with the empty pattern.

For example, suppose that you wanted to match a first name, middle initial, and last name but to have the middle initial optional. This pattern would allow that

/^(\w+)\s+(\w\s|)\s*(\w+)$/

after this match, $1 would be the first name, $3 would be the last name, and $2 would either be empty or would contain the middle initial followed by a whitespace character.