I am rewriting a debugging module to be object-oriented. This module makes extensive use of CGI.pm's HTML tag generation capability. As a result, it instantiates a new CGI object when my object's new() constructor is called. Unfortunately, if the calling CGI program uses the POST method to receive data, my routine's instantiation of a new CGI object will cause the data in STDIN to be read. As a result, if they create my object before a new CGI object, they lose all of their POST data!

To get around this, I try to read $ENV{'CONTENT_LENGTH'} amount of data from STDIN if the POST method is used. If read any data, I kill the program telling the user that they need to instantiate a new CGI object before they instantiate a debugging object. Here's the code:

sub new { my $class = $_[0]; my $buffer; if ( $ENV{'REQUEST_METHOD'} eq 'POST' ) { read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } my $cgi = CGI->new(); if ( length $buffer ) { print $cgi->header( "text/plain" ) . "Data was read from STDIN while using the POST method.\n +" . "You must instantiate a new CGI::DebugVars object AFTER\ +n" . "instantiating a CGI object."; exit; } my $objref = { _active => 1, _border => 1, _continue => 0, _cgi => $cgi, _pretty_not_installed => 0 }; bless $objref, $class; return $objref; }
My problem is that it seems like a very ugly hack. I'm really not testing to see if a CGI object already exists, I'm inferring its existence from the presense of data available in STDIN. Is there a clean way to determine if the user has already instantiated a CGI object (short of searching through the packages for one)?

I'm also wondering if it's possible that other data may be available through STDIN in a CGI script? Since my routine reads that data, it will cause a problem for the end-user's code.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just go the the link and check out our stats.


In reply to CGI object already instantiated? by Ovid

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.