Perl CGI 1. HTML sample1.html <HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY> </BODY> </HTML> WWW (^^) <H1> </H1> <H1> <H6> <H1> <P> </P> (Paragraph) <BR> (Line brake) <FONT size= number > </FONT> number 1 7 3 <FONT color= color > </FONT> (Black,White,Red,Blue,Green,Yellow,Gray ) <BODY text= color > <BODY text= Red > <BODY bgcolor= color > <BODY bgcolor= Aqua > <A href= www.fukui-nct.ac.jp > </A> <IMG src= sample.jpg alt= >
. index.html <HTML> <BODY> <H1> </H1> <HR> <!--#exec cmd="./cgi-bin/count.cgi"--> </BODY> </HTML> /cgi-bin/count.cgi open(fh,"./cgi-bin/count.dat"); $cnt=<fh>; close(fh); $cnt++; print"<center> ${cnt </CENTER>"; open(fh,">./cgi-bin/count.dat"); print FH $cnt; close(fh); END.htaccess Options +Includes Options +ExecCGI AddHandler server-parsed html AddHandler cgi-script cgi
. Perl print ; ($ ) first.pl print " Perl!" DOS perl first.pl first2.pl # name $name = " "; print "$name n"; print " ${name n"; 3 hello.cgi # UNIX Perl # /usr/local/bin or /usr/local/bin/perl # which perl # print "Content-type: text/html n n"; # HTML # n # Content-type print "<HTML> n"; print " <BODY> n"; print " <H1> Hello!</H1> n"; print " </BODY> n"; print "</HTML> n"; perl hello.cgi > hello.html hello.html calc.pl # $x = 1;
$y = 2; $ans = $x + $y; print "$x $y $ans "; END if if ( ) { ; else { ; 1 ex_if.pl #if 1 $a=6; if ( $a > 5 ) { print " $a $a 5 "; else { print " $a $a 5 "; = =! = > > = < < = ex_if2.pl #if 2 #if 0 # 0 $a = 0; if ( $a ) { print " "; else {
print " "; for (@ ) ex_for.pl # 1-10 for ( $i=1 ; $i<=10 ; $i++ ) { print "$i"; print " n"; # *( ) for ( $i=1 ; $i<=10 ; $i++ ) { print "*"; print " n"; ex_for2.pl # @list = (" "," "," "); # print " $list[0] n"; $count = @list; print "$count n"; # # for ($i=0 ; $i<=$count-1 ; $i++) { print "$list[$i] n";
open (, ); close ( ); print ; $data = < > ex_file_write.pl # open( FH, ">test.dat"); print FH " n"; close(fh); # # # ex_file_write2.pl # open( FH, ">>test.dat"); # print FH " n"; # close(fh); # ex_file_read.pl # open( FH, "<./test.dat"); $str = <FH>; print "$str"; # # $str = <FH>; print "$str"; # close(fh); #
split @list = split( / /, $str ); ex_split.pl #split $str = "abc-def-ghi"; @list = split( /-/, $str); for ($i=0 ; $i < @list; $i++) { print "$list[$i] n"; ex_split2.pl #split $str = "abcdefg"; @list = split( //, $str); for ($i=0 ; $i < @list; $i++) { print "$list[$i] n"; foreach foreach $str ( @list ); ex_foreach.pl #foreach @list=("abc","abc","123"); foreach $str (@list) { print "$str n"; printf, sprintf printf (, ); $str = sprintf (, );
ex_printf.pl #printf,sprintf $number = 10; printf("%9d n",$number); printf("%09d n",$number); # 9 (10 ) # 0 $number = 123.456; printf("%9.2f n",$number); # 9, 2 $number = 123; $new_number = sprintf("%05d n",$number); print "$number $new_number n"; #
. GIF (CGI ) gif_counter.cgi # $DataFile="./count.dat"; #gif $dir_gif="./gif"; print "Content-type: text/html n n"; print "<HTML> n"; print " <BODY> n"; print " <H1> GIF </H1> n"; # if( open( FH, "<$DataFile" )) { $count = <FH>; close(fh); # #print "$count n"; # $count++; # $count = sprintf("%05d",$count); # foreach $number (split( //, $count )) { print "<IMG SRC= "${dir_gif /${number.gif ">"; print " n";
else { # if( open( FH, ">$DataFile" )) { print FH $count; close(fh); else { print " <BR> n"; print " <BR> n"; print " </BODY> n"; print "</HTML> n"; END
HTML HTML 1,780 HTML 1,500 CGI Perl Perl CGI 2,730 Perl CGI 3,000 Perl/CGI 2,200 http://www.hi-net.zaq.ne.jp/kobej/free/free_g0113.htm http://tadamono.to/homepage.html CGI SSI Hypertext Markup Language Web ( Common Gateway Interface) CGI CGI Yahoo! CGI Server Side Include CGI HTML ( ) CGI http://hide.maruo.co.jp/ Active Perl http://www.activestate.com/ Perl FFftp http://www2.biglobe.ne.jp/~sota/ Apache http://www.apache.jp/
/cgi-bin guest_book.html 644 guest_write.html 644 guest_write.cgi 755 guest_read.cgi 755 guest_book.dat 666 cgi-lib.pl 644 cgi
guest_book.html <HTML> <BODY> <H1><< >></H1> <H2><A HREF="guest_write.html"> </H2> <H2><A HREF="guest_read.cgi" > </H2> <BODY> </HTML> guest_write.html <HTML> <BODY> <H1> </H1> <FORM ACTION="guest_write.cgi" METHOD="POST"> <BR> <INPUT TYPE="text" SIZE=60 NAME="name"> <P> <BR> <TEXTAREA NAME="comment" cols=60 rows=8> </TEXTAREA> <P> <INPUT TYPE="submit" VALUE=" "> <INPUT TYPE="reset" VALUE=" "> </FORM> </BODY> </HTML>
guest_write.cgi # # $data_file = 'guest_book.dat'; # HTML print "Content-type: text/html n n"; # require 'cgi-lib.pl'; # %form &ReadParse(*form); # $name = $form{'name'; $comment = $form{'comment'; # #$name = ' '; #$comment = ' '; # if ($name eq '') { &print_error(" "); # if ($comment eq '') { &print_error(" "); #
$name =~ s/</</g; $comment =~ s/</</g; # if (!open(txt, "+<$data_file")) { &print_error(" "); # if (!&lock_file(txt)) { close(txt); &print_error(" "); # @txt = <TXT>; # seek(txt, 0, 0); # print TXT "<DL> n"; # print TXT "<DT> <DD>$name n"; # if ($comment ne '') { print TXT "<DT> <DD>$comment n"; # $datestr = &get_date_string; print TXT "<DT> <DD>$datestr n"; print TXT "</DL> n";
print TXT "<HR> n"; # # print TXT @txt; # truncate(txt, tell(txt)); # &unlock_file(txt); # close(txt); # &page_begin(" "); &page_end; # exit(0); # # # # # &print_error(" "); sub print_error { local($msg) = @_; &page_begin($msg); &page_end; exit(0);
# # &page_begin(" "); sub page_begin { local ($msg) = @_; print "<HTML> n"; print "<HEAD> n"; print "<TITLE>$msg</TITLE> n"; print "</HEAD> n"; print "$bodytag n"; print "<H1>$msg</H1> n"; # sub page_end { print "<HR> n"; print "<A HREF=guest_book.html> </A> n"; print "<HR> n"; print "</BODY> n"; print "</HTML> n"; # sub get_date_string { local($sec, $min, $hour, $day, $mon, $year); ( $sec, $min, $hour, $day, $mon, $year ) = localtime(time); $year += 1900; $mon++; # if ($hour < 10) { $hour = "0$hour"; if ($min < 10) { $min = "0$min";
if ($sec < 10) { $sec = "0$sec"; return "$year $mon $day $hour $min $sec "; # sub lock_file { local(*file) = @_; if ($uselock) { eval("flock(file, 2)"); # 2=LOCK_EX if ($@){ # flock return 0; return 1; # sub unlock_file { local(*file) = @_; if ($uselock) { eval("flock(file, 8)"); # 8=LOCK_UN
guest_read.cgi # # # $data_file = 'guest_book.dat'; # print "Content-type: text/html n n"; # if (!open(txt, "$data_file")) { &print_error(" "); else { &page_begin(" "); print "<HR> n"; # while (<TXT>) { print; &page_end; # close(txt); # exit(0); # # #
# # &print_error(" "); sub print_error { local($msg) = @_; &page_begin($msg); &page_end; exit(0); # # &page_begin(" "); sub page_begin { local ($msg) = @_; print "<HTML> n"; print "<HEAD> n"; print "<TITLE>$msg</TITLE> n"; print "</HEAD> n"; print "$bodytag n"; print "<H1>$msg</H1> n"; # sub page_end { print "<HR> n"; print "<A HREF=guest_book.html> </A> n"; print "<HR> n"; print "</BODY> n"; print "</HTML> n";