動的実行
perlスクリプト内のサブルーチンの場合はeval命令により対応可能
sample.pl
# チェックプラグマ
use strict;
use warnings;
# サブルーチンを指定
print "-- 制的指定 ----------------\n";
func01();
func02(100, 200);
print "-- 動的指定 ----------------\n";
# 動的にサブルーチン名を切り替え
my $func;
# 変数を指定する()は引数なしの場合は省略可
$func = "func01()";
eval $func;
# 引数も変数に指定すること
$func = "func02(300, 400)";
eval $func;
sub func01
{
print "===test-func01===\n";
}
sub func02
{
my($param01, $param02) = @_;
print "===test-func02===$param01,$param02\n";
}
BEGIN {
print "----------------------------------\n";
print "-開始-----------------------------\n";
print "----------------------------------\n";
}
END {
print "----------------------------------\n";
print "-終了-----------------------------\n";
print "----------------------------------\n";
}
結果
>perl sample.pl
----------------------------------
-開始-----------------------------
----------------------------------
-- 制的指定 ----------------
===test-func01===
===test-func02===100,200
-- 動的指定 ----------------
===test-func01===
===test-func02===300,400
----------------------------------
-終了-----------------------------
----------------------------------
>
Module内の制的メソッド実行時もeval命令で対応可能
Module02.pm
# チェックプラグマ
use strict;
use warnings;
# パッケージ名定義
package Module02;
# メソッド定義
sub test01
{
#
print "Module02::test01\n";
}
sub test02
{
#
my ($param01, $param02) = @_;
print "Module02::test02 -- $param01, $param02-- \n";
}
1;
sample.pl
# チェックプラグマ
use strict;
use warnings;
use Module02;
# サブルーチンを指定
print "-- 制的指定 ----------------\n";
Module02::test01;
Module02::test02(100, 200);
print "-- 動的指定 ----------------\n";
# 動的にサブルーチン名を切り替え
my $func;
# 変数を指定する()は引数なしの場合は省略可
$func = "Module02::test01()";
eval $func;
# 引数も変数に指定すること
$func = "Module02::test02(300, 400)";
eval $func;
BEGIN {
print "----------------------------------\n";
print "-開始-----------------------------\n";
print "----------------------------------\n";
}
END {
print "----------------------------------\n";
print "-終了-----------------------------\n";
print "----------------------------------\n";
}
結果
>perl sample.pl
----------------------------------
-開始-----------------------------
----------------------------------
-- 制的指定 ----------------
Module02::test01
Module02::test02 -- 100, 200--
-- 動的指定 ----------------
Module02::test01
Module02::test02 -- 300, 400--
----------------------------------
-終了-----------------------------
----------------------------------
>
newされたオブジェクトは変数名の指定で実行可能
Module01.pm
# チェックプラグマ
use strict;
use warnings;
# パッケージ名定義
package Module01;
# コンストラクタ
sub new {
# オブジェクトを取得
my $class = shift;
print "test-constract\n";
# リファレンス
my $ref = {};
# オブジェクト生成
my $obj = bless $ref, $class;
# オブジェクト返却
return $obj;
}
# メソッド定義
sub test01
{
#
print "Module01::test01\n";
}
sub test02
{
#
print "Module02::test01\n";
}
# デストラクタ
sub DESTROY {
# オブジェクトを取得
my $obj = shift;
# 処理
print "test-descract\n";
}
1;
sample.pl
# チェックプラグマ
use strict;
use warnings;
# モジュール取り込み
use Module01;
# インスタンス生成
my $obj = Module01->new();
# メソッド名を指定して実行
$obj->test01();
# 変数にメソッド名を指定することで動的に切り替えることが可能
my $method = "test01";
$obj->$method();
BEGIN {
print "----------------------------------\n";
print "-開始-----------------------------\n";
print "----------------------------------\n";
}
END {
print "----------------------------------\n";
print "-終了-----------------------------\n";
print "----------------------------------\n";
}
結果
>perl sample.pl
----------------------------------
-開始-----------------------------
----------------------------------
test-constract
Module01::test01
Module01::test01
test-descract
----------------------------------
-終了-----------------------------
----------------------------------
>
最終更新:2012年01月21日 05:46