Skip to content

Instantly share code, notes, and snippets.

use strict;
use IO::File;
my $fh = new IO::File("somefile.txt", "r") || die "can't open file. $!¥n";
# 引数としてファイルハンドル(実体はIO::Fileオブジェクト)を渡す
&some_process($fh);
sub some_process {
# ファイルハンドルの受け取り
use strict;
use IO::File;
my $stdin_fh = new IO::File;
# STDIN(標準入力)を$stdin_fhに紐付ける
$stdin_fh->fdopen(fileno(STDIN), "r");
# 後は$stdin_fhを通常のIO::Fileオブジェクトの様に使える。
@shinchit
shinchit / gist:5268872f317559af2094
Last active August 29, 2015 14:10
Perlのスカラ変数で値が数値かどうか判定する方法
use strict;
use Scalar::Util qw/looks_like_number/;
my $var1 = 123;
my $var2 = 'abc';
if (looks_like_number($var1)) {
# $var1は数値なのでif文はtrueになる
print "数値です", "¥n";
}
@shinchit
shinchit / gist:f282bd242a51172f649d
Last active August 29, 2015 14:11
Perlでクラスのアクセサを簡単に作成する方法
use parent qw/Class::Accessor/;
__PACKAGE__->mk_accessors(qw/foo/);
# you can use
$self->foo; # gettter
$self->foo('some value'); # setter
@shinchit
shinchit / gist:80df67912529b1b1f67b
Created December 9, 2014 21:19
クラス名を得る(何らかのオブジェクトであるかどうかを判定する)方法
use Scalar::Util qw/blessed/;
my $obj = new MyClass;
if (blessed($obj)) {
print "is MyClass object.", "¥n";
print ref($obj); # you get class name (in this situation, you get MyClass)
} else {
print "is not any object.", "¥n";
}
@shinchit
shinchit / gist:4b4ff18fce8057d53ca6
Created December 10, 2014 06:54
メソッドがクラスに存在するかを調べる
my $obj = new MyClass;
# MyClassにメソッドfooが存在するかを調べる
if ($obj->can('foo')) {
print "存在する", "¥n";
} else {
print "存在しない", "¥n";
}
@shinchit
shinchit / gist:ee41d0394a592c9a9303
Created December 11, 2014 07:01
Module::Starter::PBPのインストール
$ cpan install Module::Starter
$ cpan install Module::Starter::PBP
$ cpan install Module::Starter::Smart
@shinchit
shinchit / gist:b3019956dca903230394
Last active August 29, 2015 14:11
モジュールの骨組み、~/.module-starter/の作成
$ perl -MModule::Starter::PBP=setup
Please enter your full name : [your name]
Please enter an email address : [your email address]
@shinchit
shinchit / gist:c2971ca1b4fd3b414110
Created December 11, 2014 07:14
Module::Starter::Smartの有効化
plugins: Module::Starter::PBP Module::Starter::Smart
@shinchit
shinchit / gist:08d199496c75a304c776
Created December 11, 2014 07:18
module-starterの実行例
$ module-starter --module=DataStracture::LinkedList