reflection and hooks
スクリプト言語の比較
http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。
reflection and hooks
perl | php | python | ruby | |
---|---|---|---|---|
class |
ref $a
|
<?php get_class($a) ?>
|
type(a)
|
a.class
|
has method? |
$a->can('reverse')
|
<?php method_exists($a, 'reverse') ?>
|
hasattr(a,'reverse')
|
a.respond_to?('reverse')
|
message passing |
for $i (0..10) { $meth = "phone$i"; $a->$meth(undef); }
|
<?php for ($i = 1; $i <= 10; $i++) { call_user_func(array($a, "phone$i"), NULL); } ?>
|
for i in range(1,10): getattr(a,"phone"+str(i))(None)
|
(1..9).each do |i| a.send("phone#{i}="nil) }
|
eval |
while(<>) { print ((eval), "\n"); }
|
<?php ?>
|
while True: print(eval(sys.stdin.readline()))
|
loop do puts eval(gets) end
|
methods |
$class = ref($a); keys eval "%${class}::";
|
<?php ?>
|
[m for m in dir(a) if callable(getattr(a,m))]
|
a.methods
|
attributes |
keys %$a;
|
<?php ?>
|
dir(a)
|
a.instance_variables
|
pretty print |
require 'dumpvar.pl'; %h = ('foo'=>1, 'bar'=>[2, 3]); dumpValue(\%h);
|
<?php $h = array('foo'=>1, 'bar'=>array(2,3)); print_r($h); ?>
|
import pprint h = {'foo':1, 'bar':[2,3] } pprint.PrettyPrinter().pprint(h)
|
require 'pp' h = { 'foo'=>1, 'bar'=>[2,3] } pp h
|
source line number and file name |
__LINE__ __FILE__
|
<?php __LINE__ __FILE__ ?>
|
import inspect
c = inspect.currentframe()
c.f_lineno
c.f_code.co_filename
|
__LINE__ __FILE__
|