• ベストアンサー

perl言語を使ったHTMLファイルの保存について

はじめまして。perl初心者の者です。 皆様のお力をかしていただきたく参りました。 以下はあるプログラムの一部なのですが、ここへプログラムを追加し、 ・HTMLファイルを新規作成(ファイル名は毎回違うものをつける) ・5つあるdbg();の中身全てと、$self->{is_spam}の値を書き込む ・ファイルを閉じて/home/user/WWW/へ保存 という形に仕上げたいと思っています。 "#"が先頭にある部分は自分で考えたのですが、これではうまく動いてくれませんでした。間違いがある場所もわからず困っています。どなたかわかる方がいらっしゃいましたら、教えていただきたく存じます。文字数の関係で見づらく申し訳ありませんが、よろしくお願い致します。 #use File::Temp; #$file = mktemp("/home/user/WWW/XXXXXX.html"); # open(IN, "> $file"); $self->{score} = (sprintf "%0.3f", $self->{score}) + 0; dbg("check: is spam? score=".$self->{score}. " required=".$self->{conf}->{required_score}); # print IN "check: is spam? score=$self->{score}"; # print IN "required=$self->{conf}->{required_score}"; dbg("check: tests=".$self->get_names_of_tests_hit()); # print IN "check: tests=$self->get_names_of_tests_hit()"; dbg("check: subtests=".$self->get_names_of_subtests_hit()); # print IN "check: subtests=$self->get_names_of_subtests_hit()"; dbg("check: tests=".$self->get_names_of_tests_hit()); # print IN "check: tests=$self->get_names_of_tests_hit()"; dbg("check: subtests=".$self->get_names_of_subtests_hit()); # print IN "check: subtests=$self->get_names_of_subtests_hit()"; $self->{is_spam} = $self->is_spam(); # print IN "$self->is_spam()"; # close(IN);

  • Perl
  • 回答数2
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • sakusaker7
  • ベストアンサー率62% (800/1280)
回答No.2

二点ほど指摘します。 > #$file = mktemp("/home/user/WWW/XXXXXX.html"); 単に use File::Temp しただけではこの形式で mktempを呼び出せないのでは? それに mktemp に渡すのは一時ファイルの名前のテンプレートなので、 ディレクトリは指定できないんじゃないでしょうか? もうちょっと File::Temp の使い方を調べたほうが良いと思います。 perldoc File::Temp で十分でしょう。 > # open(IN, "> $file"); オープンに成功したかどうかはいつでもチェックすべきです。

kkakey
質問者

お礼

ご指摘ありがとうございます。 返信が遅くなってしまい大変申し訳ありません。 File::Tempをはじめ、perl言語について今後もっと勉強していきたいと思います。 とても参考になりました!ありがとうございました。

その他の回答 (1)

noname#39970
noname#39970
回答No.1

あえて考え方だけ いきなり全体を作るんじゃなく、1つずつ確実に動く物を作ったらどうだろう? 1.ファイルを作成するもの 2.1を利用して特定文字列(ここで欲を出さず"ABC"とかでやめる)を書き出すもの 3.HTML加工をするもの(ファイルじゃなく画面に出し正常なのを確認する) 4.2と3を組み合わせる

kkakey
質問者

お礼

早速の回答、ご意見ありがとうございます! 参考にさせていただき、挑戦してみたいと思います。 ありがとうございました。

関連するQ&A

  • PHPからHTMLファイルを書き出し

    PHPプログラムからHTMLファイル(aaa.html)を書き出しているのですが、 その作られたHTMLファイルにブラウザでアクセスすると以下のようなエラーメッセージが表示されます。 Warning: Unknown: SAFE MODE Restriction in effect. The script whose gid is 1000 is not allowed to access /****/aaa.html owned by uid 1000 in Unknown on line 0 Warning: Unknown: SAFE MODE Restriction in effect. The script whose uid is 1000 is not allowed to access /****/aaa.html owned by uid 1000 in Unknown on line 0 Warning: Unknown: failed to open stream: Success in Unknown on line 0 Fatal error: Unknown: Failed opening required '/****/aaa.html' (include_path='.:/usr/local/lib/php') in Unknown on line 0 HTMLファイル名はユニークなので、あらかじめサーバーにアップロードし用意しておくという事ができません。 PHPプログラムの所有者と作成されたHTMLファイルの所有者が異なっていました。 chownはサーバーの仕様で使うことができません。 HTMLファイルのパーミッションを777として書き出しても、変化はありませんでした。 またPHPはsafemodeです。こちらも仕様でoffにできません。 長くなってしまい申し訳ないです。 HTMLファイルを閲覧できる方法はありますでしょうか。 どうかお知恵をお借りしたく思います。

    • 締切済み
    • PHP
  • perlがうごきません

    htmlファイルには <html> <form action="form2.cgi" method="get"> 食品名1<input type="text"name="form1"> <br> 食品名2<input type="text"name="form2"> <input type="submit"value="送信"> </form> </html> cgiファイルには #! /usr/bin/perl require "cgi-lib.pl"; &ReadParse(); print "Content-type:text/html\n"; print "\n"; print "<html>\n"; print "<head>\n"; print "<title>フォーム</title>\n"; print "</head>\n"; print "<body>\n"; print "おいしい$in{'form1'}\n"; print "まずい$in{'form2'}\n"; print "</body>\n"; print "</html>\n"; exit; cgi-lib.plはサーバーにいれております。 どうしてうごかないのでしょうか。 perlの場所も正確です。

    • ベストアンサー
    • Perl
  • <Perl>参照配列の出力に失敗する。

    <Perl>参照配列の出力に失敗する。 お世話になります。 配列の出力部で以下のエラーが出力されます。 Use of uninitialized value in print at test2.pl line 12. -----コーディングは以下の通りです。----- #!C:\perl use strict; use warnings; my @l = (); #----------- #GetDataへCSVファイル名と、格納用配列を渡す #----------- my $cnt = &GetData("test.csv", \@l); print "COUNT -> ".$cnt; for(my $i=0; $i < $cnt; $i++){ print $l[$i]; } ################################################################## # 概   要:指定したCSVファイルをオープンしCSVデータを配列に取得する。 # パラメータ:ファイル名, CSVデータ格納用配列 # 戻 り 値:データ取得件数 ################################################################## sub GetData { my ($f, @bf) = @_; my $rcnt = 0; print "FILE NAME -> ".$f."\n"; if ( open(FP, "<${f}") ){ print "FILE OPEN -> success.\n"; @bf = split(/,/, <FP>); close(FP); $rcnt = @bf; print "CSV GET COUNT -> ".$rcnt."\n"; } return $rcnt; } -----実行結果は以下の通りです。----- D:\>perl test.pl FILE NAME -> test.csv FILE OPEN -> success. CSV GET COUNT -> 5 Use of uninitialized value in print at test2.pl line 12. Use of uninitialized value in print at test2.pl line 12. Use of uninitialized value in print at test2.pl line 12. Use of uninitialized value in print at test2.pl line 12. COUNT -> 5 -----CSVファイルの内容は以下の通りです。(ファイル名:test.csv)----- あいうえお,かきくけこ,さしすせそ,たちつてと,なにぬねの 配列の要素数が取れているので、配列内にデータは格納されているとは思っています。 出力方法をどのように正せばよいがご教示お願い致します。

    • ベストアンサー
    • Perl
  • Perlがサーバー上でうごきません

    htmlファイルには <html> <form action="form2.cgi" method="get"> 食品名1<input type="text"name="form1"> <br> 食品名2<input type="text"name="form2"> <input type="submit"value="送信"> </form> </html> cgiファイルには #! /usr/bin/perl require "cgi-lib.pl"; &ReadParse(); print "Content-type:text/html\n"; print "\n"; print "<html>\n"; print "<head>\n"; print "<title>フォーム</title>\n"; print "</head>\n"; print "<body>\n"; print "おいしい$in{'form1'}\n; print "まずい$in{'form2'}\n; print "</body>\n"; print "</html>\n"; exit; cgi-lib.plはサーバーにいれております。 どうしてうごかないのでしょうか。

    • ベストアンサー
    • Perl
  • Perl:ファイルハンドルをスカラー変数にすると変

    お世話になります。 Perlの書き方について教えてください。 Perlのファイルハンドルで、昔はINやOUTやFILEのようにベアワード(裸のワード)を使っていたと思います。 ★(プログラム1) #! /bin/perl # # fh_bareword.pl use strict; use warnings; open IN, '<', 'in.txt'; open OUT, '>', 'out.txt'; while (<IN>) { print OUT; } close IN; close OUT; ★(実行例) C:\Perl\perl>type in.txt This is a pen. That is a book. C:\Perl\perl>fh_bareword.pl C:\Perl\perl>type out.txt This is a pen. That is a book. ★★★ ところが、これだとIN、OUTがグローバル名になってしまい、他のモジュールと衝突することがあるということで、myつきのスカラー変数にした方が良いと言われました。 それで、以下のように書いてみましたが、想定のように動作しません。 ★(プログラム2) #! /bin/perl # # fh_scalar.pl use strict; use warnings; open my $in, '<', 'in.txt'; open my $out, '>', 'out.txt'; while (<$in>) { print $out; } close $in; close $out; ★(実行例) C:\Perl\perl>fh_scalar.pl GLOB(0x146ea34)GLOB(0x146ea34) C:\Perl\perl>dir out.txt ドライブ C のボリューム ラベルは eMachines です ボリューム シリアル番号は 89C9-F870 です C:\Perl\perl のディレクトリ 2013/07/09 23:05 0 out.txt 1 個のファイル 0 バイト 0 個のディレクトリ 23,226,032,128 バイトの空き領域 ★★★ たぶん print $out; の部分で、私はファイルハンドル$outに$_を表示してくださいと意図していたのですが、「ファイルハンドルSTDOUTに$outを表示してください」という意図に受け取られたらしく、上記のようにGLOBのリファレンス?が表示されてしまいました。 print $out $_; と書くと、確かに正しく表示されますが、これが正しい書き方なのでしょうか。 printの後ろに来るのは出力する文字列ではなくファイルハンドルだと、正しく判断してもらえるもっと美しい書き方はあるのでしょうか。 よろしくお願いします。

    • ベストアンサー
    • Perl
  • 【c言語】ファイルの読み込みエラーについて

    自然言語処理について勉強しています。 Wikipediaのdumpデータからタイトルと本文を抜き出して処理をしようと思ってます。 xmlの処理について試しているのですが、エラーに見舞われて困っています。 プログラミングはあまり得意ではないです。 以下にソースコードとエラー部分を貼り付けます。 ご助力お願いします。 ============================ #include <stdlib.h> #include <string.h> int main (){ size_t p; char pb[5]; char line[BUFSIZ]; FILE *fp = fopen("//Wikipediaの分割ファイル//.xml", "r");//xmlファイルのアドレス char title[BUFSIZ]; //FILE *gt=fopen("list_1.txt","r"); FILE *fw = fopen("get_text_c.txt", "w"); if ((NULL == fp)||(NULL== fw )){ printf("aboooooooooooooooooooooooooooot") abort();//終了、ここがうまくいってない? } while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { if (strstr(line, "<title>")){ pb[0] = p;//タイトルの始点 } else if (strstr(line, "</title>")){  pb[3]=p;//タイトルの終点   pb[2]=p-pb[0];//タイトルのバイト数  fprintf(fw, "%zu \t %zu \n", pb[2], pb[3]); //pb2とpb3の観察。  fgets(line,pb2[2],pb[0]);//タイトルを取得  printf("%s",line);//表示  fprintf(fw,"%s",line);//書き込み } else if (strstr(line, "</page>")) {  pb[1] = p - pb[0];  //fwrite(pb, sizeof(size_t), 2, fw); // ...  fprintf(fw, "%zu \t %zu \n", pb[0], pb[1]); //... テキスト形式で観察可能 } } } fclose(fw); fclose(fp); } エラー(ファイル名は get_うぃき.c としてます。) =========================== get_うぃき.c:10:16: warning: declaration of built-in function 'fopen' requires inclusion of the header <stdio.h> [-Wbuiltin-requires-header] get_うぃき.c:11:16: error: use of undeclared identifier 'BUFSIZ' char title[BUFSIZ]; ^ get_うぃき.c:14:5: error: use of undeclared identifier 'FILE' FILE *fw = fopen("get_text_c.txt", "w"); ^ get_うぃき.c:14:11: error: use of undeclared identifier 'fw' FILE *fw = fopen("get_text_c.txt", "w"); ^ get_うぃき.c:15:18: error: use of undeclared identifier 'fp' if ((NULL == fp)||(NULL== fw )){ ^ get_うぃき.c:15:31: error: use of undeclared identifier 'fw' if ((NULL == fp)||(NULL== fw )){ ^ get_うぃき.c:16:13: warning: treating Unicode character <U+FF08> as identifier character rather than as '(' symbol [-Wunicode-homoglyph] printf("aboooooooooooooooooooooooooooot") ^~ get_うぃき.c:16:7: error: use of undeclared identifier 'printf(' printf("aboooooooooooooooooooooooooooot") ^ get_うぃき.c:16:49: warning: treating Unicode character <U+FF09> as identifier character rather than as ')' symbol [-Wunicode-homoglyph] printf("aboooooooooooooooooooooooooooot") ^~ get_うぃき.c:20:16: error: implicit declaration of function 'ftell' is invalid in C99 [-Werror,-Wimplicit-function-declaration] while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:27: error: implicit declaration of function 'fgets' is invalid in C99 [-Werror,-Wimplicit-function-declaration] while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:39: error: use of undeclared identifier 'BUFSIZ' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:47: error: use of undeclared identifier 'fp' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:12: error: use of undeclared identifier 'p' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:22: error: use of undeclared identifier 'fp' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:22:15: error: use of undeclared identifier 'p' pb[0] = p;//タイトルの始点 ^ get_うぃき.c:25:7: warning: treating Unicode character as whitespace [-Wunicode-whitespace]  pb[3]=p;//タイトルの終点 ^~ get_うぃき.c:26:6: warning: treating Unicode character as whitespace [-Wunicode-whitespace]   pb[2]=p-pb[0];//タイトルのバイト数 ^~ get_tokuhisa.c:25:16: error

  • 英文書き換え

    上の文を下の文に書き換えるんですが、空欄には何がはいりますか? A voltmeter is connected in parallel or across the part of the circuit where voltage measurement is required. A voltmeter is connected in parallel or across the part of the circuit where ______ voltage is to be measured. requiredを入れると、「必要なボルト数」といみが変わっちゃうような気がして、かといって適切な単語が分かりません。 おしえてください。

  • 英訳「貴大学で必要とされるTOEFLの最低点は幾らですか?」と「条件付入学…

    おはようございます。 「貴大学で必要とされるTOEFLの最低点は幾らですか?」 と 「条件付入学とはTOEFL最低点に見たいない者でもESL受講義務を条件で入学できる制度の事です」 を英語で尋ねたいのですが What is required minimum TOEFL score in your school? A conditional admission is a system that those who don't satisfy the required score in your school can admit with ESL attending obligation. はおかしいでしょうか?

  • Spamassassinでsendmailが通らない!

    以前、sendmailが機能しないでサーバー管理者に何度も問い合わせたところ、どうやらSpamassassinに引っかかって削除されてたようです(>o<。回答がこんな感じできました(以下)。そこでSpamassassinに関して調べてみたのですが、どうやったら潜り抜けられる(?)のか、誰かご存知の方がいましたら助けて下さい。件名、本文ともにWindows1251のキリル語を使ってます。 SA: score=7.5 required=6.0 tests=BAYES_50,INVALID_MSGID,NO_REAL_NAME,SUBJ_ILLEGAL_CHARS Here are the meanings of different tests performed on mail messages to determine if they're spam or not present in the mail-log: BAYES_50 : Bayesian spam probability is 40 to 60% INVALID_MSGID : Message-Id is fake. NO_REAL_NAME : This rule fires when no name is given in the from header. SUBJ_ILLEGAL_CHARS : Subject: has too many raw illegal characters For more info, please check, http://spamassassin.apache.org/tests_3_1_x.html ちなみにヘッダは以下のように書いてます。 $head = ""; $head .= "To: $TO_MAIL\n"; $head .= "From: xxxxx@xxxxx.com\n"; $head .= "Return-Path: xxxxx@xxxxx.com\n"; $head .= "Reply-To: xxxxx@xxxxx.com\n"; $head .= "Errors-To: xxxxx@xxxxx.com\n"; $head .= "Subject: $sub\n"; $head .= "MIME-Version: 1.0\n"; $head .= "Content-type: text/plain; charset=\"windows-1251\"\n"; $head .= "Content-Transfer-Encoding: 8bit\n\n"; 以前使っていたサーバーではちゃんと動作していました(迷惑メール扱いにはなるものの)。一体どこをどう変えたらSpamassassinを通過できるんでしょうか・・・。もう訳分からなくなってきました(;_;)。よろしくお願いします<(_ _)>

  • perl -wc

    telnet を利用できないユーザーの為に、ブラウザ上でPerl(CGI)の文法チェックができるようにしています。 フォームでファイル名を入力させ、チェックプログラム内で "perl -wc $filename" を実行して結果を出力させるだけだと思い作成したのですが、実行結果を受け取ることができません。 apache のログには実行結果が書かれているのでコマンドは正常に動作しています。  open(IN, "perl -wc $filename |");  while (<IN>) {   print $_; と  $ret=`perl -wc $filename`; でやってみましたがダメでした。 どなたかご教授願います。 よろしくお願いいたします。

    • ベストアンサー
    • Perl