1package FormValidator::Lite::Upload; 2use strict; 3use warnings; 4use Carp (); 5use Scalar::Util qw/blessed/; 6 7{ 8 my %cache; 9 sub _load { 10 my $pkg = shift; 11 unless ($cache{$pkg}++) { 12 eval "use $pkg"; ## no critic 13 Carp::croak($@) if $@; 14 } 15 $pkg; 16 } 17} 18 19 20sub new { 21 my ($self, $q, $name) = @_; 22 Carp::croak("missing parameter \$name") unless $name; 23 Carp::croak("\$q is not a object") unless blessed $q; 24 25 my $pkg = do { 26 if ($q->isa('CGI')) { 27 'CGI'; 28 } elsif ($q->isa('HTTP::Engine::Request')) { 29 'HTTPEngine'; 30 } elsif ($q->isa('Plack::Request')) { 31 'PlackRequest'; 32 } else { 33 if ($q->can('upload')){ # duck typing 34 # this feature is needed by HTML::Shakan or other form validation libraries 35 return $q->upload($name); # special case :) 36 } else { 37 die "unknown request type: $q"; 38 } 39 } 40 }; 41 $pkg = 'FormValidator::Lite::Upload::' . $pkg; 42 43 _load($pkg)->new($q, $name); 44} 45 461; 47