###前提・実現したいこと→プログラムを期待通りに動作させたい
外務省海外安全ホームページから危険情報を確認するプログラムをperlで書いています。
ホームページにアクセスする機能を実装中に以下のエラーメッセージが発生しました。
コードをどのように修正すればこのプログラムは動作するでしょうか?
ネットからダウンロードしたサンプルファイルです。
「使用しているプラットフォームにあわせてください。」とコメントが書いてあります。
この使用しているプラットフォームに合わせるとはどういった意味なのでしょうか?
プログラムが期待通りの動作をしない原因はこれなのでしょうか?
エラーメッセージ等をネットで検索してみたのですが、分かりませんでした。
どなたかご教授いただけないでしょうか。
何卒よろしくお願いします。
###該当のソースコード
mofa_anzen.pl
perl
1 1 #!/usr/bin/perl -w 2 2 use strict; 3 3 use LWP::Simple; 4 4 use Encode; 5 5 use encoding 'shiftjis'; # 使用しているプラットフォームにあわせてください。 6 6 binmode(STDERR, ':raw :encoding(shiftjis)'); # 同上 7 7 8 8 my $id = shift @ARGV || '074'; # 引数の取得(デフォルトはニュージーランド) 9 9 10 10 # 外務省海外安全ホームページ 11 11 my $url = "http://www.anzen.mofa.go.jp/info/info4.asp?id=$id"; 12 12 13 13 my $content = get($url); 14 14 die "$url を読み込めませんでした。" unless defined $content; 15 15 16 16 # $contentを読み込んで危険情報を確認する。 17 17 $content = decode('shiftjis', $content); # コンテンツのコード変換 18 18 $content =~ s/<!--(.*?)-->//gs; # HTML内のコメント削除 19 19 $content =~ / SELECTED>▼([^<]+)</; # 国名の取得 20 20 print "$1\n"; 21 21 if ($content =~ /現在、危険情報は出ておりませんが、/) { 22 22 print " 危険情報は出ていません。\n"; 23 23 } else { print " 危険情報が出ています!\n"; }; 24 24 print " 詳細はhttp://www.anzen.mofa.go.jp/を見てください。\n"; 25
###発生している問題・エラーメッセージ
$ perl mofa_anzen.pl
Use of the encoding pragma is deprecated at mofa_anzen.pl line 5. Bareword found where operator expected at mofa_anzen.pl line 14, near ""$url を読み込めませんでした。" unless" (Missing operator before ss?) syntax error at mofa_anzen.pl line 14, near ""$url を読み込めませんでした。" unless " Unrecognized character \xE3; marked by <-- HERE after ofa.go.jp/<-- HERE near column 48 at mofa_anzen.pl line 23.
###補足情報(言語/FW/ツール等のバージョンなど)
$ perl -v
とすると
This is perl 5, version 20, subversion 2 (v5.20.2) built for x86_64-linux-gnu-thread-multi
(with 53 registered patches, see perl -V for more detail)
Copyright 1987-2015, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
と表示されます。
###余談
また、ネット上で配布されていたサンプルコードはもともとWINDOWS-31Jで書かれていたようなので、vimで編集しようとした際に文字化けをしていました。
そこで
find copy -type f | xargs file | grep -v ":.*pl" | cut -d: -f1 | xargs -t -I{} iconv -f WINDOWS-31J -t utf-8 {} -o {}
で変換するということを行いました。これらは原因に関係ありますか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/12 11:52
2017/01/12 11:58
2017/01/12 12:25
2017/01/15 10:41