# login.pl use CGI; use CGI::Session ( '-ip_match' ); use DBI; use MyPM qw(&open_db &close_db &getSession); use strict; use warnings; my $cgi = new CGI; my $dbh = open_db; my $session = getSession($cgi, $dbh); # Call to MyPM.pm close_db($dbh); ...... code ...... print $session->header(-type => 'application/json'); print $json; ======================================= # MyPM.pm use CGI::Session ( '-ip_match' ); our @ISA = qw(Exporter); our @EXPORT_OK = qw( open_db close_db getSession ); sub getSession { my $cgi = shift; my $dbh = shift; my $CGICOOKIE = $cgi->cookie('CGISESSID') || 'x'; my $lng = length($CGICOOKIE); if ( $lng != 32 ) print redirect( -URL => $home ); my $session = CGI::Session->new('driver:MySQL', $cgi, {Handle=>$dbh}) ; return $session; } #### my $session = CGI::Session->new('driver:MySQL', $cgi, {Handle=>$dbh});