14 June 2011
Change Computer Name On Ubuntu Desktop
Step 1. run gksudo gedit /etc/hostname /etc/hosts
Step 2. Change your computer name in two files.
Step 3. Restart.
02 May 2011
HTML::Template 2
<tmpl_if> กำหนดเงื่อนไขใน template
if.pl
#!c:\perl\bin\perl.exe use HTML::Template; my $tmpl = HTML::Template->new(filename=>'tmpl/if.tmpl'); $tmpl->param(bool=>1); print "Content-type:text/html\n\n",$tmpl->output();
if.tmpl
<html> <head> <title>if</title> </head> <body> <tmpl_if name="bool"> is true <tmpl_else> is false </tmpl_if> </body> </html>
<tmpl_unless> ใช้งานเหมือนกับ <tmpl_if> แต่เป็นเงื่อนไขที่ตรงกันข้าม(opposit)
HTML::Template
ไฟล์ที่เป็น template นั้นจะใส่นามสกุลไว้ว่า .tmpl โดยต้องใช้ HTML::Template โหลดขึ้นมา
<tmpl_var> เป็นการส่งค่ามาแสดงใน template
test.tmpl
test.pl
<tmpl_loop> ทำลูปใน template
test.tmpl
<tmpl_include> ดึงเอา template อื่นมา
include.pl
include1.tmpl
include2.tmpl
<tmpl_var> เป็นการส่งค่ามาแสดงใน template
test.tmpl
<html>
<head>
<title>Test Template</title>
</head>
<body>
My Home Directory is <tmpl_var name=home>
<br />
My Path is set to <tmpl_var name=path>
</body>
</html>
test.pl
#!c:\perl\bin\perl.exe
use HTML::Template;
use CGI::Carp qw(fatalsToBrowser);
#open html template
my $tmpl = HTML::Template->new(filename => 'tmpl/index.tmpl');
#fill parametors
$tmpl->param(home => $ENV{HOME});
$tmpl->param(path => $ENV{PATH});
#send
print "Content-Type:text/html\n\n",$tmpl->output();
<tmpl_loop> ทำลูปใน template
test.tmpl
<html>test.pl
<head>
<title>Loop Template</title>
</head>
<body>
<tmpl_loop name=emp>
<tmpl_var name=name> -- <tmpl_var name=job><br>
</tmpl_loop>
<tmpl_loop name=loop2>
<tmpl_var name=var1> -- <tmpl_var name=var2><br>
</tmpl_loop>
</body>
</html>
#!/perl/bin/perl.exe
use HTML::Template;
use CGI::Carp qw(fatalsToBrowser);
my $tmpl = HTML::Template->new(filename=>'tmpl/loop.tmpl');
$tmpl->param(emp => [{name=>'test1',job=>'unknow'},{name=>'test2',job=>'programer'}]);
my @var1 = qw(who am i);
my @var2 = qw(set to me);
my @loop_data = ();
while(@var1 and @var2){
my %row_data;
$row_data{var1} = shift @var1;
$row_data{var2} = shift @var2;
push(@loop_data,\%row_data);
}
$tmpl->param(loop2=>\@loop_data);
print "Content-Type:text/html\n\n";
print $tmpl->output();
<tmpl_include> ดึงเอา template อื่นมา
include.pl
#!/perl/bin/perl
use HTML::Template;
use CGI::Carp qw(fatalsToBrowser);
my $tmpl = HTML::Template->new(filename=>'tmpl/include1.tmpl');
print "Content-Type:text/html\n\n",$tmpl->output();
include1.tmpl
<html>
<head>
<title>include</title>
</head>
<body>
from include1.tmpl<br>
<tmpl_include name="include2.tmpl">
include2.tmpl
from include2.tmpl
</body>
</html>
01 May 2011
Compiler Directive Module
ใจการสร้าง module ขึ้นมานั้นเราสามารถเลือกเฉพาะบาง subroutine หรือตัวแปร ให้ทำงานได้โดยใช้การทำ Compiler Directive ซึ่งโครงสร้างคร่าวๆจะเป็น
##############################
package modulename;
use Exporter;
#@ISA จะทำหน้าที่เก็บโมดูลที่ inheritance ถ้าไม่เจอใน package จะทำการค้นหาจาก ตัวแปรนี้
@ISA = qw(Exporter);
#เป็น default load เมื่อเรียกใช้โมดูล subroutine ใน @EXPORT จะสามารถทำงานได้
@EXPORT = qw(function $variable @arrayVar %hashVAr);
#ถ้าต้องการใช้ต้องส่งพารามิเตอร์ ตอนโหลดโมดูล
@EXPORT_OK = qw(functionTwo $var);
#เหมือน @EXPORT_OK แต่ทำเป็นกลุ่มเอาไว้
%EXPORT_TAGS = qw('name'=>[qw(function functionTwo)]);
##############################
ตัวอย่าง
package DModule;
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(fnc1);
@EXPORT_OK = qw(fnc2);
%EXPORT_TAGS = ('all'=>[qw(fnc1 fnc2)]);
sub fnc1
{
print "fnc1 print
";
}
sub fnc2
{
print "fnc2 print
";
}
1;
วิธีการเรียกใช้
#!c:\perl\bin\perl.exe
print "content-type:text/html\n\n";
use DModule qw(:all !fnc2); #โหลดฟังก์ชั่นจาก %EXPORT_TAGS ยกเว้น fnc2
fnc1();
fnc2(); #ฟังก์ชั่นนี้จะไม่ทำงาน
การโหลดแบบอื่น
:DEFAUL เป็นค่าที่บอกว่าให้โหลดจาก @EXPORT
use DModule qw(:DEFAULT fnc2);fnc1 และ fnc2 สามารถถูกเรียกใช้ได้
อ้างอิง
http://docstore.mik.ua/orelly/perl/prog3/ch12_05.htm
http://world.std.com/~swmcd/steven/perl/module_anatomy.html
http://perldoc.perl.org/Exporter.html
30 April 2011
GET/POST Method in Perl
module สำหรับ ใช้ get กับ post แยก subroutine กันนะครับ
DataParam.pm
package DataParam;
sub new
{
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}
sub GetParam
{
my $self = shift;
my $params = $ENV{'QUERY_STRING'};
my $data = {};
foreach (split(/&/,$params))
{
my ($key,$value) = split(/=/,$_);
#### บรรทัดล่างเอาไว้แปลงค่าอักษรที่เป็น % นำหน้า
$value =~ s/%(..)/chr(hex($1))/ge;
$data->{$key} = $value;
}
return $data;
}
sub PostParam
{
my $self = shift;
my $params;
read(STDIN,$params,$ENV{'CONTENT_LENGTH'});
my $data = {};
foreach (split(/&/,$params))
{
my ($key,$value) = split(/=/,$_);
#### บรรทัดล่างเอาไว้แปลงค่าอักษรที่เป็น % นำหน้า
$value =~ s/%(..)/chr(hex($1))/ge;
$data->{$key} = $value;
}
return $data;
}
1;
วิธีการใช้งาน
index.pl
#!c:\perl\bin\perl.exe
use strict;
use CGI;
use DataParam;
my $param = DataParam->new();
my $GET = $param->GetParam();
my $POST = $param->PostParam();
my $cgi = CGI->new();
print $cgi->header("text/html");
################## xxx กับ yyy คือชื่อตัวแปรที่ส่งมาครับ
print $POST->{'xxx'};
print $GET->{'yyy'};
29 April 2011
create Perl Module
perl module จะมีนามสกลุเป็น .pm โดยด้านบนต้องระบบ package เอาไว้
Test.pm
package Test;
sub new{
$class = shift;
$self = {};
bless($self,$class);
return $self;
}
sub TestFunction{
my $self = shift;
return "hello my module";
}
new subroutine นั้นเป็นคอนสตัคเตอ ของโมดูลต้องประกาศไว้ด้วย
ต่อจากคอนสตัคเตอร์ก็เป็นฟังก์ชันของเรา เวลาเรียกใช้งานก็ใช้ use ตามปกติ
myfile.pl
#!c:\perl\bin\perl.exe
use Test;
print "content-type:text/html\n\n";
$data = Test->new();
print $data->TestFunction();
Subscribe to:
Posts (Atom)


