1 |
package Frey::Pipe; |
2 |
use Moose; |
3 |
|
4 |
=head1 DESCRIPTION |
5 |
|
6 |
Shell pipes for structured data |
7 |
|
8 |
=head1 EXAMPLE |
9 |
|
10 |
Frey::Feed/data + uri=https://blog.rot13.org/index.xml | Frey::Dumper/markup |
11 |
|
12 |
this will produce following code: |
13 |
|
14 |
Frey::Dumper->new( data => |
15 |
Frey::Feed->new( uri => 'https://blog.rot13.org/index.xml' )->data |
16 |
)->markup |
17 |
|
18 |
=cut |
19 |
|
20 |
has pipe => ( |
21 |
is => 'rw', |
22 |
required => 1, |
23 |
default => 'Frey::Feed/data+uri=https://blog.rot13.org/index.xml|Frey::Dumper/markup', |
24 |
); |
25 |
|
26 |
use Data::Dump qw/dump/; |
27 |
|
28 |
sub markup { |
29 |
my ($self) = @_; |
30 |
|
31 |
my $out; |
32 |
my $pipe; |
33 |
|
34 |
foreach my $part ( split(/\|/, $self->pipe ) ) { |
35 |
warn "# part: '$part'"; |
36 |
if ( $part =~ m{^([^/]+)/([^\+]+)(\+.*)?$} ) { |
37 |
my ( $class, $method, $args ) = ( $1, $2, $3 ); |
38 |
my $params = $pipe; |
39 |
if ( defined $args ) { |
40 |
$args =~ s/^\+//; |
41 |
warn "# class $class method $method args $args", $pipe ? " pipe args " . join(',',keys %$pipe) : ''; |
42 |
map { |
43 |
my ( $name, $value ) = ( $1, $2 ) if m{^([^=]+)=(.+)$}; |
44 |
$params->{$name} = $value; |
45 |
} split(/[\s\+]/, $args) |
46 |
} |
47 |
my $code = '$result = ' . $class . '->new' . dump( %$params ) . '->' . $method . '();'; |
48 |
warn "# pipe $part -> $code"; |
49 |
my $result; |
50 |
eval $code; |
51 |
die $@ if $@; |
52 |
warn "# result ",ref( $result ); |
53 |
$out .= qq{<code>$part</code>} . dump( $result ) . '<hr>'; |
54 |
$pipe = { $method => $result }; |
55 |
} else { |
56 |
die "don't know what to do with '$part' from ",$self->pipe; |
57 |
} |
58 |
} |
59 |
|
60 |
return $out; |
61 |
} |
62 |
|
63 |
1; |