Henric has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm writing a web framework and I try to imitate the convenient behaviour of PHP, where script parameters are always present under $_POST, $_GET or $_REQUEST. I'm using CGI::Lite to do this. I created a module called iPLib::iRequest.pm and what i want to do is to put this module in the target script after form submit, so that I don't have to create an CGI::Lite object in every script and retrieve the form parameters with CGI::Lite->parse_form_data().

My (for better explanation simplified) module-code looks like this:
package iPLib::iRequest; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw($_POST); use warnings; use strict; use CGI::Lite; our $_POST; my $cgi = CGI::Lite->new(); $_POST = $cgi->parse_form_data('POST'); 1;

This module is included in my target script like this (don't care for the other modules):

use warnings; use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI::Cookie; use iPLib::iCFG; use iPLib::iDoc; use iPLib::iRequest; my $doc = iDoc->new( BodyOnLoad => 'iInitJS()'); $doc->iAddContent("<br>".$_POST->{'TESTNAME'}."<br>"); $doc->iDraw;

This does not work, I'm getting no warning or error messages, but form-parameter TESTNAME is empty or does not change after subsequent calls. When I use CGI::Lite directly, everything works perfect.

I think i have a basic misunderstanding when the module code is executed and the exported variable $_POST is initialized.

Can anyone help me on the way ?

Replies are listed 'Best First'.
Re: How to initialize a Global in a module
by moritz (Cardinal) on Oct 26, 2011 at 10:42 UTC
    I think i have a basic misunderstanding when the module code is executed and the exported variable $_POST is initialized.

    Directly after use YourModule; is parsed, perl search for that module, compiles it and runs its mainline, including the initialization of $_POST.

    Minimal-ish example:

    $ cat A.pm package A; use strict; use warnings; use Exporter qw/import/; our @EXPORT = qw/$_POST/; our $_POST = 42; 1; $ perl -I. -wE 'use A; say $_POST' 42

      Thanks a lot ... OK, then it can't work this way ... But how can I achieve the desired behaviour ?

Re: How to initialize a Global in a module
by Anonymous Monk on Oct 26, 2011 at 10:56 UTC

      Thanks a lot ... But I want to solve this task by myself and not to use a module. I don't earn money with programming, it is just hobby for fun and I like the Perl language very much ..

        If you refuse to have any of the things you learn be lessons learned by reading the documentation or source code of another module, then you will likely miss out on a lot of opportunities to learn.

        - tye        

        Thanks a lot ... But I want to solve this task by myself and not to use a module...

        Seems contradictory to ask how if you wish to solve it yourself :)

Re: How to initialize a Global in a module
by Anonymous Monk on Oct 26, 2011 at 14:00 UTC
    One good way to approach things is to say that all of your modules will contain 'subroutines' or 'objects,' and if necessary maybe a subroutine that creates a singleton object-instance on demand. Now you have a well-defined way for things to be initialized, and while the methods of the object are executing they have a well-defined "place to put things," i.e. the object itself. This also naturally encourages you to put the logic where it properly belongs ... right there alongside the data that's being manipulated. Values can easily be initialized either in the "new()" method, or lazily, on-demand, and all of that plumbing-work is right there where you'd expect it to be, not scattered all over the application. It works out really well in practice.