Two Mice not working after screen lock

I have written a Perl script allowing the user to use simultaneously two
mice. One right-handed and the other left-handed. The script works. The problem is that after returning from screen lock, both mice become left-handed (which is the config I’ve chosen in mouse configs).
The linux distro I have is Ubuntu 16.04.2 LTS

The script (twomice.pl) is as follows:

#!/usr/bin/perl

# print "Two Micen";
# one left-handed and the other right-handed: cool!

# enter strings from command "xinput list"
my $leftDevice  = "Mouse";
my $rightDevice = "MOUSE";

my $leftHandedOptions  = "1 2 3";
my $rightHandedOptions = "3 2 1";

my $infole = `/usr/bin/xinput list | grep $leftDevice`;
my $infori = `/usr/bin/xinput list | grep $rightDevice`;

my $beginPart = "id=";
my $endPart = "t";
$infole =~ /$beginPart(.*?)$endPart/; $infole = $1;
$infori =~ /$beginPart(.*?)$endPart/; $infori = $1;

my $commandWords = "/usr/bin/xinput set-button-map";
my $leftMouseCfg = $commandWords . " " . $infole . " " . 
$leftHandedOptions; 
my $rightMouseCfg = $commandWords . " " . $infori . " " . 
$rightHandedOptions;

#print $leftMouseCfg . "n";
#print $rightMouseCfg ."n";

system($leftMouseCfg);
system($rightMouseCfg);