-3

我编写了一个 perl 子例程,其中使用了两个 if 语句。当我调用这个子例程时,控件进入子例程,打印 xml 但不进入 if 语句。

sub send_msg {
    my ($type,$name,$number,$email,$testid) = @_;
    my $xml = qq{<tolist><to>}
            . qq{<name>$name</name>}
            . qq{<contactpersonname>$name</contactpersonname>}
            . qq{<number>$number</number>} 
            . qq{<email>$email</email>} 
            . qq{</to></tolist>}
            . qq{<from>}
            . qq{<name>$name</name>};
    $xml .= qq{<number>$number</number>}if($type eq 0);
    $xml .= qq{<email>$email</email>}if($type eq 1);
    $xml .= qq{</from>};
  print "\ntype : $type\n";
  print "\nxml :$xml\n"; 


   if ($type == 1)
  {  print"Inside type1";
    $sql3 = "select text from test where TestId='$testid'";
    $sth3 = $dbh->prepare($sql3);
    $sth3->execute
    or die "SQL Error: $DBI::errstr\n";
    my ($message) = $sth3->fetchrow_array();
    my $targetxml="<shorttext>".$message."</shorttext>";
    print "\n $targetxml \n";

  }

   if ($type == 0)
  {
   print "Inside type 0\n"; 
   $sql5 = "select testText,testTitle from test where TestId='$testid'";
   $sth5 = $dbh->prepare($sql5);
   $sth5->execute
   or die "SQL Error: $DBI::errstr\n";
   my ($subject,$title) = $sth5->fetchrow_array(); 
   my $mailxml="";
   $mailxml=$mailxml."<subject>".$title."</subject>";
   $mailxml=$mailxml."<body>".$subject."</body>";
   $mailxml=$mailxml."<type>html</type>";
   print "\n$mailxml\n";

  }
}

在上面的代码中,我使用send_msg(1,Joe,91.97451214551,rich@r.in,32);.
$xml 和 $type 被打印,但为什么它无法输入 if 语句。

4

1 回答 1

1

当然不是。前有return声明if。调试留下的东西?

发生在我们最好的人身上。:-)

OP修复代码后编辑:

$type既不是数字 0 也不是数字 1。您应该使用分隔符打印它

print "length($type) = ". length($type) . "\n";
print "type = <$type>\n";

接下来,将您的代码简化为:

sub send_msg {
   my ($type,$name,$number,$email,$testid) = @_;
   if ($type == 0) {
     print "type is 0\n";
   }
   elsif ($type == 1) {
     print "type is 1\n";
   }
   else {
     print "type is neither 0 nor 1, but <$type>\n";
   }
}

我不认为这是错误,但我注意到你使用

... if($type eq 0);

在另一个地方,不是数字比较(==),而是字符串比较(eq)。

于 2013-06-02T12:20:32.537 回答