I’m writting a small server with perl. There is some small problem. When the client give me a sentence like this “op:xxx:xxx:xxx”, I’ll get op. then do things depending on what op is. It works will if the op is adduser and so on. (I use if $op eq “adduser”…)
But when I get a “getList:xxx:xxx” and I have get the $op = getList, it can’t pass it like “if $op eq “getList””. I know, it must be my mistake. But I just can’t find it.
thank you for everyone.
use warnings;
use strict;
package MyPackage;
use base qw(Net::Server);
our %data_base;
our %tag_base;
sub list {
my %resault;
foreach ( keys %tag_base) {
print STDERR $_ . "1";
my @tags = split /:/, $tag_base{$_};
foreach ( @tags) {
$resault{$_} ++;
}
}
my @tags;
foreach ( keys %resault) {
push @tags, "$_,$resault{$_}";
}
$_ = join ";", @tags;
print ;
print STDERR ;
}
sub users {
my $topic = shift;
my @users;
foreach ( keys %tag_base) {
push @users, $_ if $tag_base{$_} =~ /$topic/;
}
$_ = join ";", @users;
print ;
}
sub process_request {
my $self = shift;
my $person;
my @info;
while (<STDIN>) {
my @gets = split /:/, $_;
print STDERR "@gets\n";
# $data_base{shift @person} = join ":", @person;
my $op = shift @gets;
$op =~ s/\s//;
print STDERR $op . "\n";
if ( $op eq "adduser") {
my $user_name = shift @gets;
if ( exists $data_base{$user_name}) {
print "already_exist";
} else {
$data_base{$user_name} = join ":", @gets;
print "addUserSu";
}
} elsif ( $op eq "login") {
my $login_name = shift @gets;
my $login_pw = shift @gets;
if ( defined $data_base{$login_name}) {
$person = $data_base{$login_name};
@info = split /:/, $person;
$info[0] =~ s/\s+//;
if ($login_pw eq $info[0]) {
print "$person";
} else {
print "/$info[0]/";
}
} else {
print "unexist_user";
}
} elsif ( $op eq "addTag") {
my $tag_user = shift @gets;
$tag_base{$tag_user} = join ":", @gets;
print "addTagSu";
} elsif ( $op eq "getList") {
print STDERR "right";
&list;
} elsif ( $op eq "getUsers") {
&users;
}
}
}
MyPackage->run(port => 13800);
I can see two (simple) reasons this might fail:
You only remove one whitespace: The first one. If your intention is to strip all whitespace, you’d want
s/\s+//g.And second:
Random capital letters in strings, variable names and commands is Evil.
eqis case sensitive, so if$opis"getlist", thenif ($op eq "getList")will be false. Unless capitalization is important to you, you could doif (lc($op) eq "getlist").Without sample input, expected output and actual output, this is however nothing more than guesswork.
Also, as a debug statement, this is useless:
That is easily confused and overlooked. For example, if
$opis empty, it just produces a blank line in your error log. Use:Now you will be able to identify the line where
$opshould appear, and you will be more easily see whitespace surrounding it.