On Sat, 14 Feb 2009, Patrick Galbraith wrote:
> Thanks - as soon as I read what you said, I looked at the
> code and saw I instantiated a CGI object at the top of the
> script, package-scoped so I wouldn't have to pass it
> between subroutines. I thought I would test this intuition
> by instantiating in the method that needed it, and pass it
> to the subroutines that need it. Which worked, no more
> segfault.
> ...
> So now
>
> #!/usr/bin/perl
> ...
> use CGI qw(:standard);
> my $webapp= new WebApp();
> ....
> main()
> sub main {
>  my $cgi= new CGI;
>  my $form = $cgi->Vars;
>  my $user= $webapp->getUser();
>   someform($user, $form, $cgi);
> }
> sub someform {
>   ...
>  print $cgi->header('text/html'); <-- no more segfaults
> }

Just my two cents...

    package WebApp;
    sub new { bless { cgi => CGI->new() }, shift }
    sub someform {
        my ($self) = @_;
        my $cgi = $self->{cgi};
        print $cgi->header('text/html');
        # ...
    }

    package main;
    my $webapp = WebApp->new();
    $webapp->someform();

Then as you expand your application you don't have to
keep passing it around.  You get what you wanted in the
first place by using objects and containing all the
per-request objects like CGI within the one per-request
WebApp object.

Mark

Reply via email to