package TAP::Parser::SourceHandler; use strict; use warnings; use TAP::Parser::Iterator (); use base 'TAP::Object'; =head1 NAME TAP::Parser::SourceHandler - Base class for different TAP source handlers =head1 VERSION Version 3.42 =cut our $VERSION = '3.42'; =head1 SYNOPSIS # abstract class - don't use directly! # see TAP::Parser::IteratorFactory for general usage # must be sub-classed for use package MySourceHandler; use base 'TAP::Parser::SourceHandler'; sub can_handle { return $confidence_level } sub make_iterator { return $iterator } # see example below for more details =head1 DESCRIPTION This is an abstract base class for L handlers / handlers. A C does whatever is necessary to produce & capture a stream of TAP from the I source, and package it up in a L for the parser to consume. C must implement the I interface used by L. At 2 methods, the interface is pretty simple: L and L. Unless you're writing a new L, a plugin, or subclassing L, you probably won't need to use this module directly. =head1 METHODS =head2 Class Methods =head3 C I. my $vote = $class->can_handle( $source ); C<$source> is a L. Returns a number between C<0> & C<1> reflecting how confidently the raw source can be handled. For example, C<0> means the source cannot handle it, C<0.5> means it may be able to, and C<1> means it definitely can. See L for details on how this is used. =cut sub can_handle { my ( $class, $args ) = @_; $class->_croak( "Abstract method 'can_handle' not implemented for $class!"); return; } =head3 C I. my $iterator = $class->make_iterator( $source ); C<$source> is a L. Returns a new L object for use by the L. Cs on error. =cut sub make_iterator { my ( $class, $args ) = @_; $class->_croak( "Abstract method 'make_iterator' not implemented for $class!"); return; } 1; __END__ =head1 SUBCLASSING Please see L for a subclassing overview, and any of the subclasses that ship with this module as an example. What follows is a quick overview. Start by familiarizing yourself with L and L. L is the easiest sub-class to use as an example. It's important to point out that if you want your subclass to be automatically used by L you'll have to and make sure it gets loaded somehow. If you're using L you can write an L plugin. If you're using L or L directly (e.g. through a custom script, L, or L) you can use the C option which will cause L to load your subclass). Don't forget to register your class with L. =head2 Example package MySourceHandler; use strict; use MySourceHandler; # see TAP::Parser::SourceHandler use TAP::Parser::IteratorFactory; use base 'TAP::Parser::SourceHandler'; TAP::Parser::IteratorFactory->register_handler( __PACKAGE__ ); sub can_handle { my ( $class, $src ) = @_; my $meta = $src->meta; my $config = $src->config_for( $class ); if ($config->{accept_all}) { return 1.0; } elsif (my $file = $meta->{file}) { return 0.0 unless $file->{exists}; return 1.0 if $file->{lc_ext} eq '.tap'; return 0.9 if $file->{shebang} && $file->{shebang} =~ /^#!.+tap/; return 0.5 if $file->{text}; return 0.1 if $file->{binary}; } elsif ($meta->{scalar}) { return 0.8 if $$raw_source_ref =~ /\d\.\.\d/; return 0.6 if $meta->{has_newlines}; } elsif ($meta->{array}) { return 0.8 if $meta->{size} < 5; return 0.6 if $raw_source_ref->[0] =~ /foo/; return 0.5; } elsif ($meta->{hash}) { return 0.6 if $raw_source_ref->{foo}; return 0.2; } return 0; } sub make_iterator { my ($class, $source) = @_; # this is where you manipulate the source and # capture the stream of TAP in an iterator # either pick a TAP::Parser::Iterator::* or write your own... my $iterator = TAP::Parser::Iterator::Array->new([ 'foo', 'bar' ]); return $iterator; } 1; =head1 AUTHORS TAPx Developers. Source detection stuff added by Steve Purkis =head1 SEE ALSO L, L, L, L, L, L, L, L, L, L =cut