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