File Coverage

blib/lib/Erlang/Parser/Node/Comprehension.pm
Criterion Covered Total %
statement 16 16 100.0
branch 6 6 100.0
condition 1 2 50.0
subroutine 2 2 100.0
pod 1 1 100.0
total 26 27 96.3


line stmt bran cond sub pod time code
1             # Copyright 2011-2012 Yuki Izumi. ( anneli AT cpan DOT org )
2             # This is free software; you can redistribute it and/or modify it under the
3             # same terms as Perl itself.
4              
5             package Erlang::Parser::Node::Comprehension;
6              
7 3     3   12 use Moose;
  3         4  
  3         15  
8             with 'Erlang::Parser::Node';
9              
10             has 'binary' => (is => 'rw', default => 0, isa => 'Bool');
11             has 'output' => (is => 'rw', required => 1, isa => 'Erlang::Parser::Node');
12             has 'generators' => (is => 'rw', required => 1, isa => 'ArrayRef[Erlang::Parser::Node]');
13              
14             sub print {
15 54     54 1 119 my ($self, $fh, $depth) = @_;
16 54   50     138 $depth ||= 0;
17              
18 54 100       1561 print $fh $self->binary ? '<<' : '[';
19 54         1553 $self->output->print($fh, $depth);
20 54         96 print $fh ' || ';
21            
22 54         60 my $first = 1;
23 54         48 foreach (@{$self->generators}) {
  54         1524  
24 66 100       143 if ($first) { $first = 0 } else { print $fh ', ' }
  54         72  
  12         24  
25 66         227 $_->print($fh, $depth);
26             }
27 54 100       1408 print $fh $self->binary ? '>>' : ']';
28             }
29              
30             __PACKAGE__->meta->make_immutable;
31              
32             =head1 NAME
33              
34             Erlang::Parser::Node::Comprehension - a list or binary comprehension
35              
36             =head1 DESCRIPTION
37              
38             Used to generate (binary) lists/strings by a combination of generators, guards
39             and output expressions.
40              
41             =head2 Accessors
42              
43             =over 4
44              
45             =item C<binary>
46              
47             True if this is a binary comprehension.
48              
49             =item C<output>
50              
51             The L<Erlang::Parser::Node> which forms the output elements based on
52             C<generators>.
53              
54             =item C<generators>
55              
56             A mixture of generators (in the form C<<X <- Y>> or C<<X <= Y>>) and guards
57             which create the terms used by C<output>.
58              
59             =back
60              
61             =head2 Methods
62              
63             =over 4
64              
65             =item C<print>
66              
67             Pretty-prints the node to its filehandle argument.
68              
69             =back
70              
71             =head1 EXAMPLE
72              
73             [X + Y || X <- [1, 2, 3], Y <- [1, 2, 3], X + Y > 2]
74              
75             =cut
76              
77             1;
78              
79             # vim: set sw=4 ts=4: