27 April 2011

use strict in perl

strict module เป็นตัวที่คอยช่วยตรวจสอบการเขียนโค๊ดของเราว่าถูกต้องตามหลักหรือไม่

#!c:\perl\bin\perl.exe

use strict;
use CGI;

my $cgi = CGI->new;
print $cgi->header("text/html");

$ref = \$foo;
print $$ref; #ถ้าใส่ use strict จะแสดง error แต่ถ้าไม่ใช่จะเป็นหน้าเปล่า

$ref = "foo";
print $$ref; #ถ้าใส่ use strict จะแสดง error แต่ถ้าไม่ใช่จะเป็นหน้าเปล่า

$file = "STDOUT";
print $file "Hi!"; #ถ้าใส่ use strict จะแสดง error แต่ถ้าไม่ใช่จะแสดงคำว่า Hi! อย่างเดียว (ผิดเนื่องจากไม่ใส่ . เชื่อม)

perl auto-flush

การทำ auto-flush ใน perl ทำได้โดยการเซ็ตค่า $| เป็นหนึ่งซึ่งโดยปกติแล้วค่านี้จะมีค่าเป็น 0

#!c:\perl\bin\perl.exe

use CGI;

my $cgi = CGI->new;
print $cgi->header('text/html');

$|=1;

for ( 1..5 ) {
print "Value is $_";
sleep 1;
}

เมื่อทำการรัน จะเห็นข้อความขึ้นมาหนึ่งบรรทัดแล้วหยุดไปหนึ่งวิแล้วก็ค่อยมีบรรทัดใหม่ขึ้นมา

printf VS sprintf

ความแตกต่างของ printf กับ sprintf คือ sprintf มี null character (\0) ต่อท้าย

print ใน perl

#!c:\perl\bin\perl.exe

print "content-type:text/html\n\n";

#print ค่าของตัวแปร
print "Server $ENV{'SERVER_NAME'}\n\n";

#print ตัวอักษรด้วย (')
print 'We are running Perl version:';
print $^V."\n\n";

#print multiline
print < The perl binary : $^X
The perl script : $0
EOF

passing parametor to subroutines

การส่งค่า parametor ใน perl

รับเข้ามาเป็น array ใช้ @_
รับค่าแต่ละตัวตามลำดับ $_[1],$_[2],.....

การใช้ quote ใน perl

double qoute (") ค่าที่อยู่ใน doble qoute ถ้าเป็นตัวแปรจะแสดงค่าของตัวแปรนั้นออกมา ถ้าเป็นตัวอักษรพิเศษก็จะแสดงตัวอักษรพิเศษออกมา

single qoute (') ค่าที่อยู่ใน single qoute จะแสดงตามที่อยู่ข้างในทั้งหมด

black qoute (`) ค่าที่อยู่ในนี้คือคำสั่ง shell หรือ dos โดยจะ return ค่าผลลัพธ์ที่ได้ สามารถใช้ตัวแปรมารับค่าได้

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
print "Hello World\n"; #จะได้ Hello World
print 'Hello World\n'; #จะได้ Hello World\n
print `date`; #จะได้ The current date is: Wed 04/27/2011 Enter the new date: (mm-dd-yy)

ผลลัพธ์ใน browser
Hello World Hello World\nThe current date is: Wed 04/27/2011 Enter the new date: (mm-dd-yy)

ผลลัพธ์ใน view->source
Hello World
Hello World\nThe current date is: Wed 04/27/2011
Enter the new date: (mm-dd-yy)

my function ในภาษา perl

การใช้ my function กับตัวแปรใน perl คือการกำหนด variable scope ถ้าเราสร้างตัวแปรที่มีชื่อเหมือนกับ global variable ในฟังก์ชั่นโดยใช้ my ด้วย global variable จะไม่ถูกแก้ไข

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
$myString = "haha";
sub test
{
my $myString = "test";
}
print $myString."->01";
&test();
print $myString."->02";

ผลลัพธ์
haha->01
haha->02

###############################################

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
$myString = "haha";
sub test
{
$myString = "test";
}
print $myString."->01";
&test();
print $myString."->02";

ผลลัพธ์
haha->01
test->02

###############################################

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
my $myString = "haha";
sub test {
$myString = "test";
}
print $myString. "->01";
&test();
print $myString. "->02";

ผลลัพธ์
haha->01
test->02

###############################################

#!c:\perl\bin\perl
print "content-type:text/html \n\n";
my $myString = "haha";
sub test {
my $myString = "test";
}
print $myString. "->01";
&test();
print $myString. "->02";

ผลลัพธ์
haha->01
haha->02