perl 5 まとめウィキ

コードを引数に渡して、evalで実行しちゃう感じです。


#!/usr/bin/perl 

use warnings;
use strict;
use Devel::EvalContext;

my $cxt = Devel::EvalContext->new();
my $hoge =$cxt->run(q{my $foo = "hoge";});
$cxt->run(q{print $foo;}); # hoge 

ユースケース

ターミナルにコードを書いて実行


Jifty::Script::Console?というモジュールで使われています。ちょとした開発時に役にたつかも?

sub run {
    my $self = shift;
    Jifty->new();
    my $term = new Term::ReadLine 'Jifty Console';
    my $OUT = $term->OUT || \*STDOUT;
    my $cxt = Devel::EvalContext->new;
    while (defined($_ = $term->readline("jifty> "))) {
        if (/\S/) {
            my $res = $cxt->run($_);
            warn $@ if $@;
            print $OUT $res, "\n" unless $@ || !defined($res);
            $term->addhistory($_);
        }
    }
}

このページへのコメント

gZGqtq Major thanks for the blog article.Much thanks again. Much obliged.

0
Posted by awesome things! 2014年01月21日(火) 05:52:36 返信

tbKLq6 I cannot thank you enough for the blog.Really looking forward to read more. Really Cool.

0
Posted by seo thing 2013年12月20日(金) 22:57:34 返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

Menu

ここは自由に編集できるエリアです。

フリーエリア

どなたでも編集できます