リメイク:ファイルに書き出す

                             <p>CGI::Prettyは、出力するHTMLソースをインデントをつけて整形して出力してくれる。<br />

それ以外は、CGI.pmと同じで、使い方も

1
use CGI;
1
use CGI::Pretty;
1
use encoding "euc-jp";
1
Wide character in print at ....
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl -T

use strict;
use warnings;
use Encode;
use encoding "euc-jp";

use CGI::Pretty;
use FileHandle;

{
    my $cgi    = CGI::Pretty->new;
    my $styles = [q{../../ipp.css}, q{../test.css}];
    my $wdays  = ["日", "月", "火", "水", "木", "金", "土"];

    my $output = "";

    $output .= &amp;print_header($cgi, "テスト3", $styles);

    $output .= $cgi->p("「temp.txt」に書き出します。");

    my $fh = FileHandle->new("./temp.txt", "w") or die;
    my $fh_output = "";
    $fh_output .= "この文は temp.txt の中身になるはずです。n";
    $fh_output .= "書き込んだ時間は、";

    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime;
    $fh_output .= sprintf("%04d/%02d/%02d(%s) %02d:%02d:%02dです。n", $year + 1900, $mon + 1, $mday, $wdays->[$wday], $hour, $min, $sec);
    $fh->print(Encode::encode("utf8", $fh_output));
    undef $fh;

    $output .= $cgi->div({-class => "test"}, $cgi->a({-href => "./temp.txt"}, "temp.txtを見る"));

    $output .= &amp;print_footer($cgi);

    print Encode::encode("utf8", $output);
}

exit;

# ヘッダ
sub print_header {
    my ($cgi, $title, $styles) = @_;

    my $result = "";
    $result .= $cgi->header({ -charset => "utf-8" });
    $result .= $cgi->start_html({
            -lang  => 'ja',
            -title => $title,
            -style => { src => $styles },
        });
    $result .= $cgi->div({-class => "head"},
            $cgi->h1($title),
            $cgi->hr,
            $cgi->a({-href => "../../../" }, "Home"),
            "/",
            $cgi->a({-href => "../../" }, "Perl"),
            "/",
            $cgi->a({-href => "../" }, "TestCGI Index"),
            $cgi->hr,
        );

    return $result;
}

# フッタ
sub print_footer {
    my ($cgi) = @_;

    my $fh = FileHandle->new("../../sig.txt") or die;
    my $sig = join "", $fh->getlines;
    undef $fh;

    my $result = "";
    $result .= $cgi->div({-class => "foot"},
            $cgi->hr,
            $cgi->a({-href => "../../../" }, "Home"),
            "/",
            $cgi->a({-href => "../../" }, "Perl"),
            "/",
            $cgi->a({-href => "../" }, "TestCGI Index"),
            $cgi->hr,
            $sig,
        );
    $result .= $cgi->end_html;

    return $result;
}
                          </div>
comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。