File Coverage

blib/lib/Array/Autojoin.pm
Criterion Covered Total %
statement 21 23 91.3
branch 3 4 75.0
condition 2 2 100.0
subroutine 8 8 100.0
pod 0 1 0.0
total 34 38 89.4


line stmt bran cond sub pod time code
1              
2             require 5;
3             package Array::Autojoin;
4 2     2   6032 use strict;
  2         5  
  2         91  
5 2     2   9 use vars qw($VERSION @ISA @EXPORT);
  2         4  
  2         577  
6             # Time-stamp: "2004-12-29 19:47:36 AST"
7             $VERSION = '0.03';
8             require Exporter;
9             @ISA = ('Exporter');
10             @EXPORT = ('mkarray');
11              
12 3     3 0 177 sub mkarray { bless [@_], 'Array::Autojoin::X' }
13              
14             {
15             # And then the class for that:
16              
17             package Array::Autojoin::X;
18              
19             # TODO: Hmm, methods for *=, +=, etc?
20              
21             use overload(
22              
23 8     8   102 '""' => sub { join ', ', @{$_[0]}},
  8         39  
24              
25 8   100 8   59 '0+' => sub {0 + ( $_[0][0] || 0 ) },
26             # stringifies and then just numerifies, but feh.
27              
28             'fallback' => 1, # turn on cleverness
29              
30             'bool', => sub { # true iff there's any true items in it
31 3 100   3   30 for (@{$_[0]}) { return 1 if $_ };
  3         8  
  5         17  
32 1         3 return '';
33             },
34              
35             '.=' => sub { # sure, why not.
36 1 50   1   2 if(@{$_[0]}) { $_[0][-1] .= $_[1] } else { push @{$_[0]}, $_[1] }
  1         4  
  1         3  
  0         0  
  0         0  
37 1         2 $_[0];
38             }, # but can't overload ||= or the like
39              
40 2     2   3269 );
  2         2192  
  2         28  
41             }
42              
43             1;
44              
45             __END__