●メニューへ ●単福へ ●自動車の維持費をシミュレーション ●InDesign JavaScript ノート

cgi/環境変数出力


#!/usr/bin/perl

print "Content-type:text/html;charset:Shift_JIS\n\n";
print "<html><body>\n";
print "<table>\n";

foreach (keys %ENV)
{
	print "<tr><td>$_ </td><td> $ENV{$_}</td></tr>";
}

print "</table>\n";
print "</html>\n";

exit;


foreach (keys %ENV)
{
	print "$_ : $ENV{$_}\n";
}


TOPへ


file/アクセスカウンター


open(DATA,"+< access.txt");#読み書きモード
@data=<DATA>;
$data[0]++;
print $data[0];
seek(DATA,0,0);#ファイルカーソル移動
print DATA $data[0];
close (DATA);

access.txtには下記のように数字を入れておく
100018


TOPへ


file/読み書き


open(IN,"perl01.cgi");
while(<IN>)
{
	print "$. :$_";#$.は現在の行数
}
close(IN);

#open (F,"< filename.txt")読み込み
#open (F,"> filename.txt")書き込み新規文章で書き込む
#open (F,">> filename.txt")追加書き込み。一番後ろに書き込む
#open (F,"+< filename.txt")読み書き


open (OUT,"> $outputfile");
print (OUT $myStr);
close(OUT);


TOPへ


file/読み書き2


$inputfile="サンプルテキスト.txt";
$outputfile="サンプルテキストout.txt";
open(IN, $inputfile);
@ARY = <IN>;
close(IN);

$myStr = join("", @ARY);

open (OUT,"> $outputfile");#書き込み新規文章で書き込む
print (OUT $myStr);
close(OUT);

exit;

#単にファイルをコピーするだけ


TOPへ


file/配列に読み込み


inputfile="./サンプルテキスト.txt";
open(IN, inputfile);
@ARY = <IN>;
foreach $buf (@ARY) {
	print $buf;
}
close(IN);


配列読み込みで連結は下記

$inputfile="サンプルテキスト.txt";
open(IN, $inputfile);
@ARY = <IN>;
close(IN);

$myStr = join("", @ARY);
print $myStr;

exit;


TOPへ


ダウンロード/MacJPerl五


http://world.std.com/~habilis/MacJP5.j.htm


TOPへ


ダウンロード/Win Perl


http://www.activestate.net/

トップのメニューから
Support&RESOUCES



Downloadsをクリック

ftpに入るので
ftp://ftp.activestate.com/ActivePerl/Windows/5.8/で
ActivePerl-5.8.7.815-MSWin32-x86-211909.msi
などmsi形式のをダウンロード


インストール終了後再起動



httpd.confを書き換える

ScriptAlias /cgi-bin/ "ローカルパス"

<Directory "ローカルパス">

ローカルパスは、パソコンの実際のパス。
指定したフォルダ以下でCGIが動作。
標準のままなら、
C:\Program Files\Apache Group\Apache\cgi-bin の中のファイルが
http://ホストネーム/cgi-bin/〜 で実行できる。

AddHandler cgi-script .cgi .pl

DirectoryIndex index.html index.cgi

下記2行を追加
AddType application/x-httpd-cgi .cgi
AddType application/x-httpd-cgi .pl


アパッチ再起動
設定の確認

Apacheの「C:\Program Files\Apache Group\Apache\cgi-bin」
フォルダの「printenv.pl」というPerlファイル
をテキストエディタで開いて
一行目で、Perlのパスを指定。
ActivePerl を標準で C:\Perl\ にインストールした場合、

#!C:/Perl/bin/perl


TOPへ


制御構文/for


for($i=0;$i<10;$i++)
{
	print "$i\n";
}
for(0..9)#0〜9まで範囲指定
{
	print "$_\n";
}

@tmp=(0,1,2,3,4,5,6,7,8,9);
for(@tmp)
{
	print "$_\n";
}
exit


TOPへ


制御構文/if


if(1){
	print "test\n\n";
}
elsif($a<0)
{
	print "-\n\n";
}
else
{
	print "error\n\n";
}
print "test2\n\n" if(1);
$a == 0 ? print "test":print "error";
exit;


TOPへ


制御構文/while


$i=0;
while(1)
{
	#if ( $a == 0 ){ last; }#抜ける
	#if ( $a == 0 ){ next; }#次のループへ
	print "test";
}
continue#while文のみ。絶対毎回実行される。
{
	$i++;
}


TOPへ


正規表現/パターン


.	改行以外のすべての文字
[a-zA-Z0-9]	セット内の1文字
[^a-zA-Z0-9]	セット以外の1文字
\d	数字
\D	数字以外
\w	英数字
\W	英数字以外
\s	スペース・tab・改行
\S	スペース・tab・改行以外
\n	改行chr(10)
\r	復帰chr(13)
\t	tab
\f	改ページ
\b	バックスペース
\0	ヌル文字

?	0または1個
*	0個以上
+	1個以上
{m,n}	m個以上n個以下

abc	abcにHIT
a|b|c	aかbかcにHIT

$	行の最後
^	行の先頭


TOPへ


正規表現/演算子


■変換演算子
tr/対象リスト/変換リスト/
$S =~ tr/abc/ABC/;	#小文字→大文字
$S =~ tr/a-z/A-Z/;	#小文字→大文字

オプション
c	対象リストに含まれないものを変換
d	変換リストに含まれないものを削除
s	連続文字を1文字に変換



=	代入
~	2の補数(ビットを反転)
=~	右側を実行して左に代入

■マッチパターン演算子
s/マッチパターン/置換文字列/
$S =~ s/abc/123/;	abcを123へ

オプション
e	置換文字列を式とする
g	全部置き換え
i	大文字小文字の区別なし
s	単一行マッチ
m	複数行マッチ
o	パターンコンパイル
x	空白を無視 #をコメント処理

■マッチ演算子
m/マッチパターン/	#mは省略可能。
$str =~ m!/!;		#このように"/"を検索するようなときは区切り文字を変更できる。ただしmは省略できない。


TOPへ


正規表現/検索置換1


$inputfile="サンプルテキスト.txt";
$outputfile="サンプルテキストout.txt";
open(IN, $inputfile);
@ARY = <IN>;
close(IN);

$myStr = join("
", @ARY);
$myStr =~ s/(if.+then)/<font color="red">$1<\/font>/;	#小文字→大文字
open (OUT,"> $outputfile");#書き込み新規文章で書き込む
print (OUT $myStr);
close(OUT);

exit;


TOPへ


正規表現/検索置換2


$inputfile="サンプルテキスト.txt";
$outputfile="サンプルテキストout.txt";
open(IN, $inputfile);
@ARY = <IN>;
close(IN);

$myStr = join("
", @ARY);
$myStr =~ s/(if)(.+)=(.+)(then)/<font color="red">$1<\/font><b>$2<\/b>=<b>$3<\/b><font color="red">$4<\/font>/;	#小文字→大文字
open (OUT,"> $outputfile");#書き込み新規文章で書き込む
print (OUT $myStr);
close(OUT);

exit;


TOPへ


正規表現/抜き出す


""ではさまれた文字をぬきだす。

$inputfile="サンプルテキスト.txt";
$outputfile="サンプルテキストout.txt";
open(IN, $inputfile);
@ARY = <IN>;
close(IN);

#まずは""を含む配列を取り出す。
@ARY2 = grep(/".+"/, @ARY);
#@ARY2 =~ m/(".+")/;#こんな風に一発で取り出せると思うのだが。。。

#つぎに取り出した配列の中から""のテキストだけとりだす。
foreach $buf (@ARY2) {
	$buf  =~ m/"(.+)"/g;
	print $1;
	$wStr.="$1\n";
}

open (OUT,"> $outputfile");#書き込み新規文章で書き込む
print (OUT $wStr);
close(OUT);

exit;


TOPへ


正規表現/変換


$inputfile="サンプルテキスト.txt";
$outputfile="サンプルテキストout.txt";
open(IN, $inputfile);
@ARY = <IN>;
close(IN);

$myStr = join("", @ARY);
$myStr =~ tr/a-z/A-Z/;	#小文字→大文字
open (OUT,"> $outputfile");#書き込み新規文章で書き込む
print (OUT $myStr);
close(OUT);

exit;


TOPへ


設定/MacOSX


デフォルトでperlは入っているので動くように設定する。

/etc/httpd/httpd.conf
を開き書き換える

#LoadModule perl_module libexec/httpd/libperl.so
先頭の「#」を削除します。

#AddModule mod_perl.c
先頭の「#」を削除します。

ScriptAlias /cgi-bin/ "/Library/WebServer/CGI-Executables/"
この行のすこし下

<Directory "/Library/WebServer/CGI-Executables">
AllowOverride None
Options Indexes MultiViews ExecCGI FollowSymLinks Includes
#Options None
Order allow,deny
Allow from all
</Directory>
と書き換える

#AddHandler cgi-script .cgi 
先頭の「#」を削除します。



これでパーソナルWEB共有を再起動すれば動く。

作成したcgiは
/Library/WebServer/CGI-Executables/
に入れる。

ブラウザからは
http://localhost/cgi-bin/test.cgi
で呼び出す


TOPへ


文字列/chomp


$str="abcdefg";

chop($str);#1バイト削る
chomp($str);#改行を1けずる

print $str;


TOPへ


文字列/cmp


$a="aaa";
$b="aaA";
print $a cmp $b;
exit;


TOPへ