| 1 | 115 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 115 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 115 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package LINQ; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 115 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||
| 8 | 115 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 115 | ▶ | use Exporter::Shiny qw( LINQ Range Repeat END ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 115 | our $FORCE_ITERATOR; | |||||||||||||||||||||||||||
| 13 | 115 | our $IN_LOOP; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 115 | my $end = do { | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | package LINQ::END; | ||||||||||||||||||||||||||||
| 18 | 115 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||
| 19 | 115 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||
| 20 | 115 | my $x = 42; | |||||||||||||||||||||||||||
| 21 | 115 | bless( \$x ); | |||||||||||||||||||||||||||
| 22 | 115 | &Internals::SvREADONLY( \$x, !!1 ); | |||||||||||||||||||||||||||
| 23 | 115 | \$x; | |||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 115 | my $last = do { | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | package LINQ::LAST; | ||||||||||||||||||||||||||||
| 29 | 115 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||
| 30 | 115 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||
| 31 | 115 | my $x = 666; | |||||||||||||||||||||||||||
| 32 | 115 | bless( \$x ); | |||||||||||||||||||||||||||
| 33 | 115 | &Internals::SvREADONLY( \$x, !!1 ); | |||||||||||||||||||||||||||
| 34 | 115 | \$x; | |||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | BEGIN { | ||||||||||||||||||||||||||||
| 38 | 115 | ▶ | *LINQ::END = sub () { $end }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | *LINQ::LAST = sub () { | ||||||||||||||||||||||||||||
| 41 | 6 | ▶ | if ( $IN_LOOP ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 3 | die( $last ); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 3 | require LINQ::Exception; | |||||||||||||||||||||||||||
| 45 | 3 | 'LINQ::Exception::CallerError'->throw( | |||||||||||||||||||||||||||
| 46 | message => "Cannot call LINQ::LAST outside foreach", | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | 115 | }; | |||||||||||||||||||||||||||
| 49 | } #/ BEGIN | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub LINQ ($) { | ||||||||||||||||||||||||||||
| 52 | 784 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
LINQ: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 784 | my $ref = ref( $data ); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 784 | ▶ | if ( $ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 469 | ▶ | if ( $FORCE_ITERATOR ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 216 | my @data = @$data; | |||||||||||||||||||||||||||
| 58 | 216 | require LINQ::Iterator; | |||||||||||||||||||||||||||
| 59 | 216 | ▶ | return LINQ::Iterator::->new( sub { @data ? shift( @data ) : LINQ::END } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 253 | require LINQ::Array; | |||||||||||||||||||||||||||
| 63 | 253 | return LINQ::Array::->new( $data ); | |||||||||||||||||||||||||||
| 64 | } #/ if ( $ref eq 'ARRAY' ) | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 315 | ▶ | if ( $ref eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 312 | require LINQ::Iterator; | |||||||||||||||||||||||||||
| 68 | 312 | return LINQ::Iterator::->new( $data ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | require Scalar::Util; | |||||||||||||||||||||||||||
| 72 | 3 | ▶ | if ( Scalar::Util::blessed( $data ) and $data->DOES( 'LINQ::Collection' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($data) and $data->DOES("LINQ::Collection")
MC/DC100%2/2
builtin::blessed($data) and $data->DOES("LINQ::Collection")
builtin::blessed($data)
$data->DOES("LINQ::Collection")
Truth table100%3/3
A and B
A= builtin::blessed($data)B= $data->DOES("LINQ::Collection")
| |||||||||||||||||||||||||||||
| 73 | 1 | return $data; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | require LINQ::Exception; | |||||||||||||||||||||||||||
| 77 | 2 | 'LINQ::Exception::CallerError'->throw( | |||||||||||||||||||||||||||
| 78 | message => "Cannot create LINQ object from '$data'", | ||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||
| 80 | } #/ sub LINQ ($) | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub Range { | ||||||||||||||||||||||||||||
| 83 | 7 | ▶ | my ( $min, $max ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
Range: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 7 | ▶ | my $value = defined( $min ) ? $min : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 7 | ▶ | if ( not defined $max ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | return LINQ sub { $value++ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 5 | ▶ | return LINQ sub { return LINQ::END if $value > $max; $value++ }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } #/ sub Range | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub Repeat { | ||||||||||||||||||||||||||||
| 95 | 6 | ▶ | my ( $value, $count ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
Repeat: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 6 | ▶ | if ( not defined $count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 5 | ▶ | return LINQ sub { $value }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | ▶ | return LINQ sub { return LINQ::END if $count-- <= 0; $value }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 115 | 1; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =pod | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 NAME | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | LINQ - an interpretation of Microsoft's Language Integrated Query | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | use feature qw( say ); | ||||||||||||||||||||||||||||
| 119 | use LINQ qw( LINQ )'; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | my $double_even_numbers = | ||||||||||||||||||||||||||||
| 122 | LINQ( [ 1 .. 100 ] ) | ||||||||||||||||||||||||||||
| 123 | ->where( sub { $_ % 2 == 0 } ) | ||||||||||||||||||||||||||||
| 124 | ->select( sub { $_ * 2 } ); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | for my $n ( $double_even_numbers->to_list ) { | ||||||||||||||||||||||||||||
| 127 | say $n; | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | LINQ is basically an application of SQL concepts to arrays and iterators. | ||||||||||||||||||||||||||||
| 133 | Hopefully this implementation will eventually cover other data types like | ||||||||||||||||||||||||||||
| 134 | SQL tables, XML and JSON data, etc. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Not much is documented yet, but the test suite includes numerous examples | ||||||||||||||||||||||||||||
| 137 | of LINQ's usage. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | The C<LINQ>, C<Range>, and C<Repeat> functions return LINQ collections, | ||||||||||||||||||||||||||||
| 142 | objects implementing the L<LINQ::Collection> interface. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | The C<< LINQ::END() >> and C<< LINQ::LAST() >> functions are used as | ||||||||||||||||||||||||||||
| 145 | signals to control LINQ's iterators and loops. | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Additional utility functions can be found in L<LINQ::Util>. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =over | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item C<< LINQ( SOURCE ) >> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Creates a LINQ collection from a source. The source may be an existing LINQ | ||||||||||||||||||||||||||||
| 154 | collection, which will be returned as-is, an arrayref of items, or a coderef | ||||||||||||||||||||||||||||
| 155 | which will be called in scalar context and expected to return a single item | ||||||||||||||||||||||||||||
| 156 | each time it is called. It should return the special value C<< LINQ::END() >> | ||||||||||||||||||||||||||||
| 157 | to indicate that the end of the collection has been reached. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | C<LINQ> may be exported, but is not exported by default. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =item C<< Range( MIN, MAX ) >> | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Returns a LINQ collection containing the range of numbers from MIN to MAX. | ||||||||||||||||||||||||||||
| 164 | If MIN is undef, it is treated as 0. If MAX is undef, it is treated as positive | ||||||||||||||||||||||||||||
| 165 | infinity. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | If you want a range from 0 to negative infinity, use: | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | my $below_zero = Range( 0, undef )->select( sub { -$_ } ); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | C<Range> may be exported, but is not exported by default. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =item C<< Repeat( VALUE, COUNT ) >> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Returns a LINQ collection containing the same value multiple times. If COUNT | ||||||||||||||||||||||||||||
| 176 | is undef, then it is treated as infinity. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | C<Repeat> may be exported, but is not exported by default. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =item C<< END() >> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Returns the special value C<< LINQ::END() >>. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | C<END> may be exported, but is not exported by default, and I recommend | ||||||||||||||||||||||||||||
| 185 | calling it by its fully qualified name for clarity. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item C<< LAST() >> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Used by the C<foreach> method of L<LINQ::Collection>. If called otherwise, | ||||||||||||||||||||||||||||
| 190 | will die. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =back | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 HISTORY | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | I wrote this back in 2014, but never released it. After a discussion | ||||||||||||||||||||||||||||
| 197 | about how nice it would be to have a programming language which used SQL | ||||||||||||||||||||||||||||
| 198 | concepts natively, eliminating the need to "map" between how your | ||||||||||||||||||||||||||||
| 199 | application handled data and how your database handled data, I remembered | ||||||||||||||||||||||||||||
| 200 | this. So I thought I'd push what I had so far onto CPAN and maybe think | ||||||||||||||||||||||||||||
| 201 | about reviving it. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 BUGS | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Please report any bugs to | ||||||||||||||||||||||||||||
| 206 | L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ>. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | L<LINQ::Collection>, L<LINQ::Util>, L<LINQ::Exception>. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | L<https://en.wikipedia.org/wiki/Language_Integrated_Query> | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Toby Inkster E<lt>tobyink@cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | This software is copyright (c) 2014, 2021 by Toby Inkster. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 223 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head1 DISCLAIMER OF WARRANTIES | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||||||||||||||||||||||||||||
| 228 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||||||||||||||||||||||||||||
| 229 | MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||||||||||||||||||