optree hacking in the YAPC::Asia hackthon
作者:gugod 發佈於:In the YAPC::Asia Hackthon, done a really awesome hacks on PerlX::Range module with flora -- We re-implement it with XS approach so it works more robust.
The current (version 0.04) implementation is to use B::OPCheck
to
replace the statements like:
1..100
To something like
1 + PerlX::Range->new(last => 100)
This can be easily broken in many ways (inappropriate precendence, random syntax errors, etc).
The new approach is to do it in XS with B::Hooks::OP::Check
to
replace the flipflop range OP with a manually built entersub OP.
Effectivly, truning:
1..100
into
PerlX::Range::xrange(1, 100)
which returns a PerlX::Range object.
The current XS base implementation is in the xs branch on github, take a look if you're interested.
Here are some brief examples to try:
# build into blib/ first
perl Makefile.PL; make;
# => PerlX::Range
perl -Mblib -MPerlX::Range -E 'say ref(1..10000)'
# => 10000;
perl -Mblib -MPerlX::Range -E '$r = 1..10000; print $r->items'
# => 10000;
perl -Mblib -MPerlX::Range -MData::Dumper -E '$r = 'a'..'z'; say Dumper($r)'