| 1 | package Perl6::Feeds; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | require 5.009_005; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Filter::Simple; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use re 'eval'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.20'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | FILTER_ONLY code => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | s/(?<!\$)#.*//g; | |||||||||||||||||||||||||||||||||||||||||
| 11 | my $nested = qr/ ( | ||||||||||||||||||||||||||||||||||||||||||
| 12 | ( [{[(] ) | ||||||||||||||||||||||||||||||||||||||||||
| 13 | (?: [^{}[\]()]++ | (?-2) )* | ||||||||||||||||||||||||||||||||||||||||||
| 14 | (??{ '\\'.{qw'{ } [ ] ( )'}->{$^N} }) | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ) /x; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | my $expression = qr/ (?: $nested | [^;{}[\]()] )+? /x; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | 1 while s{ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | (?: ^ | (?<= [\{[(;] ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 19 | \s* | ||||||||||||||||||||||||||||||||||||||||||
| 20 | (?<source> $expression ) | ||||||||||||||||||||||||||||||||||||||||||
| 21 | \s* ==+>(?<push> >?+ )(?<merge> [<>]?+ ) \s* | ||||||||||||||||||||||||||||||||||||||||||
| 22 | (?<action> $expression ) | ||||||||||||||||||||||||||||||||||||||||||
| 23 | \s* | ||||||||||||||||||||||||||||||||||||||||||
| 24 | (?= ==+>+ | [\}\]);] | $ ) | ||||||||||||||||||||||||||||||||||||||||||
| 25 | }{ | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 21 | 'sub{@_}->(' . do { | |||||||||||||||||||||||||||||||||||||||||
| 27 | 21 | my ($source, $action) = @+{qw/source action/}; | |||||||||||||||||||||||||||||||||||||||||
| 28 | $+{merge} eq '<' and "($action), ($source)" or | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 21 | ▶ | $+{merge} eq '>' and "($source), ($action)" or do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition90%9/10
$+{'merge'} eq '<' && "($action), ($source)" || $+{'merge'} eq '>' && "($source), ($action)" || do {
foreach $_ ($action) {
$action = "push $_,", last if $+{'push'};
$action .= '=', last if /^(?:(?:my|our|local)\W|[$@%&*])/;
$source = ",$source" unless /^\w+\s*(?:(?={)$nested)?$/;
$source .= ')' if s/\)$//;
};
"$action $source"
}
$+{"merge"} eq "<" and "($action), ($source)" or $+{"merge"} eq ">" and "($source), ($action)"
$+{"merge"} eq "<" and "($action), ($source)"
$+{"merge"} eq ">" and "($source), ($action)"
MC/DC0%0/3
$+{'merge'} eq '<' && "($action), ($source)" || $+{'merge'} eq '>' && "($source), ($action)" || do {
foreach $_ ($action) {
$action = "push $_,", last if $+{'push'};
$action .= '=', last if /^(?:(?:my|our|local)\W|[$@%&*])/;
$source = ",$source" unless /^\w+\s*(?:(?={)$nested)?$/;
$source .= ')' if s/\)$//;
};
"$action $source"
}
$+{"merge"} eq "<"
$+{"merge"} eq ">"
do {
foreach $_ ($action) {
$action = "push $_,", last if $+{'push'};
$action .= '=', last if /^(?:(?:my|our|local)\W|[$@%&*])/;
$source = ",$source" unless /^\w+\s*(?:(?={)$nested)?$/;
$source .= ')' if s/\)$//;
};
"$action $source"
}
Truth table75%3/4
A or B || C
A= $+{"merge"} eq "<"B= $+{"merge"} eq ">"C= do {
foreach $_ ($action) {
$action = "push $_,", last if $+{'push'};
$action .= '=', last if /^(?:(?:my|our|local)\W|[$@%&*])/;
$source = ",$source" unless /^\w+\s*(?:(?={)$nested)?$/;
$source .= ')' if s/\)$//;
};
"$action $source"
}
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 17 | for ($action) { | |||||||||||||||||||||||||||||||||||||||||
| 31 | 17 | ▶ | $action = "push $_,", last if $+{push}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 16 | ▶ | $action .= '=', last if /^(?:(?:my|our|local)\W|[$@%&*])/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 12 | ▶ | $source = ",$source" unless /^\w+\s*(?:(?=\{)$nested)?$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 12 | ▶ | $source .= ')' if s/\)$//; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | "$action $source" | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 17 | } | |||||||||||||||||||||||||||||||||||||||||
| 38 | } . ')' | ||||||||||||||||||||||||||||||||||||||||||
| 39 | }xse | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Perl6::Feeds - implements perl6 feed operators in perl5 via source filtering | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | version 0.20 | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | requires perl version 5.9.5 or higher | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | this code is currently in beta, bug reports welcome | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | feed operators allow you to write expressions that flow left to right and top down, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | rather than the right to left, bottom up order imposed by function nesting. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | use Perl6::Feeds; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1..10 ==> map {$_**2} ==> grep {$_>10} ==> join " " ==> print; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # is the same as | ||||||||||||||||||||||||||||||||||||||||||
| 63 | print join " " => grep {$_>10} map {$_**2} 1..10; | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 .. 3000 | ||||||||||||||||||||||||||||||||||||||||||
| 66 | ==> map [$_, $_ ** 2 ] | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ==> grep {$$_[1] =~ s/(([^0])\2{3,})/ ($1) /g} | ||||||||||||||||||||||||||||||||||||||||||
| 68 | ==> our @list # assignments start with /my|our|local|[$@%&*]/ | ||||||||||||||||||||||||||||||||||||||||||
| 69 | ==> map {@$_ ==> map "[$_]" # nesting is fine | ||||||||||||||||||||||||||||||||||||||||||
| 70 | ==> join '^2 ==> '} # strings are safe | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ==>>> 'found '.@list.' numbers: ' # appends a list | ||||||||||||||||||||||||||||||||||||||||||
| 72 | ==>>< "\nnumbers with squares that ". # prepends a list | ||||||||||||||||||||||||||||||||||||||||||
| 73 | "have non zero runs of 4+ digits:\n" # both of these are not in the perl6 spec | ||||||||||||||||||||||||||||||||||||||||||
| 74 | ==> join ("\n") # closed argument lists are adjusted | ||||||||||||||||||||||||||||||||||||||||||
| 75 | ==>>> (@list ==> map $$_[0] ==> join ' ') | ||||||||||||||||||||||||||||||||||||||||||
| 76 | =====> print; # feed arrows match /==+>[<>]?/ | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =head1 SYNTAX | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 .. 10 ==> join ", " ==> print; # print join ", " => 1 .. 10; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 .. 10 ==> my @a; # my @a = 1 .. 10; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 20 .. 30 ==>> @a; # push @a, 20 .. 30; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | @a ==>>> "appended" ==> print; # print @a, "appended"; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | @a ==>>< "prepended" ==> print; # print "prepended", @a; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | Eric Strom, C<< <ejstrom at gmail.com> >> | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | currently, only left to right feeds are supported, and there may be a few corner cases that | ||||||||||||||||||||||||||||||||||||||||||
| 97 | the filter will fail on. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | bug reports or patches welcome, send them to C<bug-perl6-feeds at rt.cpan.org>, or through the | ||||||||||||||||||||||||||||||||||||||||||
| 100 | web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Perl6-Feeds>. I will be | ||||||||||||||||||||||||||||||||||||||||||
| 101 | notified, and then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | the perl6 synopsi | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | copyright 2009 Eric Strom. | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | this program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 112 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||
| 113 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | see http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | 1; # End of Perl6::Feeds | |||||||||||||||||||||||||||||||||||||||||