PHP でも大差無いんですが、Perl の場合ヒアドキュメントが確実なようなので get_data 関数の機能は、ファイルの中身を変数へセットするのですが、 その時点でファイルの中にある $ で始まる文字列を変数としてパースします。 要するに eval です。 html.cgi 拡張子:txtvbswsfjsphpjavahtmlutf8sjis #!/usr/local/bin/perl print "Content-type: text/html\n\n"; $head_height = "150"; $head_inner = "<b>ヘッド部分</b>"; &get_data( "head_unit", "./head.htm" ); $main_width = "950"; &get_data( "main_html", "./disp.htm" ); print $main_html; #------------------------------------------------- # 埋め込み用データ取得 #------------------------------------------------- sub get_data { local($var_name,$file_path) = @_; local($buffer) = "\$$var_name = <<\"HTML_DATA\";\n"; open(IN,$file_path); while (<IN>) { $buffer .= $_; } close(IN); $buffer .= "HTML_DATA"; eval( $buffer ); } disp.htm 拡張子:txtvbswsfjsphpjavahtmlutf8sjis <HTML> <HEAD> <META http-equiv="Content-type" content="text/html; charset=EUC-JP"> <TITLE>HTML テンプレート</TITLE> </HEAD> <BODY id="body" style='margin-left:0px;margin-top:0px;'> <DIV id="main" style=' width: $main_width px; background-color: #A0A0A0; ' > $head_unit $body_unit $foot_unit </DIV> </BODY> </HTML> head.htm 拡張子:txtvbswsfjsphpjavahtmlutf8sjis <DIV style=' width: 100%; background-color: #A3A3A3; height: $head_heigh px; ' > $head_inner </DIV>
#!/usr/local/bin/perl print "Content-type: text/html\n\n"; $head_height = "150"; $head_inner = "<b>ヘッド部分</b>"; &get_data( "head_unit", "./head.htm" ); $main_width = "950"; &get_data( "main_html", "./disp.htm" ); print $main_html; #------------------------------------------------- # 埋め込み用データ取得 #------------------------------------------------- sub get_data { local($var_name,$file_path) = @_; local($buffer) = "\$$var_name = <<\"HTML_DATA\";\n"; open(IN,$file_path); while (<IN>) { $buffer .= $_; } close(IN); $buffer .= "HTML_DATA"; eval( $buffer ); }
<HTML> <HEAD> <META http-equiv="Content-type" content="text/html; charset=EUC-JP"> <TITLE>HTML テンプレート</TITLE> </HEAD> <BODY id="body" style='margin-left:0px;margin-top:0px;'> <DIV id="main" style=' width: $main_width px; background-color: #A0A0A0; ' > $head_unit $body_unit $foot_unit </DIV> </BODY> </HTML>
<DIV style=' width: 100%; background-color: #A3A3A3; height: $head_heigh px; ' > $head_inner </DIV>
拡張子:txtvbswsfjsphpjavahtmlutf8sjis sub urlencode { my($str) = @_; $str =~ s/(\W)/'%'.unpack("H2", $1)/ego; $str =~ tr/ /+/; return $str; } 拡張子:txtvbswsfjsphpjavahtmlutf8sjis #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Session; my $session = new CGI::Session("driver:File", undef, {Directory=>'/tmp'}); # 有効なセッションIDを取得 print $session->id();
sub urlencode { my($str) = @_; $str =~ s/(\W)/'%'.unpack("H2", $1)/ego; $str =~ tr/ /+/; return $str; }
#!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Session; my $session = new CGI::Session("driver:File", undef, {Directory=>'/tmp'}); # 有効なセッションIDを取得 print $session->id();