Interface.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Build.pl ↑ Summary
1 package Win32::API::Interface;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 2 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 $VERSION = '0.0001_01';
7
8 2 ▶use Win32::API ();
Subroutine BEGIN: called CC=1 SCAR=2.6
9
10 =head1 NAME
11
12 Win32::API::Interface - Object oriented interface generation
13
14 =head1 SYNOPSIS
15
16
17 package MyModule;
18 use base qw/Win32::API::Interface/;
19
20 __PACKAGE__->generate( "kernel32", "GetCurrentProcessId", "", "N" );
21 __PACKAGE__->generate( "kernel32", "GetCurrentProcessId", "", "N", 'get_pid' );
22
23 1;
24
25 my $obj = MyModule->new );
26 print "PID: " . $obj->GetCurrentProcessId . "\n";
27 print "PID: " . $obj->get_pid . "\n";
28
29
30 =head1 DESCRIPTION
31
32 This module provides functions for generating a object oriented interface to
33 Win32 API functions.
34
35 =head1 METHODS
36
37 =head2 new
38
39 my $obj = Module->new;
40
41
42 Win32::API::Interface provides a basic constructor. It generates a
43 hash-based object and can be called as either a class method or an object
44 method.
45
46 =cut
47
48 sub new {
49 my $proto = shift;
50 my $class = ref $proto || $proto;
51
52 return bless {}, $class;
53 }
54
55 =head2 generate
56
57 __PACKAGE__->generate( "kernel32", "GetCurrentProcessId", "", "N" );
58
59 This generates a method called I<GetCurrentProcessId> which is exported
60 by I<kernel32.dll>. It does not take any input parameters but returns a value
61 of type I<long>.
62
63 __PACKAGE__->generate( "kernel32", "GetCurrentProcessId", "", "N", "get_pid" );
64
65 Actually the same as above, but this will generate a method called I<get_pid>.
66 This is useful if you do not want to rely on the API function name.
67
68 __PACKAGE__->generate(
69 [ "kernel32", "GetTempPath", "NP", "N" ],
70 [ "kernel32", "GetCurrentProcessId", "", "N", "get_pid" ],
71 );
72
73 You may call I<generate> passing an array of array references.
74
75 __PACKAGE__->generate( {
76 "kernel32" => [
77 [ "GetTempPath", "NP", "N" ],
78 [ "GetCurrentProcessId", "", "N", "get_pid" ],
79 ],
80 "user32" => [
81 [ "GetCursorPos", "P", "I"]
82 ],
83 } );
84
85 =cut
86
87 {
88 no strict 'refs';
89
90 sub generate {
91 my $self = shift;
92
93 if ( 'ARRAY' eq ref $_[0] ) {
94 foreach my $args (@_) {
95 $self->generate( @{$args} );
96 }
97 }
98 elsif ( 'HASH' eq ref $_[0] ) {
99 while ( my ( $library, $params ) = each %{ $_[0] } ) {
100 foreach my $args ( @{$params} ) {
101 $self->generate( $library, @{$args} );
102 }
103 }
104 }
105 else {
106
107 my ( $library, $name, $params, $retr, $alias ) = @_;
108 my $class = ref $self || $self;
109 $alias ||= $name;
110
111 *{"${class}::$alias"} =
112 $self->_generate( $library, $name, $params, $retr )
113 unless defined &{"${class}::$alias"};
114 }
115
116 return 1;
117 }
118 }
119
120 sub _generate {
121 my ( $class, $library, $name, $params, $retr ) = @_;
122
123 my $key = uc "$library-$name";
124
125 return sub {
126 my $self = shift;
127
128 my $api = defined $self->{$key} ? $self->{$key} : $self->{$key} =
129 Win32::API->new( $library, $name, $params, $retr );
130 die "Unable to import API $name from $library: $^E" unless defined $api;
131
132 return $api->Call(@_);
133 };
134 }
135
136 sub generate_ex {
137 my $self = shift;
138 my %args = 'HASH' eq ref $_[0] ? %{ $_[0] } : @_;
139
140 while ( my ( $library, $params ) = each %args ) {
141 foreach my $args ( @{$params} ) {
142 $self->generate( $library, @{$args} );
143 }
144 }
145
146 return 1;
147 }
148
149 =head1 AUTHOR
150
151 Sascha Kiefer, L<esskar@cpan.org>
152
153 =head1 COPYRIGHT AND LICENSE
154
155 Copyright (C) 2006 Sascha Kiefer
156
157 This library is free software; you can redistribute it and/or modify
158 it under the same terms as Perl itself.
159
160 =cut
161
162 1;
163
« Build.pl ↑ Summary