D Server
เนื้อหาของเรา
- กลับหน้าแรก
- CGI-Perl WebBoard
- Php3 WebBoard
- Developer WebBoard
บริการของเรา
- Free GuestBook
- Free Counter
- Free WebBoard
- Free Stats
- Free JumpBox
- Free E-Mail
ข้อมูลของเรา
- ข่าวประกาศ
- D'Server คืออะไร?
- ทีมงาน D'Server
- ติดต่อลงโฆษณา
- ข้อมูลการติดต่อทีมงาน
- ข้อตกลงการให้บริการ
Bandwidth Support by Internet Thailand
Power by Apache
Power by FreeBSD
บทเรียนภาษา Perl

บทที่ 8 : การตรวจสอบเงื่อนไข

การตรวจสอบเงื่อนไข ในภาษา Perl สามารถทำได้โดยการ ใช้ชุดคำสั่งตรวจสอบ เงื่อนไขแบบ if/then/else โดยใช้รูปแบบดังต่อไปนี้

  • โครงสร้างของการตรวจเงื่อนไขแบบ if .. elsif .. else
    • if (Expression) { Block }
    • if (Expression) { Block } else { Block2 }
    • if (Expression) { Block } elsif (Expression2) { Block2 } else { Block3 }

ตัวอย่างการตรวจเงื่อนไข

if ($data)
{
     print "The string is not empty\n";
}
else
{
     print "The string is empty\n";
}

จากตัวอย่างถ้าหากว่า ไม่มีข้อความในตัวแปร $data การตรวจสอบเงื่อนไข จะให้ค่าเป็น false ซึ่งจะแสดงผลลัพธ์ว่า ข้อความว่างเปล่าขึ้นมา

ถ้าหากว่าต้องการ ที่จะตรวจสอบเงื่อนไข มากกว่า 1 เงื่อนไข สามารถทำได้ดังตัวอย่าง ต่อไปนี้

if (!$data)     # The ! is the NOT operator
{
     print "The string is empty\n";
}
elsif (length($a) == 1)     # If above fails, try this
{
     print "The string has one character\n";
}
elsif (length($a) == 2)     # If that fails, try this
{
     print "The string has two characters\n";
}
else     # Now, everything has failed
{
     print "The string has lots of characters\n";
}

ข้อความจำ : elsif ในภาษา Perl จะไม่มีตัวอักษร "e" ไม่ใช่การพิมพ์ผิด หรือพิมพ์ตกแต่อย่างใด และ Block การทำงาน ในการตรวจสอบเงื่อนไขจะต้องอยู่ภายใน เครื่องหมายวงเล็บปีกกาเสมอ แม้จะมีคำสั่งที่ต้องทำ เพียงคำสั่งเดียวก็ตาม ในจุดนี้จะต่างจากภาษา C

เนื้อหาส่วนถัดไป.. ->



[ สนับสนุนช่องทางข้อมูลโดย Internet Thailand | สงวนลิขสิทธิ์ (c) Dream Group 2542 | ข้อตกลงการบริการ | ทีมงาน Dream Group | แนะนำติชม มาที่ webmonster@dserver.org ]