Perl로 작성된 간단한 서버 응용 프로그램이 있습니다. 여기에 그것의 작동 버전이 있습니다.
my $client;
while ($client = $local->accept() ) {
print "Connected: ", $client->peerhost(), ":", $client->peerport(), "\n";
while (<$client>) {
if ($mod_ctr == -1) {
$num_count = $_;
init();
}
elsif ($mod_sayaci % 2 == 0) {
$plus_count = $_;
}
elsif ($mod_sayaci % 2 == 1) {
$minus_count = $_;
eval();
}
last if m/^q/gi;
$mod_sayaci++;
}
print "Server awaits..\n";
}
나는 이것이 완벽하게 작동한다고 확신합니다. 이제 mod를 사용하는 대신 작업을 결정하기 위해 클라이언트에서 시작 문자를 가져 오도록 코드를 변경할 때 :
my $client;
while ($client = $local->accept() ) {
print "Connected: ", $client->peerhost(), ":", $client->peerport(), "\n";
$input;
$operation;
$value;
while ($input = <$client>) {
$operation = substr($input, 0, 1);
$value = substr($input, 1, 1);
print "input: $input \n";
print "operation: $operation \n";
print "value: $value \n";
if ($operation == "r") {
print "entered r \n";
$num_count = $value;
init();
}
elsif ($operation == "a") {
print "entered a \n";
$plus_count = $value;
}
elsif ($operation == "e") {
print "entered e \n";
$minus_count = $value;
eval();
}
elsif ($operation == "q") {
# will quit here
}
}
print "Server awaits..\n";
}
클라이언트 측에서는 사용자 r
가 operation
. 지금까지 모든 것이 잘 작동합니다. 첫 번째 입력 후 input
, operation
및 value
인쇄는 정상적으로 작동하지만 항상 첫 번째를 입력 if
하고 인쇄합니다 entered r
. 내가 여기서 무엇을 놓치고 있습니까?
숫자 사용에서 문자열 사용으로 변경하여 실행할 분기를 지정했습니다. 당신은 사용할 필요가 eq
대신 ==
문자열 비교를 할 수 있습니다.
이렇게
if ($operation eq "r") {
print "entered r\n";
$num_count = $value;
init();
}
기타
또한, 당신은 당신 자신과 당신에게 큰 호의를 베푸는 사람을 도울 것입니다.
use strict;
use warnings;
작성하는 모든 Perl 프로그램의 맨 위에 있습니다. "선언"
$input;
$operation;
$value;
블록 내에서 어떤 변수가 사용되는지 알려주는 주석 외에는 유용한 작업을 수행하지 마십시오. 이 쓰기
my ($input, $operation, $value);
훨씬 더 유용한 일을했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다