Perl CGI  «Prev 

Perl Regular Expression Explanation

The htmlp() routine uses the following code to replace strings in the input file:
#  to execute perl code
s/$\{(.*?)}/eval($1),""/eg;

# $$filename to include another file
s/$$([\S;]+;?)/htmlp($1)/eg;

# $variable to include a variable
s/$(\w+)//eg;

Each of these regular expressions replace the left-hand side (LHS) with the results of an expression. For example, the first one:

 s/$\{(.*?)}/eval($1),""/eg; 
 

matches anything that starts with a $, followed by a pair of curly braces and what is contained within the braces.
You may note the ".*?" within the parenthesis. This makes the repeat less greedy. Without the question mark, the .* would match all the rest of the characters of the string, up to the last "}" character. With the question mark, it matches up to the first }.
On the right-hand side (RHS) of the first substitution, the expression eval($1) inserts the return value of the expression in $1 (which is in turn replaced with the contents of .*? from the LHS). This returned value must be discarded so that it does not end up in your HTML! In order to accomplish that, the ,"" is used to force the whole expression to return a null string, which will be inserted in the HTML without consequence.
Question: So, if the return value is discarded, what effect does this code have, if any?
Answer: Remember that the standard output stream of your CGI program is passed through the server to the client (browser). During the process of this substitution, whatever data is sent from the $1 expression to the standard output will be sent to the client at that time. The effect is that the output from this code will be inserted in the correct place with the .htmlp file.
The (RHS) right hand side of the second substitution works very similarly. It allows you to insert another file in the stream, much like a server-side include. (In fact, this is the code I use to accomplish SSI on servers that do not otherwise support it.) The RHS expression simply includes calls the htmlp() routine recursively.
The third substitution is probably the simplest. It simply inserts the value of a variable in the stream.