TextMode/Loader.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 74 CC 5 SCAR 27.7
CC
5
Cov
0%
load
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 16.0
1 package Image::TextMode::Loader;
2  
3 use strict;
4 use warnings;
5  
6 use Module::Pluggable::Object;
7 use Image::TextMode::SAUCE;
8  
9 use Carp 'croak';
10  
11 sub load {
12 my ( $self, @files ) = @_;
13 my @result;
14  
15 my $base = 'Image::TextMode::Format';
16 my $default = 'Image::TextMode::Format::ANSI';
17 my $finder = Module::Pluggable::Object->new(
18 search_path => [ $base ],
19 require => 1
20 );
21  
22 my %exts;
23 for my $format ( $finder->plugins ) {
24 $exts{ $_ } = $format for $format->extensions;
25 }
26  
27 for my $file ( @files ) {
28 my $read_options = {};
29 if ( ref $file ) {
30 ( $file, $read_options ) = @$file;
31 }
32  
33 my ( $ext ) = $file =~ m{([^.]+?)$}s;
34 $ext = lc $ext;
35 my $format = $exts{ $ext } || $default;
36  
37 # if we get ANSI, we need to see if it's ansimation or not from the SAUCE data.
38 if ( $format eq $default ) {
39 my $sauce = Image::TextMode::SAUCE->new;
40 open( my $fh, '<', $file )
41 or croak "Unable to read SAUCE data for '$file': $!";
42 $sauce->read( $fh );
43 close( $fh ) or croak "Unable to close '$file': $!";
44 if ( $sauce->has_sauce
45 && $sauce->datatype
46 && $sauce->filetype
47 && $sauce->filetype eq 'ANSiMation' )
48 {
49 $format = 'Image::TextMode::Format::ANSIMation';
50 }
51 }
52  
53 my $image = $format->new;
54 $image->read( $file, $read_options );
55 push @result, $image;
56 }
57  
58 return wantarray ? @result : $result[ 0 ];
59 }
60  
61 =head1 NAME
62  
63 Image::TextMode::Loader - Load text mode images by best-guess
64  
65 =head1 SYNOPSIS
66  
67 my $img = Image::TextMode::Loader->load( $filename );
68  
69 =head1 DESCRIPTION
70  
71 This module allows you to load a set of images without having to explicitly
72 specify the format before-hand.
73  
74 =head1 METHODS
75  
76 =head2 load( @files )
77  
78 Attempts to load C<@files> based on some filetype guessing. Returns the first
79 result in scalar context.
80  
81 =head1 AUTHOR
82  
83 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
84  
85 =head1 COPYRIGHT AND LICENSE
86  
87 Copyright 2008-2014 by Brian Cassidy
88  
89 This library is free software; you can redistribute it and/or modify
90 it under the same terms as Perl itself.
91  
92 =cut
93  
94 1;