in reply to mod_perl and multi user environment - clashes with other users's data

Because print_response closed over the my $name that existed when the script was compiled. That my $name is different from the one in the second pass.

Solution 1: Use package (our) variables instead of lexical (my) variables for globals.

#!/usr/cisco/bin/perl -w use strict; use CGI; our $q; our $name; sub print_response { print "Content-type: text/plain\n\n"; print "Thank you, $name!"; } { local $q = CGI->new; local $name = $q->param('name'); print_response( ); }

Solution 2: Use arguments instead of global variables.

#!/usr/cisco/bin/perl -w use strict; use CGI; sub print_response { my ($name) = @_; print "Content-type: text/plain\n\n"; print "Thank you, $name!"; } { my $q = CGI->new; my $name = $q->param('name'); print_response($name); }

Update:

Solution 3: Use objects instead of global variables.

#!/usr/cisco/bin/perl -w use strict; use CGI; sub new { my ($class) = @_; my $q = CGI->new; my $name = $q->param('name'); return bless({ q => $q, name => $name, }, $class); } sub print_response { my ($self) = @_; my $name = $self->{name}; print "Content-type: text/plain\n\n"; print "Thank you, $name!"; } { my $self = __PACKAGE__->new(); $self->print_response(); }

Replies are listed 'Best First'.
Re^2: mod_perl and multi user environment - clashes with other users's data
by rsennat (Beadle) on Dec 05, 2005 at 09:47 UTC
    hi,
    Even after using "our" keyword, im facing problem with my code. with that sample your solution works fine. but please check whats wrong in my code,
    #! /usr/cisco/bin/perl -w #use strict; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use HTML::Template; use Data::Dumper; use hiGuiStatus; # my own module # send the obligatory Content-Type print "Content-Type: text/html\n\n"; #$q = new CGI; #print $q->Dump(); our $status = hiGuiStatus->new(); our $userid = $status->get_ldap_user_name(); print $userid; my $err_msg; ($err_msg, %hoh) = $status->read_file($userid); #print Dumper \%hoh; if(param('sort')) { my $sort_sel = param('sort_option'); ($err_msg, @sort_keys) = $status->jobs_sort($sort_sel); #print $err_msg; } if(param('terminate')) { my @del_ids = param('check_del'); ($err_msg) = $status->jobs_terminate(\@del_ids, $userid, \%hoh); ($err_msg, %hoh) = $status->read_file($userid); } if(param('publish')) { my $ddts = param('ddts_no'); my @del_ids = param('check_del'); $status->jobs_publish($ddts, \@del_ids); } if(param('delete')) { my @del_ids = param('check_del'); ($err_msg) = $status->jobs_delete(\@del_ids, $userid, \%hoh); ($err_msg, %hoh) = $status->read_file($userid); } @loop = $status->jobs_loop(); #print Dumper \@loop; my $template = HTML::Template->new(filename => 'hiGuiStatus.tmpl'); # call param to fill in the loop with the loop data by reference. $template->param(err_msg => $err_msg, job_loop => \@loop); # print the template print $template->output;

    Basically this code does retrieve data from a text file for a particular user who has logged in and display it using HTML::Template.
    I used the keyword "our" and still face the problem of other user's data being displayed in my browser, ONLY IN mod_perl

    thanks
    rsennat