パッケージ・モジュール
定義
Module01.pm
# パッケージモジュール定義
package Module01;
# public変数
our $var1 = "test-our";
our $var2 = "test-my";
# new
sub new{
#
$class = shift;
my $self = {};
$self->{aaa} = 10;
# オブジェクトを生成
my $obj = bless($self, $class);
# オブジェクトを返却する
return $obj;
}
# サブルーチン
sub test_func1{
print "test-module01-list\n";
}
# インスタンスメソッド
sub print_test1{
$self = shift;
print $self->{aaa} . "\n";
}
# モジュール終端記号
1;
sample.pl
# パッケージ使用宣言
use Module01;
# パッケージ変数にアクセス
print $Module01::var1 . "\n";
print $Module01::var2 . "\n";
# サブルーチン呼び出し
Module01::test_func1();
print "----------------------------------------\n";
# インスタンス変数定義して使用
$obj = Module01->new();
$obj->print_test1();
実行結果
>sample.pl
test-our
test-my
test-module01-list
----------------------------------------
10
>
最終更新:2012年01月14日 09:35