Skip to content

Commit

Permalink
very simple implementation of foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpilot committed Dec 24, 2009
1 parent 32cde5f commit d904f49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Partcl/commands/main.pm
Expand Up @@ -201,7 +201,18 @@ our sub for(*@args) {
'';
}

# TODO: do this correctly! This is a very naive implementation.
our sub foreach(*@args) {
if +@args == 0 || +@args % 2 == 0 {
error('wrong # args: should be "foreach varList list ?varList list ...? command"');
}
my $var := @args[0];
my @list := split(@args[1],' ');
my $body := @args.pop; # Body is always last
for @list -> $val {
set($var,$val);
eval($body);
}
}

our sub format(*@args) {
Expand Down
2 changes: 1 addition & 1 deletion t/cmd_foreach.t
Expand Up @@ -9,7 +9,7 @@ eval_is {foreach} \

eval_is {foreach a b q {puts $a}} \
{wrong # args: should be "foreach varList list ?varList list ...? command"} \
{uneven # of args}
{even # of args}

eval_is {foreach {} {a b c} {puts foo}} \
{foreach varlist is empty} \
Expand Down

0 comments on commit d904f49

Please sign in to comment.