Permalink
Browse files

[Ratel] printing -> returning a string

Inside the eval, we override the &print sub to collect to a dynamic variable
instead. (This lets through &say, .print and .say, but it's a start.)
  • Loading branch information...
1 parent 511ea75 commit 061b062ddbd64b4eb9b6d7f703ae06e70565fa9d @masak committed Jun 9, 2010
Showing with 10 additions and 5 deletions.
  1. +10 −5 lib/Ratel.pm
View
@@ -1,7 +1,7 @@
class Ratel {
has $.source;
- has $.compiled;
- has @.hunks;
+ has $!compiled;
+ has @!hunks;
method load(Str $filename) {
$.compile(slurp($filename));
@@ -16,13 +16,18 @@ class Ratel {
= $source.subst(/(['%]' | ^ ] .*? [ $ | '[%' ])/,
{";\$.emit-hunk({$index++});"},
:g);
+ $!compiled = 'my sub print(*@args) { $*result ~= $_ for @args };'
+ ~ $!compiled;
+ return;
}
method emit-hunk(Int $i) {
- print @.hunks[$i][0];
+ $*result ~= @!hunks[$i][0];
}
- method do(*%attrs) {
- eval $.compiled;
+ method serialize(*%attrs) {
+ my $*result = '';
+ eval $!compiled;
+ return $*result;
}
}

0 comments on commit 061b062

Please sign in to comment.