cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

Thanks for everyone who chipped in a couple of days ago with ideas on this.

I've been having a play, and I've found something that appears to work and because I'm still not 100% on my OO stuff, I was hoping y'all could look at my solution and let me know if I've missed anything glaringly obvious, or really gone about this the wrong way.

I created a 'Common' module (for now it just contains the cgi function):

package JPI::Common; use strict; use warnings; use CGI; { my $q; sub new_cgi { $q = CGI->new() unless defined $q; return $q; } } 1;

To test this, I created a test module to change a param value:

package JPI::Test; use strict; use warnings; use JPI::Common; my $q = JPI::Common->new_cgi(); sub change_value { $q->param('test','value changed'); } 1;
Then I created the test script:
#!/usr/bin/perl -w use strict; use JPI::Common; use JPI::Test; my $q = JPI::Common->new_cgi(); $q->param('test','initial value'); print $q->header(), 'Before: ', $q->param('test'), "\n"; JPI::Test->change_value(); print 'After: ', $q->param('test'), "\n";
And this does what I was looking for, ie, outputs:
Before: initial value After: value changed
So, have I managed to solve the problem, or did I just get lucky? Am I doing this the most efficient way, or should I be returning a reference to the CGI object?!? Arghhh - another all-nighter takes its toll...

thoughts welcomed :)

cLive ;-)

--
seek(JOB,$$LA,0);

Replies are listed 'Best First'.
Re: using modules already 'used' (part 2)
by vladb (Vicar) on May 11, 2002 at 16:36 UTC
    Heh, interesting play with the modules ;-) -- although, I can't quite grasp the purpose for such a 'wrapper'. Anyhow, straight onto the question..

    Thou art pleas:

    did I just get lucky? Am I doing this the most efficient way, or should I be returning a reference to the CGI object?!?

    Yes, and no. Yes, you are lucky that you have such a wonderful tool at your disposal as Perl :). Your module is built properly. Frankly, I don't see anything peculiar about it. For the second question, I say 'no' because you are already returning reference to the CGI object, don't you? In your new_cgi() method you are simply invoking the cgi constructor (new()) which returns a reference to a blassed 'CGI' hash. You than simply return that same reference to the caller of your constructor. Therefore, indeed you only deal with references here. I see no point optimizing any further in this direction.

    UPDATE: removed my statement of 'faith'.. untill I come up with a new one :)

      I can't quite grasp the purpose for such a 'wrapper'

      I was using CGI in several modules used by several scripts. The two main issues were:

      • POST data only got slurped into the first creation of a CGI object
      • changes to a param() value didn't change that param() in a different namespace

      Because these modules developed organically, there was no clear structure. I didn't want to have to do a lot of re-writing to solve the issues, so creating one shared object quickly solved the problem and (hopefully) future problems.

      cLive ;-)

      --
      seek(JOB,$$LA,0);