Skip to content

Instantly share code, notes, and snippets.

@eremita0083
eremita0083 / test.pl
Last active April 1, 2017 08:47
perl3-1, ファイルへの書き込み(リダイレクト出力とファイルを書き込みモードでopen)
use strict;
use warnings;
use Carp qw/croak/;
# 単一のtxt ファイルに結果を出力する場合は、ターミナルで perf filename > {txt} のように出力先を指定すると良い
# リダイレクト出力
my $file = shift;
# ファイル読み込み
# open 関数の 第一引数に 未定義のスカラ変数を渡す。 open が成功したら fileh_andler が取得できる
@eremita0083
eremita0083 / test.pl
Created April 1, 2017 08:19
2-10, まとめ ftp.riken.jp から.gz ファイルを取得して c:/tmp に保存する (サンプルコードがまちがってる。そのうち修正)
use strict;
use warnings;
use Carp qw/croak/;
# 以下のプラグマを利用
use Net::FTP;
use File::Spec;
use File::Copy qw/move/;
@eremita0083
eremita0083 / test.pl
Created March 31, 2017 06:50
perl2-9, ftp
use strict;
use warnings;
use Carp qw/croak/;
# ftp によるファイル転送
# 1, ホスト名 or ipアドレスを指定してftpサーバに接続
# 2, ipass でログイン
# 3, ftpコマンドでファイルをdl
# 4, 接続終了
@eremita0083
eremita0083 / test.pl
Created March 31, 2017 06:13
perl2-8, 日本語でファイル名を指定する
use strict;
use warnings;
use File::Copy q/copy/;
use Carp q/croak/;
# 以下のプラグマを利用する
use utf8;
use Encode q/encode/;
use Encode::UTF8Mac; # cpan でmodule をinstallしておく必要がある
@eremita0083
eremita0083 / test.pl
Created March 31, 2017 05:10
perl2-7, ファイル演算子
use strict;
use warnings;
# ファイル演算子 通常はif文の中で使う
# -e ファイルが存在するか。directory や simbolic link も含まれる。
# -d ディレクトリが存在するか
# -f 通常ファイルが存在するか。テキストファイルやbinファイル、データを格納しているファイル。
# -s ファイルサイズの確認。単位はバイト
# -M 最終更新から経過した日数の取得
# -A 最終アクセスから経過した日数の取得
@eremita0083
eremita0083 / test.pl
Last active March 31, 2017 05:03
perl2-6, directory と file 操作(ファイル名の連結や取得)
use strict;
use warnings;
use File::Basename qw/basename dirname fileparse/;
use File::Spec qw/catfile/;
use Carp 'croak';
use FindBin;
use lib "$FindBin::Bin/lib";
# 絶対パスから末尾のファイル名を取得
@eremita0083
eremita0083 / test.pl
Created March 31, 2017 04:03
perl2-5, directory と file の操作1
use strict;
use warnings;
# move copy
use File::Copy qw/move copy/;
use Carp qw/croak/;
if ($#ARGV < 0) {
croak qq/No argments : $!/;
@eremita0083
eremita0083 / test.pl
Last active March 30, 2017 07:25
perl2-4, 無名配列、無名ハッシュ
use strict;
use warnings;
# use utf8;
# 無名配列生成 --- () を [] に置き換える
my $arr_ref = [1,2,3];
print @$arr_ref, "\n";
# 要素にアクセスする
print $$arr_ref[0], "\n";
@eremita0083
eremita0083 / test.pl
Last active March 29, 2017 07:53
perl2-3. リファレンスとデリファレンス
use strict;
use warnings;
# use utf8;
# スカラ値を生成
my $str = 'hello';
my @arr = (1, 2);
my %hs = ("key1" => '1', "key2" => '2');
# reference を作成 (参照). \ を prefix の前につける。
@eremita0083
eremita0083 / test.pl
Last active March 29, 2017 02:40
perl2-2, POD(plain old doc。perl の標準的なdocの形式) の書き方と使用方法の出力
use strict;
use warnings;
# 文字列で日本語を扱うなら utf8 プラグマを利用する
use utf8;
# 呼び出し元の視点から、エラーが呼ばれた行を記述できる
use Carp 'croak';
# 使用方法を出力するためのmodule
# module を追加する時は、可能な限り使用する関数を明示する