File Coverage

blib/lib/Net/SSLeay/OO/Functions.pm
Criterion Covered Total %
statement 16 48 33.3
branch 4 24 16.6
condition 1 2 50.0
subroutine 3 5 60.0
pod n/a
total 24 79 30.3


line stmt bran cond sub pod time code
1              
2             package Net::SSLeay::OO::Functions;
3              
4 3     3   16 use Net::SSLeay;
  3         6  
  3         1037  
5              
6             my %prefixes = (
7             "" => "Net::SSLeay::OO::SSL",
8             BIO => "Net::SSLeay::OO::BIO",
9             CIPHER => "Net::SSLeay::OO::Cipher",
10             COMP => "Net::SSLeay::OO::Compression",
11             CTX => "Net::SSLeay::OO::Context",
12             DH => "Net::SSLeay::OO::KeyType::DH",
13             ENGINE => "Net::SSLeay::OO::Engine",
14             ERR => "Net::SSLeay::OO::Error",
15             EVP_PKEY => "Net::SSLeay::OO::PrivateKey",
16              
17             #MD2 => undef,
18             #MD4 => undef,
19             #MD5 => undef,
20             PEM => "Net::SSLeay::OO::PEM",
21              
22             #P_ASN1_UTCTIME => undef,
23             RAND => "Net::SSLeay::OO::PRNG",
24             RSA => "Net::SSLeay::OO::KeyType::RSA",
25             SESSION => "Net::SSLeay::OO::Session",
26              
27             #X509V3_EXT => undef,
28             X509_NAME => "Net::SSLeay::OO::X509::Name",
29             X509_STORE => "Net::SSLeay::OO::X509::Store",
30             X509_STORE_CTX => "Net::SSLeay::OO::X509::Context",
31             X509 => "Net::SSLeay::OO::X509",
32             );
33              
34             my %ready;
35              
36             while ( my ( $sym, $glob ) = each %Net::SSLeay:: ) {
37             my $display = $sym =~ /ERRZX/;
38             print STDERR "Considering $sym: " if $display;
39             my ( $sub_pkg, $method ) =
40             $sym =~ m{^(?:([A-Z][A-Z0-9]*(?:_[A-Z][A-Z0-9]*)*)_)?
41             ([a-z]\w+)$}x;
42             if ( !$method ) {
43             print STDERR "didn't match pattern, next\n" if $display;
44             next;
45             }
46 3     3   3250 use Data::Dumper;
  3         42191  
  3         2288  
47             if ( !*{"Net::SSLeay::$sym"}{CODE} ) {
48             print STDERR "not a func, next\n" if $display;
49             next;
50             }
51             if ( $method eq "new" ) {
52             print STDERR "it's 'new', next\n" if $display;
53             next;
54             }
55             my $pkg = $prefixes{ $sub_pkg || "" };
56             if ( !$pkg ) {
57             print STDERR "destination package undefined; next\n"
58             if $display;
59             next;
60             }
61             print STDERR " => belongs in $pkg as $method\n" if $display;
62             if ( *{$glob}{CODE} ) {
63             $ready{$pkg}{$method} = \&{*$glob};
64             }
65             else {
66             $ready{$pkg}{$method} = sub {
67             goto \&{"Net::SSLeay::$sym"};
68             };
69             }
70             }
71              
72             sub import {
73 4     4   1166 my $pkg = shift;
74 4         234 my $caller = caller;
75 4   50 0   43 my $install = shift || sub {shift};
  0            
76 4         12 my %opts = @_;
77 4         7 my %exclude;
78 4 50       21 if ( my $aref = $opts{-exclude} ) {
79 0 0       0 die "usage: -exclude => [qw( func1 func2 )]"
80             unless ref $aref eq "ARRAY";
81 0         0 %exclude = map { $_ => 1 } @$aref;
  0         0  
82             }
83 4         8 my %include;
84 4 50       17 if ( my $href = $opts{-include} ) {
85 0 0       0 die "usage: -include => { foo_func => 'methname' }"
86             unless ref $href eq "HASH";
87 0         0 %include = %$href;
88             }
89 4 50       17 if ( !ref $install ) {
90 0         0 my $att = $install;
91             $install = sub {
92 0     0   0 my $code = shift;
93 0         0 my $method = shift;
94             sub {
95 0         0 my $self = shift;
96 0         0 my @rv;
97 0 0       0 my $pointer = $self->$att
98             or die "no pointer in $self; this"
99             . " object may be being used outside of its valid lifetime";
100 0 0       0 if (wantarray) {
101 0         0 @rv = $code->( $pointer, @_ );
102             }
103             else {
104 0         0 $rv[0] = $code->( $pointer, @_ );
105             }
106 0         0 &Net::SSLeay::OO::Error::die_if_ssl_error(
107             $method);
108 0 0       0 wantarray ? @rv : $rv[0];
109 0         0 };
110 0         0 };
111             }
112 4 50       245 if ( my $table = delete $ready{$caller} ) {
113 0           while ( my ( $method, $code ) = each %$table ) {
114 0 0         next if $exclude{$method};
115 0           my $fullname = $caller . "::" . $method;
116 0 0         next if defined &{$fullname};
  0            
117 0           *{$fullname} = $install->( $code, $method );
  0            
118             }
119 0           while ( my ( $source, $dest ) = each %include ) {
120 0           my $fullname = $caller . "::" . $dest;
121 0           my $code = Net::SSLeay->can($source);
122 0 0         next unless $code;
123 0           *{$fullname} = $install->( $code, $dest );
  0            
124             }
125             }
126             }
127              
128             1;
129              
130             __END__
131              
132             =head1 NAME
133              
134             Net::SSLeay::OO::Functions - convert Net::SSLeay functions to methods
135              
136             =head1 SYNOPSIS
137              
138             use Net::SSLeay::OO::Functions 'foo';
139              
140             # means, roughly:
141             use Net::SSLeay::OO::Functions sub {
142             my $code = shift;
143             sub {
144             my $self = shift;
145             $code->($self->foo, @_);
146             }
147             };
148              
149             =head1 DESCRIPTION
150              
151             This internal utility module distributes Net::SSLeay functions into
152             the calling package. Its import method takes a callback which should
153             return a callback to be assigned into the symbol table; not providing
154             that will mean that the Net::SSLeay function is directly assigned into
155             the symbol table of the calling namespace.
156              
157             If a function is passed instead of a closure, it is taken to be the
158             name of an attribute which refers to where the Net::SSLeay magic
159             pointer is kept.
160              
161             The difference between the version of the installed handler function
162             and the actual installed function is that the real one checks for
163             OpenSSL errors which were raised while the function was called.
164              
165             After the first argument, options may be passed:
166              
167             =over
168              
169             =item B<-exclude => [qw(func1 func2)]>
170              
171             Specify NOT to include some functions that otherwise would be; perhaps
172             they won't work, perhaps they are badly named for their argument types.
173              
174             =item B<-include => { func_name => 'method_name'}>
175              
176             Import the L<Net::SSLeay> function called C<func_name>, as the local
177             method C<method_name>. This is mostly useful for functions which were
178             missing their prefix indicating the argument types.
179              
180             =back
181              
182             =head1 AUTHOR
183              
184             Sam Vilain, L<samv@cpan.org>
185              
186             =head1 COPYRIGHT
187              
188             Copyright (C) 2009 NZ Registry Services
189              
190             This program is free software: you can redistribute it and/or modify
191             it under the terms of the Artistic License 2.0 or later. You should
192             have received a copy of the Artistic License the file COPYING.txt. If
193             not, see <http://www.perlfoundation.org/artistic_license_2_0>
194              
195             =head1 SEE ALSO
196              
197             L<Net::SSLeay::OO>
198              
199             =cut
200              
201             # Local Variables:
202             # mode:cperl
203             # indent-tabs-mode: t
204             # cperl-continued-statement-offset: 8
205             # cperl-brace-offset: 0
206             # cperl-close-paren-offset: 0
207             # cperl-continued-brace-offset: 0
208             # cperl-continued-statement-offset: 8
209             # cperl-extra-newline-before-brace: nil
210             # cperl-indent-level: 8
211             # cperl-indent-parens-as-block: t
212             # cperl-indent-wrt-brace: nil
213             # cperl-label-offset: -8
214             # cperl-merge-trailing-else: t
215             # End:
216             # vim: filetype=perl:noexpandtab:ts=3:sw=3