perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tuomo Salo <tas...@almamedia.fi>
Subject [mp2] return value of print can break legacy apps
Date Fri, 10 Dec 2004 15:10:28 GMT
Under mod_perl2 the return value of print seems to be the number of
bytes printed. I run across a legacy app (a registry script), that
frequently used the following idiom:


print(CGI::redirect("url_to_error_page")) and return if $error;


While this is obviously a stupid way to write, it seems ok since the
perldoc for print says: "Prints a string or a list of strings.  Returns 
true if successful."

Under mod_perl 1 and (command line) perl 5.8.0 the return value of print
is indeed always 1 and the code works fine. However, under mod_perl2,
the following situation will occur:

CGI.pm will notice that it has been called from mod_perl, and will
perform some undocumented magic to avoid unnecessary parsing:

----snip----
$CGI::revision = '$Id: CGI.pm,v 1.75 2002/10/16 17:48:37 lstein Exp $';
$CGI::VERSION='2.89';
..

sub header
..
     if ($MOD_PERL and not $nph) {
         my $r = Apache->request;
         $r->send_cgi_header($header);
         return '';
     }
----snip----

Now the redirection will be performed, and an empty string is passed to
print. print has been overridden by mod_perl2, and will now return the
number of bytes written, that is, 0. The "and" will naturally
short-circuit, and the "return" will never be reached.

This behaviour is nearly impossible to notice, since the redirection
will happen as usual. The resulting malfunctions are not trivial to
debug, since the error results from two un(der)documented features,
and the symptoms can be completely random.

The natural workaround is of course to write less stupid code:

if($error)
{
   print($q->redirect("url_to_error_page"));
   return;
}

However, this could be a thing worth mentioning in a "possible pitfalls"
document.

   -Tuomo Salo

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Mime
View raw message