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 |