require 'jcode.pl';
# post or get if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $str, $ENV{'CONTENT_LENGTH'}); } else { $str = $ENV{'QUERY_STRING'}; }
# common CGI send data decode routine @part = split('&', $str); foreach $i (@part) { ($variable, $value) = split('=', $i); $value =~ tr/+/ /; $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg; &jcode'convert(*value, 'euc'); $value =~ s/</g; $value =~ s/>/>/g; $value =~ s/\r\n/\n/g; $value =~ s/\r/\n/g; $data{$variable} = $value; } # write data to file open(OUTFILE, ">>$outfile") || die "cannot write $outfile"; print OUTFILE "$data{'header'},$data{'key'},$data{'contents'}\n"; close(OUTFILE);
sub name-of-subroutine { サブルーチン内処理; }サブルーチンの呼び出しは、&を付けて次のように行う。
&name-of-subroutine;ローカル変数はキーワードlocalを付けて次のように定義する。
sub name-of-subroutine { local $local-data; サブルーチン内処理; }
# call &name-of-subroutine($data1, $data2, $data3); sub name-of-subroutine { local($arg1, $arg2, $arg3) = @_; print $arg1, $arg2, $arg3, "\n"; }
sub sort_lines_of_file { local($file) = @_; open(DATA, "$file") || die "cannot open $file"; $i = 0; while ($line = ) { $array[$i] = $line; $i++; } close(DATA); return (@array); }
# send html header sub send_html_header { print <<EOF; Content-type: text/html <html> <head> <title>Links</title> </head> <body> EOF }
# directory access mode must be 777 $lock ="lock/lock"; # lock on sub lock_on { $wait = 0; while (-f "$lock") { if ($wait++ >= 30) { unlink($lock); exit(0); } sleep(1); } open(LOCK, ">$lock"); close(LOCK); } # lock off sub lock_off { unlink("$lock"); }
# call &write_lines_to_file($datafile, @data); # write lines sub write_lines_to_file { local($file, @array) = @_; open(DATA, ">$file") || die "cannot open $file"; $i = 0; while ($array[$i]) { print DATA $array[$i]; $i++; } close(DATA); }
# call @data = &get_array_of_file($datafile); @new = sort(@data); # get array of lines in the file sub get_array_of_file { local($file) = @_; open(DATA, "$file") || die "cannot open $file"; $i = 0; while ($line = ) { $array[$i] = $line; $i++; } close(DATA); return (@array); }