home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _da0ba9a981f4c45c8a98d0b12619f222 < prev    next >
Text File  |  2004-06-01  |  6KB  |  198 lines

  1. # The documentation is at the __END__
  2.  
  3. package Win32::OLE::Const;
  4.  
  5. use strict;
  6. use Carp;
  7. use Win32::OLE;
  8.  
  9. my $Typelibs;
  10. sub _Typelib {
  11.     my ($clsid,$title,$version,$langid,$filename) = @_;
  12.     # Filenames might have a resource index appended to it.
  13.     $filename = $1 if $filename =~ /^(.*\.(?:dll|exe))(\\\d+)$/i;
  14.     # Ignore if it looks like a file but doesn't exist.
  15.     # We don't verify existance of monikers or filenames
  16.     # without a full pathname.
  17.     return unless -f $filename || $filename !~ /^\w:\\.*\.(exe|dll)$/;
  18.     push @$Typelibs, \@_;
  19. }
  20. __PACKAGE__->_Typelibs;
  21.  
  22. sub import {
  23.     my ($self,$name,$major,$minor,$language,$codepage) = @_;
  24.     return unless defined($name) && $name !~ /^\s*$/;
  25.     $self->Load($name,$major,$minor,$language,$codepage,scalar caller);
  26. }
  27.  
  28. sub EnumTypeLibs {
  29.     my ($self,$callback) = @_;
  30.     foreach (@$Typelibs) { &$callback(@$_) }
  31.     return;
  32. }
  33.  
  34. sub Load {
  35.     my ($self,$name,$major,$minor,$language,$codepage,$caller) = @_;
  36.  
  37.     if (UNIVERSAL::isa($name,'Win32::OLE')) {
  38.     my $typelib = $name->GetTypeInfo->GetContainingTypeLib;
  39.     return _Constants($typelib, undef);
  40.     }
  41.  
  42.     undef $minor unless defined $major;
  43.     my $typelib = $self->LoadRegTypeLib($name,$major,$minor,
  44.                     $language,$codepage);
  45.     return _Constants($typelib, $caller);
  46. }
  47.  
  48. sub LoadRegTypeLib {
  49.     my ($self,$name,$major,$minor,$language,$codepage) = @_;
  50.     undef $minor unless defined $major;
  51.  
  52.     unless (defined($name) && $name !~ /^\s*$/) {
  53.     carp "Win32::OLE::Const->Load: No or invalid type library name";
  54.     return;
  55.     }
  56.  
  57.     my @found;
  58.     foreach my $Typelib (@$Typelibs) {
  59.     my ($clsid,$title,$version,$langid,$filename) = @$Typelib;
  60.     next unless $title =~ /^$name/;
  61.     next unless $version =~ /^([0-9a-fA-F]+)\.([0-9a-fA-F]+)$/;
  62.     my ($maj,$min) = (hex($1), hex($2));
  63.     next if defined($major) && $maj != $major;
  64.     next if defined($minor) && $min < $minor;
  65.     next if defined($language) && $language != $langid;
  66.     push @found, [$clsid,$maj,$min,$langid,$filename];
  67.     }
  68.  
  69.     unless (@found) {
  70.     carp "No type library matching \"$name\" found";
  71.     return;
  72.     }
  73.  
  74.     @found = sort {
  75.     # Prefer greater version number
  76.     my $res = $b->[1] <=> $a->[1];
  77.     $res = $b->[2] <=> $a->[2] if $res == 0;
  78.     # Prefer default language for equal version numbers
  79.     $res = -1 if $res == 0 && $a->[3] == 0;
  80.     $res =  1 if $res == 0 && $b->[3] == 0;
  81.     $res;
  82.     } @found;
  83.  
  84.     #printf "Loading %s\n", join(' ', @{$found[0]});
  85.     return _LoadRegTypeLib(@{$found[0]},$codepage);
  86. }
  87.  
  88. 1;
  89.  
  90. __END__
  91.  
  92. =head1 NAME
  93.  
  94. Win32::OLE::Const - Extract constant definitions from TypeLib
  95.  
  96. =head1 SYNOPSIS
  97.  
  98.     use Win32::OLE::Const 'Microsoft Excel';
  99.     printf "xlMarkerStyleDot = %d\n", xlMarkerStyleDot;
  100.  
  101.     my $wd = Win32::OLE::Const->Load("Microsoft Word 8\\.0 Object Library");
  102.     foreach my $key (keys %$wd) {
  103.         printf "$key = %s\n", $wd->{$key};
  104.     }
  105.  
  106. =head1 DESCRIPTION
  107.  
  108. This modules makes all constants from a registered OLE type library
  109. available to the Perl program.  The constant definitions can be
  110. imported as functions, providing compile time name checking.
  111. Alternatively the constants can be returned in a hash reference
  112. which avoids defining lots of functions of unknown names.
  113.  
  114. =head2 Functions/Methods
  115.  
  116. =over 4
  117.  
  118. =item use Win32::OLE::Const
  119.  
  120. The C<use> statement can be used to directly import the constant names
  121. and values into the users namespace.
  122.  
  123.     use Win32::OLE::Const (TYPELIB,MAJOR,MINOR,LANGUAGE);
  124.  
  125. The TYPELIB argument specifies a regular expression for searching
  126. through the registry for the type library.  Note that this argument is
  127. implicitly prefixed with C<^> to speed up matches in the most common
  128. cases.  Use a typelib name like ".*Excel" to match anywhere within the
  129. description.  TYPELIB is the only required argument.
  130.  
  131. The MAJOR and MINOR arguments specify the requested version of
  132. the type specification.  If the MAJOR argument is used then only
  133. typelibs with exactly this major version number will be matched.  The
  134. MINOR argument however specifies the minimum acceptable minor version.
  135. MINOR is ignored if MAJOR is undefined.
  136.  
  137. If the LANGUAGE argument is used then only typelibs with exactly this
  138. language id will be matched.
  139.  
  140. The module will select the typelib with the highest version number
  141. satisfying the request.  If no language id is specified then a the default
  142. language (0) will be preferred over the others.
  143.  
  144. Note that only constants with valid Perl variable names will be exported,
  145. i.e. names matching this regexp: C</^[a-zA-Z_][a-zA-Z0-9_]*$/>.
  146.  
  147. =item Win32::OLE::Const->Load
  148.  
  149. The Win32::OLE::Const->Load method returns a reference to a hash of
  150. constant definitions.
  151.  
  152.     my $const = Win32::OLE::Const->Load(TYPELIB,MAJOR,MINOR,LANGUAGE);
  153.  
  154. The parameters are the same as for the C<use> case.
  155.  
  156. This method is generally preferrable when the typelib uses a non-english
  157. language and the constant names contain locale specific characters not
  158. allowed in Perl variable names.
  159.  
  160. Another advantage is that all available constants can now be enumerated.
  161.  
  162. The load method also accepts an OLE object as a parameter.  In this case
  163. the OLE object is queried about its containing type library and no registry
  164. search is done at all.  Interestingly this seems to be slower.
  165.  
  166. =back
  167.  
  168. =head1 EXAMPLES
  169.  
  170. The first example imports all Excel constants names into the main namespace
  171. and prints the value of xlMarkerStyleDot (-4118).
  172.  
  173.     use Win32::OLE::Const ('Microsoft Excel 8.0 Object Library');
  174.     print "xlMarkerStyleDot = %d\n", xlMarkerStyleDot;
  175.  
  176. The second example returns all Word constants in a hash ref.
  177.  
  178.     use Win32::OLE::Const;
  179.     my $wd = Win32::OLE::Const->Load("Microsoft Word 8.0 Object Library");
  180.     foreach my $key (keys %$wd) {
  181.         printf "$key = %s\n", $wd->{$key};
  182.     }
  183.     printf "wdGreen = %s\n", $wd->{wdGreen};
  184.  
  185. The last example uses an OLE object to specify the type library:
  186.  
  187.     use Win32::OLE;
  188.     use Win32::OLE::Const;
  189.     my $Excel = Win32::OLE->new('Excel.Application', 'Quit');
  190.     my $xl = Win32::OLE::Const->Load($Excel);
  191.  
  192.  
  193. =head1 AUTHORS/COPYRIGHT
  194.  
  195. This module is part of the Win32::OLE distribution.
  196.  
  197. =cut
  198.