Dear Fellow Monks,

please, have a look at the following quite minimalistic CGI program:

#!/usr/bin/perl use strict; use warnings; use CGI; my $cgi = new CGI; print_document_header(); print_document(); ###################################################################### sub print_document_header { ###################################################################### print $cgi->header( -type => 'text/html' ), "\n"; } ###################################################################### sub print_document { ###################################################################### print $cgi->start_html( -dtd => '-//W3C//DTD HTML 4.01 Transitional//EN', -title => 'Hello, world!', ), "\n", $cgi->p( 'Hello, world!' ), "\n", $cgi->end_html(); }

I have three questions regarding this program:

  1. Whenever this program runs I get the warnings Variable "$cgi" will not stay shared referring to lines 15 and 22. Of course, I consulted perldiag in order to learn more about this kind of warning but - just like each of the other pages I found by searching the web for it - perldiag only talks about "an inner (nested) named subroutine ... referencing a lexical variable defined in an outer named subroutine". So where am I nesting named subroutines in my program? I only see two separate subroutines in a main program.
  2. When running this CGI under Apache/2.2.31 (Unix), mod_perl/2.0.8, Perl/v5.10.1 I see it working as expected the first few times I reload the request but after some (varying) number of reloads it crashes with a segmentation fault! What's happening there?
  3. By trial-and-error I found that it makes the program perfectly stable if I pass $cgi to the print_document_header subroutine as a parameter, i.e. calling print_document_header( $cgi ) with
    ###################################################################### sub print_document_header { ###################################################################### my $cgi = shift @_; print $cgi->header( -type => 'text/html' ), "\n"; }
    Why is it sufficient to pass $cgi only to the subroutine which calls $cgi->header and simply use the "global" $cgi in any other subroutine?

Humble thanks for your adwise in advance!


In reply to Variable will not stay shared in a sporadically crashing CGI by Locutus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.