xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Harness/lib/TAP/Object.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b39c5158Smillertpackage TAP::Object;
2b39c5158Smillert
3b39c5158Smillertuse strict;
46fb12b70Safresh1use warnings;
5b39c5158Smillert
6b39c5158Smillert=head1 NAME
7b39c5158Smillert
8b39c5158SmillertTAP::Object - Base class that provides common functionality to all C<TAP::*> modules
9b39c5158Smillert
10b39c5158Smillert=head1 VERSION
11b39c5158Smillert
12*3d61058aSafresh1Version 3.48
13b39c5158Smillert
14b39c5158Smillert=cut
15b39c5158Smillert
16*3d61058aSafresh1our $VERSION = '3.48';
17b39c5158Smillert
18b39c5158Smillert=head1 SYNOPSIS
19b39c5158Smillert
20b39c5158Smillert    package TAP::Whatever;
21b39c5158Smillert
22b39c5158Smillert    use strict;
23b39c5158Smillert
246fb12b70Safresh1    use base 'TAP::Object';
25b39c5158Smillert
26b39c5158Smillert    # new() implementation by TAP::Object
27b39c5158Smillert    sub _initialize {
28b39c5158Smillert        my ( $self, @args) = @_;
29b39c5158Smillert        # initialize your object
30b39c5158Smillert        return $self;
31b39c5158Smillert    }
32b39c5158Smillert
33b39c5158Smillert    # ... later ...
34b39c5158Smillert    my $obj = TAP::Whatever->new(@args);
35b39c5158Smillert
36b39c5158Smillert=head1 DESCRIPTION
37b39c5158Smillert
38b39c5158SmillertC<TAP::Object> provides a default constructor and exception model for all
39b39c5158SmillertC<TAP::*> classes.  Exceptions are raised using L<Carp>.
40b39c5158Smillert
41b39c5158Smillert=head1 METHODS
42b39c5158Smillert
43b39c5158Smillert=head2 Class Methods
44b39c5158Smillert
45b39c5158Smillert=head3 C<new>
46b39c5158Smillert
47b39c5158SmillertCreate a new object.  Any arguments passed to C<new> will be passed on to the
48b39c5158SmillertL</_initialize> method.  Returns a new object.
49b39c5158Smillert
50b39c5158Smillert=cut
51b39c5158Smillert
52b39c5158Smillertsub new {
53b39c5158Smillert    my $class = shift;
54b39c5158Smillert    my $self = bless {}, $class;
55b39c5158Smillert    return $self->_initialize(@_);
56b39c5158Smillert}
57b39c5158Smillert
58b39c5158Smillert=head2 Instance Methods
59b39c5158Smillert
60b39c5158Smillert=head3 C<_initialize>
61b39c5158Smillert
62b39c5158SmillertInitializes a new object.  This method is a stub by default, you should override
63b39c5158Smillertit as appropriate.
64b39c5158Smillert
65b39c5158SmillertI<Note:> L</new> expects you to return C<$self> or raise an exception.  See
66b39c5158SmillertL</_croak>, and L<Carp>.
67b39c5158Smillert
68b39c5158Smillert=cut
69b39c5158Smillert
70b39c5158Smillertsub _initialize {
71b39c5158Smillert    return $_[0];
72b39c5158Smillert}
73b39c5158Smillert
74b39c5158Smillert=head3 C<_croak>
75b39c5158Smillert
76b39c5158SmillertRaise an exception using C<croak> from L<Carp>, eg:
77b39c5158Smillert
78b39c5158Smillert    $self->_croak( 'why me?', 'aaarrgh!' );
79b39c5158Smillert
80b39c5158SmillertMay also be called as a I<class> method.
81b39c5158Smillert
82b39c5158Smillert    $class->_croak( 'this works too' );
83b39c5158Smillert
84b39c5158Smillert=cut
85b39c5158Smillert
86b39c5158Smillertsub _croak {
87b39c5158Smillert    my $proto = shift;
88b39c5158Smillert    require Carp;
89b39c5158Smillert    Carp::croak(@_);
90b39c5158Smillert    return;
91b39c5158Smillert}
92b39c5158Smillert
93898184e3Ssthen=head3 C<_confess>
94898184e3Ssthen
95898184e3SsthenRaise an exception using C<confess> from L<Carp>, eg:
96898184e3Ssthen
97898184e3Ssthen    $self->_confess( 'why me?', 'aaarrgh!' );
98898184e3Ssthen
99898184e3SsthenMay also be called as a I<class> method.
100898184e3Ssthen
101898184e3Ssthen    $class->_confess( 'this works too' );
102898184e3Ssthen
103898184e3Ssthen=cut
104898184e3Ssthen
105898184e3Ssthensub _confess {
106898184e3Ssthen    my $proto = shift;
107898184e3Ssthen    require Carp;
108898184e3Ssthen    Carp::confess(@_);
109898184e3Ssthen    return;
110898184e3Ssthen}
111898184e3Ssthen
112b39c5158Smillert=head3 C<_construct>
113b39c5158Smillert
114b39c5158SmillertCreate a new instance of the specified class.
115b39c5158Smillert
116b39c5158Smillert=cut
117b39c5158Smillert
118b39c5158Smillertsub _construct {
119b39c5158Smillert    my ( $self, $class, @args ) = @_;
120b39c5158Smillert
121b39c5158Smillert    $self->_croak("Bad module name $class")
122b39c5158Smillert      unless $class =~ /^ \w+ (?: :: \w+ ) *$/x;
123b39c5158Smillert
124b39c5158Smillert    unless ( $class->can('new') ) {
125b39c5158Smillert        local $@;
126b39c5158Smillert        eval "require $class";
1276fb12b70Safresh1        $self->_croak("Can't load $class: $@") if $@;
128b39c5158Smillert    }
129b39c5158Smillert
130b39c5158Smillert    return $class->new(@args);
131b39c5158Smillert}
132b39c5158Smillert
133b39c5158Smillert=head3 C<mk_methods>
134b39c5158Smillert
135b39c5158SmillertCreate simple getter/setters.
136b39c5158Smillert
137b39c5158Smillert __PACKAGE__->mk_methods(@method_names);
138b39c5158Smillert
139b39c5158Smillert=cut
140b39c5158Smillert
141b39c5158Smillertsub mk_methods {
142b39c5158Smillert    my ( $class, @methods ) = @_;
143898184e3Ssthen    for my $method_name (@methods) {
144b39c5158Smillert        my $method = "${class}::$method_name";
145b39c5158Smillert        no strict 'refs';
146b39c5158Smillert        *$method = sub {
147b39c5158Smillert            my $self = shift;
148b39c5158Smillert            $self->{$method_name} = shift if @_;
149b39c5158Smillert            return $self->{$method_name};
150b39c5158Smillert        };
151b39c5158Smillert    }
152b39c5158Smillert}
153b39c5158Smillert
154b39c5158Smillert1;
155b39c5158Smillert
156