|  | 
   
    |  | 
บทที่1:เรียนLinux บทที่3:เรียนPerl 
บทที่4:บทเรียนPHP บทที่5:บทเรียนSQL 
บทที่8:หน้าที่ของแฟ้ม/ระบบDirectory 
บทที่9:คำสั่งบริหารระบบSuperUser 
เริ่มต้นกับ 
  Perl
บทที่ 3 : บทเรียน PERL (Perl programming) 
  3.1 เริ่มต้นกับ perl 3.2 Perl สำหรับสร้างแฟ้ม 
  3.3 Perl สำหรับแสดงข้อมูลจากแฟ้ม 3.4 Perl สำหรับเพิ่มระเบียนใหม่ 
  3.5 Perl สำหรับลบระเบียน 3.6 Perl เรียกตัวเองมาแสดงผล 
  
 
  -  3.1 
    เริ่มต้นกับ perl 
 :::::: เมื่อเขียน perl สักโปรแกรมหนึ่ง แล้วใช้ ftp upload เข้าไปใน home directory 
    ของท่าน ก็ถือว่าผ่านจุดแรกแล้ว
      ตัวอย่างโปรแกรม perl ที่ใช้ในการทดสอบ 
      3 บรรทัด #!/usr/bin/perl
 print"Content-type:text/html\n\n";
 print"<body bgcolor=#ffffdd><h1>This is test.</h1><br>By 
      tester</body>";
 
 
      วิธีการทำให้ perl ใช้งานได้ 
       
      - Host name ที่ท่านจะใช้อ้างอิงในการ 
        upload คือ www.isinthai.com  
      
- ก่อน upload ต้องให้แน่ใจว่า โปรแกรมของท่านนามสกุล 
        pl  
      
- ก่อน upload ต้องให้แน่ใจว่ากำหนด 
        mode การส่งแบบ ascii ไม่ใช่ bin เพราะถ้าเป็น binary จะใช้งานไม่ได้ 
         
      
- หลัง upload แล้วให้ใช้ ftp ตัวใหม่ 
        chmod เป็น 755 เช่น chmod 755 f.pl เป็นต้น  
      
- ตอนเรียกใช้ให้เรียกทาง url ดังนี้ 
        http://www.isinthai.com/uname/f.pl  
      
- ถ้าเรียกใช้แบบ http://www.isinthai.com/uname/f.pl 
        จะแสดง source code ออกมาตรง ๆ ไม่ผ่านการประมวลผล  
      
- ควรทราบว่า Home directory อยู่ที่ 
        /home/httpd/html/uname  
    
 
 3.2 Perl สำหรับสร้างแฟ้ม
 :::::: โปรแกรมเล็ก ๆที่ใช้ในการสร้างแฟ้ม ตัวอย่างนี้มี 6 ระเบียน 5 เขตข้อมูล 
     
    
      Html ที่ใช้เรียก perl มาทำงาน 
      <form action=http://www.isinthai.com/thaiall/perlcrt.pl 
      method=post>
 <input type=submit value=Create_dataperl.txt> perlcrt.pl
 </form>
 หรือ จะเรียกตรง ๆ ผ่าน URL ก็ได้เช่น http://www.isinthai.com/thaiall/perlcrt.pl
      ตัวอย่างโปรแกรม perl  
      #!/usr/bin/perl
 print"Content-type:text/html\n\n";
 # $filename = "/data1/hm/thaiall.com/isinthai/dataperl.txt";
 $filename = "/home/httpd/html/thaiall/dataperl.txt";
 open(myfile,">$filename");
 $dat = "3101001:Tom:Jojo:16000:A:\n";
 print myfile $dat;
 $dat = "3101002:Naja:Rujj:5600:R:\n";
 print myfile $dat;
 $dat = "3101003:Packman:Somsu:7000:L:\n";
 print myfile $dat;
 $dat = "3101004:Nichole:Jaiboon:15000:A:\n";
 print myfile $dat;
 $dat = "3101005:Suwi:Hancharn:10600:A:\n";
 print myfile $dat;
 $dat = "3101006:Nirund:Jiva:9700:A:\n";
 print myfile $dat;
 close(myfile);
 print "<body bgcolor=#ddffff>Create 6 Records and 5 Fields : Finish";
 
 
home 
  -  
    
 3.3 Perl สำหรับแสดงข้อมูลจากแฟ้ม
 :::::: โปรแกรมเล็ก ๆ ที่อ่านข้อมูลจากแฟ้มมาแสดง
 Html ที่ใช้เรียก perl มาทำงาน 
      <form action=http://www.isinthai.com/thaiall/perllst.pl 
      method=post>
 <input type=submit value=List_dataperl.txt> perllst.pl
 </form>
 หรือ จะเรียกตรง ๆ ผ่าน URL ก็ได้เช่น http://www.isinthai.com/thaiall/perllst.pl
      ตัวอย่างโปรแกรม perl  
      #!/usr/bin/perl
 print"Content-type:text/html\n\n";
 # $filename = "/data1/hm/thaiall.com/isinthai/dataperl.txt";
 $filename = "/home/httpd/html/thaiall/dataperl.txt";
 print "<body bgcolor=#ddffff><pre>";
 open(myfile,"<$filename");
 @getrec = <myfile>;
 close(myfile);
 $i = 1;
 foreach $rec (@getrec) {
 @r = split(/:/,$rec);
 print $i,$r[0]," - ",$r[1],$r[2],$r[3],$r[4],$r[5],"\n";
 $i++;
 }
 
 
home 
  -  
    
 3.4 Perl สำหรับเพิ่มระเบียนใหม่
 :::::: โปรแกรมเล็ก ๆ เพิ่มระเบียนใหม่อย่างง่าย ๆ ทีละระเบียน
 Html ที่ใช้เรียก perl มาทำงาน 
      <form action=http://www.isinthai.com/thaiall/perladd.pl 
      method=post>Add new<br>
 s-id : <input type=text name=sid><br>
 s-name : <input type=text name=sn><br>
 s-surname : <input type=text name=ss><br>
 s-salary : <input type=text name=ssal><br>
 status: <input type=text name=stat><br>
 <input type=submit value=Add_dataperl.txt>
 </form>
 
      ตัวอย่างโปรแกรม perl  
      #!/usr/bin/perl
 &parse_form;
 print"Content-type:text/html\n\n";
 # $filename = "/data1/hm/thaiall.com/isinthai/dataperl.txt";
 $filename = "/home/httpd/html/thaiall/dataperl.txt";
 open(myfile,">>$filename");
 $dat = "$config{'sid'}:$config{'sn'}:$config{'ss'}:$config{'ssal'}:$config{'stat'}:\n";
 print myfile $dat;
 close(myfile);
 print "Add new : Finish";
 exit;
 
 sub parse_form {
 if ($ENV{'REQUEST_METHOD'} eq 'GET') {
 @pairs = split(/&/, $ENV{'QUERY_STRING'});
 } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 @pairs = split(/&/, $buffer);
 }
 foreach $pair (@pairs) {
 local($name, $value) = split(/=/, $pair);
 $name =~ tr/+/ /;
 $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ tr/+/ /;
 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ s/<!--(.|\n)*-->//g;
 $config{$name} = $value;
 }
 }
 
 
home 
  -  
    
 3.5 Perl สำหรับลบระเบียน
 :::::: โปรแกรมเล็ก ๆ ที่ใช้ลบระเบียนตาม key field ที่ป้อนเข้าไป
 Html ที่ใช้เรียก perl มาทำงาน 
      <form action=http://www.isinthai.com/thaiall/perldel.pl 
      method=post>Delete record on
 s-id : <input type=text name=sid> <input type=submit value=Del_dataperl.txt>
 </form>
 
      ตัวอย่างโปรแกรม perl  
      #!/usr/bin/perl
 &parse_form;
 print"Content-type:text/html\n\n";
 # $filename = "/data1/hm/thaiall.com/isinthai/dataperl.txt";
 $filename = "/home/httpd/html/thaiall/dataperl.txt";
 print "<body bgcolor=#ddffff><pre>";
 open(myfile,"<$filename");
 @getrec = <myfile>;
 close(myfile);
 $i = 1;
 open(myfile,">$filename");
 foreach $rec (@getrec) {
 @r = split(/:/,$rec);
 if ($r[0] ne $config{'sid'}) {
 print myfile $rec;
 }
 }
 close(myfile);
 print "Delete : Finish";
 exit;
 
 sub parse_form {
 if ($ENV{'REQUEST_METHOD'} eq 'GET') {
 @pairs = split(/&/, $ENV{'QUERY_STRING'});
 } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 @pairs = split(/&/, $buffer);
 }
 foreach $pair (@pairs) {
 local($name, $value) = split(/=/, $pair);
 $name =~ tr/+/ /;
 $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ tr/+/ /;
 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 $value =~ s/<!--(.|\n)*-->//g;
 $config{$name} = $value;
 }
 }
 
 
 3.6 Perl เรียกตัวเองมาแสดงผล
 :::::: โปรแกรมนี้เป็น ภาษา perl ซึ่งทดสอบให้ ประมวลผลได้ใน linux เครื่อง isinthai.com 
    นี้
 
- โปรแกรมนี้ชื่อ testf.pl ถูกเก็บไว้ในห้อง 
    /home/httpd/html/thaiall และ link ไปยังห้อง /home/httpd/html/thaiall ซึ่งต้องกำหนดให้ 
    chmod 700 เพื่อให้สั่งประมวลผล shell script ตัวนี้ได้ การเรียก Perl script 
    ตัวนี้ ให้ทำตามข้างล่างนี้ >> http://www.isinthai.com/thaiall/testf.pl 
     
    
      ตัวอย่างโปรแกรมที่ใช้ทดสอบ
 #!/usr/bin/perl
 $filename = "/home/httpd/html/thaiall/testf.pl";
 print"Content-type:text/html\n\n";
 print"<body bgcolor=#ffffdd><pre>";
 open(myfile,"$filename");
 @getrec = <myfile>
 close(myfile);
 $i = 1;
 foreach $r (@getrec) {
 $r =~ s/</</g;
 $r =~ s/>/>/g;
 $r =~ s/"/"/g;
 print $i,". ",$r,"<br>";
 $i++;
 }
 print"<hr>";
 
 
home