Memo

メモ > 技術 > プログラミング言語: Perl

■基本的な文法
■昔ながらの解説 とほほのperl入門 - とほほのWWW入門 https://www.tohoho-web.com/wwwperl.htm Perl基礎入門 | KentWeb https://www.kent-web.com/perl/ Perlの文法基礎 - Web Liberty https://www.web-liberty.net/improve/perl/index.html ■モダンPerl 現代的なPerlの記述方法一覧 - PerlならサンプルコードPerl入門 http://d.hatena.ne.jp/perlcodesample/20091120/1246679588 おそらくはそれさえも平凡な日々: モダンなPerlを「読む」上で覚えておくとよい構文 第1回(?) http://www.songmu.jp/riji/archives/2010/03/perl_1_1.html
■基本的なプログラム
■Linux
#!/usr/local/bin/perl print "Content-Type: text/html; charset=Shift_JIS\n\n"; print "<html>\n"; print "<head><title>サンプル</title></head>\n"; print "<body>\n"; print "<p>初めてのCGI。</p>\n"; print "</body>\n"; print "</html>\n"; exit;
■XAMPP
#!/xampp/perl/bin/perl print "Content-Type: text/html; charset=Shift_JIS\n\n"; print "<html>\n"; print "<head><title>サンプル</title></head>\n"; print "<body>\n"; print "<p>初めてのCGI。</p>\n"; print "</body>\n"; print "</html>\n"; exit;
■Perlのパス
■Linux環境 以下のように指定しておくと、大抵のLinux環境で動く ただし、意図しないバージョンのPerlを参照してしまう可能性があるようなので注意
#!/usr/bin/env perl
Perlにおけるお勧めのシェバンの書き方 - Perlゼミ|Perlの基礎をインストールからサンプルで丁寧に解説 https://perlzemi.com/blog/20200731104457.html /usr/bin/envを使って移植性の高いスクリプトを書く方法 | TECH+(テックプラス) https://news.mynavi.jp/techplus/article/20180620-650003/ ■XAMPP環境 Perlのパスを任意のものに変更する方法 XAMPP アドオンのPerlを入れ替える (Windows 10) #Perl - Qiita https://qiita.com/jagaimo300/items/f8ad2919d579080c22d2 【2020年】win10のXAMPPでPerl開発環境♪Perlの場所の宣言│そらのまなびぶろぐ https://soramanabrog.com/thor/xampp-s-perl/
■ローカル環境構築(XAMPP)
■XAMPPインストール 通常の手順でインストールする ■CPANからモジュールを追加 まずは以下を対応する Perl〜XAMPPのCPANでエラー https://nandaka.net/archives/1013 エクスプローラーで以下を開く C:\xampp\perl\bin cpan.bat をダブルクリックで実行する 以下のとおり「install Image::EXIF」をインストールできた
Loading internal logger. Log::Log4perl recommended for better logging Unable to get Terminal Size. The Win32 GetConsoleScreenBufferInfo call didn't work. The COLUMNS and LINES environment variables didn't work. at C:\xampp\perl\vendor\lib/Term/ReadLine/readline.pm line 410. cpan shell -- CPAN exploration and modules installation (v2.27) Enter 'h' for help. cpan> install Image::EXIF Database was generated on Tue, 31 Oct 2023 08:14:11 GMT Running install for module 'Image::EXIF' Checksum for C:\xampp\cpan\sources\authors\id\A\AR\ARC\Image-EXIF-2.01.tar.gz ok Scanning cache C:\xampp\cpan\build for sizes ............................................................................DONE Configuring A/AR/ARC/Image-EXIF-2.01.tar.gz with Makefile.PL Checking if your kit is complete... Looks good 〜略〜 ok 1 Lockfile removed. ARC/Image-EXIF-2.01.tar.gz C:\xampp\c\bin\gmake.exe test -- OK Running make install for ARC/Image-EXIF-2.01.tar.gz "C:\xampp\perl\bin\perl.exe" -MExtUtils::Command::MM -e cp_nonempty -- EXIF.bs blib\arch\auto\Image\EXIF\EXIF.bs 644 Files found in blib\arch: installing files in blib\lib into architecture dependent library tree Installing \xampp\perl\site\lib\auto\Image\EXIF\EXIF.xs.dll Installing \xampp\perl\site\lib\Image\EXIF.pm Appending installation info to \xampp\perl\lib/perllocal.pod ARC/Image-EXIF-2.01.tar.gz C:\xampp\c\bin\gmake.exe install UNINST=1 -- OK
それらしいファイルが以下に作成されている C:\xampp\perl\site\lib\Image\EXIF.pm コマンドライン経由だとモジュールを参照できるが、Apache経由だと参照できず …だったが、Windowsを再起動すると解消した
#!/xampp/perl/bin/perl use strict; use warnings; use Image::Exif; print "Content-Type: text/html; charset=Shift_JIS\n\n"; print "<html>\n"; print "<head><title>Perl</title></head>\n"; print "<body>\n"; print "<p>Perl = " . $] . "</p>\n"; print "</body>\n"; print "</html>\n"; exit;
"Shade検索"Wiki https://shade-search.com/sts/term/term.php?sort=category&category_filter=Perl&noform=all 10年くらいぶりにPerlのCPANモジュールをインストールしようとしたら全然覚えてなくて焦った。 - tohokuaikiのチラシの裏 https://tohokuaiki.hateblo.jp/entry/2018/09/27/10%E5%B9%B4%E3%81%8F%E3%82%89%E3%81%84%E3%81%B6%E3%82... Perl モジュールのインストール方法 | CMSプラットフォーム Movable Type ドキュメントサイト https://www.movabletype.jp/documentation/mt7/appendices/install-perl-modules/ CPAN - CPANからモジュールインストール - Perlゼミ|Perlの基礎をインストールからサンプルで丁寧に解説 https://perlzemi.com/blog/20100504127089.html PerlでDBIでMySQLに接続 - m_shige1979のときどきITブログ https://m-shige1979.hatenablog.com/entry/2014/03/04/211419 ■データベース接続 PerlでDBIでMySQLに接続 - m_shige1979のときどきITブログ https://m-shige1979.hatenablog.com/entry/2014/03/04/211419 PerlのDBIモジュールで自動的にUTF8フラグを付ける - ノウハウブログ - カンタローCGI https://kantaro-cgi.com/blog/perl/auto_utf8_flagon_by_dbi.html Perl DBI と UTF8フラグ と 文字化け問題 (2017/05/31追記) - adiary開発日誌 https://adiary.adiary.jp/0367 DBIのインストールコマンドを実行しなくても接続できているような CPANの設定をしたことで、使えるようになったのか
#!/xampp/perl/bin/perl use strict; use warnings; use DBI; my $dbh; $dbh = DBI->connect( 'DBI:mysql:database=test;host=localhost;port=3306;', 'root', '1234', { mysql_enable_utf8 => 1 } ); print "Content-Type: text/html; charset=UTF-8\n\n"; print "<html>\n"; print "<head><title>Perl + MySQL</title></head>\n"; print "<body>\n"; print "<h1>Perl + MySQL</h1>\n"; print "<ul>\n"; my $sth; my $ref; $sth = $dbh->prepare('SELECT * FROM table_test LIMIT 10;'); $sth->execute(); while ($ref = $sth->fetchrow_hashref()) { print "<li>$ref->{'id'}: $ref->{'text'}</li>\n"; } $sth->finish(); print "</ul>\n"; print "</body>\n"; print "</html>\n"; exit;
■ローカル環境構築(StrawberryPerl)
■StrawberryPerlインストール かつてはActivePerlが定番だったが、今は色々と問題があるらしい 代わりに、StrawberryPerlがよく紹介されている 今更だけど、ActivePerlからStrawberryPerlに乗り換えた話: 書きたい事や思いついた事を記録する備忘ブログ http://blog.makewls.com/article/188720799.html さよならActivePerl、こんにちはStrawberry Perl - mballackの日記 https://mballack.hatenadiary.org/entry/2020/09/29/160427 以下を参考に、XAMPPからStrawberryPerlを使ってみる Perl | Strawberry Perlのダウンロード及びインストール https://www.javadrive.jp/perl/activeperl/index4.html 以下でPerlのバージョンを確認すると、「5.10.1 以上(5.32 を推奨 / 5.36 まで検証済み)」と書かれている 動作に必要な環境とブラウザの対応 | CMSプラットフォーム Movable Type ドキュメントサイト https://www.movabletype.jp/documentation/system_requirements.html 以下にアクセスして「More downloads (all releases)」から「5.36.1.1 / 64bit」をダウンロードする Strawberry Perl for Windows https://strawberryperl.com/ ダウンロードした「strawberry-perl-5.36.1.1-64bit.msi」をダブルクリックでインストール デフォルト設定のまま進めて「C:\Strawberry\」にインストール 完了すると、以下の環境変数が自動で追加される C:\Strawberry\ C:\Strawberry\perl\site\bin C:\Strawberry\perl\bin コマンドラインから、Perlを参照できていることを確認する
>perl -v This is perl 5, version 36, subversion 1 (v5.36.1) built for MSWin32-x64-multi-thread Copyright 1987-2023, 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 https://www.perl.org/, the Perl Home Page.
CGIプログラムから参照してみる 「Perl = 5.036001」のように表示されれば成功(Perl5.6.0以降、バージョンの値は「5.6.0」ではなく「5.006」のように表示されるので注意)
#!/Strawberry/perl/bin/perl print "Content-Type: text/html; charset=Shift_JIS\n\n"; print "<html>\n"; print "<head><title>Perl</title></head>\n"; print "<body>\n"; print "<p>Perl = " . $] . "</p>\n"; print "</body>\n"; print "</html>\n"; exit;
Perl/CGIプログラムからPerlパッケージのバージョンを取得する - 木村秀一のホームページ https://kimurashuuichi.com/programming/perl/memo/system_perl_version.html ■既存モジュールの利用を確認
#!/Strawberry/perl/bin/perl use strict; use warnings; use Math::BigInt; my $big_int = Math::BigInt->new('11111111111111111111111111111111111111111111'); $big_int = $big_int * 2; print "Content-Type: text/html; charset=Shift_JIS\n\n"; print "<html>\n"; print "<head><title>Perl</title></head>\n"; print "<body>\n"; print "<p>BigInt = " . $big_int . "</p>\n"; print "</body>\n"; print "</html>\n"; exit;
Math::BigInt、Math::BigFloat - 大きな桁数を持つ数の計算 - Perlゼミ|Perlの基礎をインストールからサンプルで丁寧に解説 https://perlzemi.com/blog/20080925122245.html ■CPANからモジュールを追加
>cpan Loading internal logger. Log::Log4perl recommended for better logging Starting with version 2.29 of the cpan shell, a new download mechanism is the default which exclusively uses cpan.org as the host to download from. The configuration variable pushy_https can be used to (de)select the new mechanism. Please read more about it and make your choice between the old and the new mechanism by running o conf init pushy_https Once you have done that and stored the config variable this dialog will disappear. Unable to get Terminal Size. The Win32 GetConsoleScreenBufferInfo call didn't work. The COLUMNS and LINES environment variables didn't work. at C:\Strawberry\perl\vendor\lib/Term/ReadLine/readline.pm line 410. cpan shell -- CPAN exploration and modules installation (v2.36) Enter 'h' for help. cpan> install Image::EXIF Starting with version 2.29 of the cpan shell, a new download mechanism is the default which exclusively uses cpan.org as the host to download from. The configuration variable pushy_https can be used to (de)select the new mechanism. Please read more about it and make your choice between the old and the new mechanism by running 〜中略〜 Files found in blib\arch: installing files in blib\lib into architecture dependent library tree Installing C:\STRAWB~1\perl\site\lib\auto\Image\EXIF\EXIF.xs.dll Installing C:\STRAWB~1\perl\site\lib\Image\EXIF.pm Appending installation info to C:\STRAWB~1\perl\lib/perllocal.pod ARC/Image-EXIF-2.01.tar.gz C:\STRAWB~1\c\bin\gmake.exe install UNINST=1 -- OK
コマンドライン経由だとモジュールを参照できるが、Apache経由だと参照できず …だったが、Windowsを再起動すると解消した
#!/Strawberry/perl/bin/perl use strict; use warnings; use Image::Exif; print "Content-Type: text/html; charset=Shift_JIS\n\n"; print "<html>\n"; print "<head><title>Perl</title></head>\n"; print "<body>\n"; print "<p>Perl = " . $] . "</p>\n"; print "</body>\n"; print "</html>\n"; exit;
■データベース接続 DBIのインストールコマンドを実行しなくても接続できているような CPANの設定をしたことで、使えるようになったのか Perlのパス以外は、「ローカル環境構築(XAMPP)」と同じで接続できる

Advertisement