# generate either HTML or graph depending on CGI parms my $graph_parms = (new CGI)->param('graph'); if ($graph_parms) { gen_graph($graph_parms) } else { gen_html() } sub gen_html { # ... generate other HTML ... # add IMG element that calls back to us to get image print qq(); } sub gen_graph { my $parms = shift; my $img_data = generate_graph( $parms ); print "Content-Type: image/png\n", "Cache-Control: max-age=300\n\n", $img_data; }