use strict; use warnings; use CGI; my $hook_called; my $cgi = CGI->new( \&_hook ); print $cgi->header(), $cgi->start_html("FOO"), $cgi->start_multipart_form, $cgi->filefield('file'), $cgi->submit, $cgi->end_form; sub _hook { my ( $filename, $buffer, $bytes_read, $data ) = @_; if( !$hook_called ) { print "Content-Type: text/html; charset=ISO-8859-1\n\n"; # $cgi is not defined here $hook_called = 1; } else { print "Read $bytes_read bytes of filename\n"; } $hook_called += $bytes_read; } print $cgi->end_html;