in reply to CGI Module

The problem with the code you posted is that my $forms is a completely different variable from $main::form. The former is a lexical variable, the latter a package variable. See perldoc -q lexical.

Can you tell us more about the module you that you want to access CGI data from? Is it an OO class? If so, you could design the class so that it (or its instances) contain a reference to a CGI object (a has-a relationship). If it is not an OO class, you may have to pass the CGI object as an additional argument to the functions in the module, or else have a package-level configuration variable for the module to hold a reference to some CGI object, and provide a method to set this configuration variable from the calling code.

the lowliest monk

Replies are listed 'Best First'.
Re^2: CGI Module
by Anonymous Monk on Jun 05, 2005 at 03:43 UTC
    This is the entire code for my module. Yes I know I am reinventing the wheel a bit, but the purpose is to store all form elements in a standard location accessable from any of the other modules in the system that would be dependant. Instead of having to call the CGI module from each(which mind you doesnt work anyway.
    package forms; use strict; use warnings; use CGI; sub new { my ($class) = @_; my $self = {_form => undef}; bless $self,$class; return $self; } sub fetch { my ($self) = @_; $self->{_form} = new CGI; return $self->{_form}; } 1;

      What are you trying to do with this module, or why do you need a separate module? That's a lot of typing to simply do my $q = CGI->new.

      Remember that once you've created a CGI object, it reads STDIN, and you won't be able to read that again. If all of your form data is in the message body, only the first CGI object will have been able to see it.

      How are you using this module in your script?

      --
      brian d foy <brian@stonehenge.com>

      One potential problem I see is that, as you have it, the fetch method will return a different CGI object with each invocation. Also, it's not clear to me that a OO class is particularly useful here. (I mean, if you are going to pass an object of this class around so that you can access the CGI object through it, then you may as well pass around the CGI object in the first place.)

      I'm not entirely clear on what you're trying to do, but maybe this would do what you want:

      package forms; my $FORMS; sub fetch { $FORMS ||= CGI->new(); return $FORMS; } ## elsewhere use forms; my $q = forms::fetch();

      the lowliest monk

        That worked. But I found in my infinate wisdom I didnt realize by dumb luck that Iwas calling the module AFTER the return statement so it was being ignored... I has returned to strictly using the CGI module and its all good everything works