Created
September 26, 2018 09:14
-
-
Save karupanerura/5b7dd8d4d58abbe623fa8fedb3e56f49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
package Node { | |
use Class::Accessor::Lite ro => [qw/value children/], new => 1; | |
} | |
# DSL | |
package main { | |
sub node ($;&) { goto \&_node } | |
sub _node { | |
my ($value, $code) = @_; | |
my @children; | |
if ($code) { | |
my $node_sub = sub ($;&) { push @children => _node(@_) }; | |
no warnings qw/redefine/; | |
local *node = $node_sub; | |
use warnings qw/redefine/; | |
$code->(); | |
} | |
return Node->new(value => $value, children => \@children); | |
} | |
}; | |
my $tree = node 1, sub { | |
node 2, sub { | |
node 3, sub { | |
node 4; | |
node 5; | |
}; | |
node 6; | |
}; | |
node 7; | |
node 8, sub { | |
node 9, sub { | |
node 10; | |
node 11; | |
}; | |
node 12; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment