home ホーム search 検索 -  login ログイン  | reload edit datainfo version cmd icon diff delete  | help ヘルプ

Perl/codepiece/caller関数

Perl/codepiece/caller関数

Perl / codepiece / caller関数
id: 342 所有者: msakamoto-sf    作成日: 2004-09-14 22:03:11
カテゴリ: Perl 

現在のサブルーチン呼び出しのコンテキストを取得するcaller関数を使うと、パッケージ名や関数名などを取得できる。

以下、簡単なコードピース

#!/usr/bin/perl
 
use strict;
use warnings;
 
{
    package Sample;
    sub whoami {
#        my ($package, $filename, $line) = caller;
        my ($package, $filename, $line, 
            $subroutine, $hasargs, $wantarray, 
            $evaltext, $is_require, $hints, $bitmask) = caller(0);
 
        print "package = [${package}]\n" if defined($package);
        print "filename = [${filename}]\n" if defined($filename);
        print "line = [${line}]\n" if defined($line);
        print "subroutine = [${subroutine}]\n" if defined($subroutine);
        print "hasargs = [${hasargs}]\n" if defined($hasargs);
        print "wantarray = [${wantarray}]\n" if defined($wantarray);
        print "evaltext = [${evaltext}]\n" if defined($evaltext);
        print "is_require = [${is_require}]\n" if defined($is_require);
        print "hints = [${hints}]\n" if defined($hints);
        print "bitmask = [${bitmask}]\n" if defined($bitmask);
        print '__FILE__ = [', __FILE__, "]\n";
        print '__PACKAGE__ = [', __PACKAGE__, "]\n";
    }
}
 
Sample::whoami;
 
print "\n--------------------------\n";
my ($package, $filename, $line) = caller;
print "package = [${package}]\n" if defined($package);
print "filename = [${filename}]\n" if defined($filename);
print "line = [${line}]\n" if defined($line);
print '__FILE__ = [', __FILE__, "]\n";
print '__PACKAGE__ = [', __PACKAGE__, "]\n";

実行例

C:\in_vitro\perl\basic>perl caller01.pl
package = [main]
filename = [caller01.pl]
line = [29]
subroutine = [Sample::whoami]
hasargs = [1]
hints = [2]
bitmask = [UUUUUUUUUUUU]
__FILE__ = [caller01.pl]
__PACKAGE__ = [Sample]

--------------------------
__FILE__ = [caller01.pl]
__PACKAGE__ = [main]


プレーンテキスト形式でダウンロード
現在のバージョン : 1
更新者: msakamoto-sf
更新日: 2009-04-12 22:10:54
md5:3be4e4f3281e9892ed6769a03a1c17f9
sha1:22772be132b5237ecc10f04fcef8d85012dcbb54
コメント
コメントを投稿するにはログインして下さい。