Previous: Value Replacement, Up: Replacements



7.3.5 Temporary Variables

Each pair of evaluated expression in getstats has access to a hash named "ptemp", which can be used for temporary private variables. When used with warnings, the expression can set values in ptemp that the warning text uses.

This is used in zscore warnings:

     warnval(
       "if($stdev) {$ptemp{'zscore'} =
         eval \"(abs($mean - $val) / $stdev)\";
         return (($globals{'zscore-thresh'} > 0)
         && $ptemp{'zscore'} > $globals{'zscore-thresh'}); }",
       "High z-score of \$zscore for \$name in epoch \$epoch."
     )

The variable $ptemp{'zscore'} is set to eval of (abs($mean - $val) / $stdev), and if it is higher than the global variable zscore-thresh, the warning is raised. The warning text reference $zscore, which is picked up from %ptemp.

Notice that eval is used within the predicate, because the standard deviation may be zero. Before the string is evaluated, $stdev could be replaced with zero, and if Perl sees a division by the constant zero it causes an error – even if that part of the expression is never evaluated. By using nested evals, we delay parsing of that expression until we have checked that $stdev is not zero.