home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / perl5.002 / configpm < prev    next >
Encoding:
Text File  |  1996-04-14  |  5.9 KB  |  258 lines  |  [TEXT/MPS ]

  1. #!./miniperl -w
  2.  
  3. $config_pm = $ARGV[0] || ':lib:Config.pm';
  4. @ARGV = ":config.mac";
  5.  
  6. # list names to put first (and hence lookup fastest)
  7. @fast = qw(archname osname osvers prefix libs libpth
  8.     dynamic_ext static_ext extensions dlsrc so
  9.     sig_name cc ccflags cppflags
  10.     privlibexp archlibexp installprivlib installarchlib
  11.     sharpbang startsh shsharp
  12. );
  13.  
  14. # names of things which may need to have slashes changed to double-colons
  15. @extensions = qw(dynamic_ext static_ext extensions known_extensions);
  16.  
  17.  
  18. open CONFIG, ">$config_pm" or die "Can't open $config_pm: $!\n";
  19. $myver = $];
  20.  
  21. print CONFIG <<"ENDOFBEG";
  22. package Config;
  23. use Exporter ();
  24. \@ISA = (Exporter);
  25. \@EXPORT = qw(%Config);
  26. \@EXPORT_OK = qw(myconfig config_sh config_vars);
  27.  
  28. \$] == $myver
  29.   or die "Perl lib version ($myver) doesn't match executable version (\$])\\n";
  30.  
  31. # This file was created by configpm when Perl was built. Any changes
  32. # made to this file will be lost the next time perl is built.
  33.  
  34. ENDOFBEG
  35.  
  36.  
  37. @fast{@fast} = @fast;
  38. @extensions{@extensions} = @extensions;
  39. @non_v=();
  40. @v_fast=();
  41. @v_others=();
  42.  
  43. while (<>) {
  44.     next if m:^#!/bin/sh:;
  45.     # Catch CONFIG=true and PATCHLEVEL=n line from Configure.
  46.     s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/;
  47.     unless (m/^Set (\w+) '(.*)'/){
  48.     push(@non_v, "#$_"); # not a name='value' line
  49.     next;
  50.     }
  51.     $name = $1;
  52.     $_ = "$1=\'$2\'\n";
  53.     if ($extensions{$name}) { s/\s:/ /g; s/:/::/g }
  54.     if (!$fast{$name}){ push(@v_others, $_); next; }
  55.     push(@v_fast,$_);
  56. }
  57.  
  58. foreach(@non_v){ print CONFIG $_ }
  59.  
  60. print CONFIG "\n",
  61.     "my \$config_sh = <<'!END!';\n",
  62.     join("", @v_fast, sort @v_others),
  63.     "!END!\n\n";
  64.  
  65. # copy config summary format from the myconfig script
  66.  
  67. print CONFIG "my \$summary = <<'!END!';\n";
  68.  
  69. open(MYCONFIG,"<myconfig") || die "open myconfig failed: $!";
  70. 1 while( ($_=<MYCONFIG>) !~ /^Summary of/);
  71. do { print CONFIG $_ } until ($_ = <MYCONFIG>) =~ /^\s*$/;
  72. close(MYCONFIG);
  73.  
  74. print CONFIG "\n!END!\n", <<'EOT';
  75. my $summary_expanded = 0;
  76.  
  77. sub myconfig {
  78.     return $summary if $summary_expanded;
  79.     $summary =~ s/\$(\w+)/$Config{$1}/ge;
  80.     $summary_expanded = 1;
  81.     $summary;
  82. }
  83. EOT
  84.  
  85. # ----
  86.  
  87. print CONFIG <<'ENDOFEND';
  88.  
  89. tie %Config, Config;
  90. sub TIEHASH { bless {} }
  91. sub FETCH { 
  92.     # check for cached value (which maybe undef so we use exists not defined)
  93.     return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
  94.  
  95.     my($value); # search for the item in the big $config_sh string
  96.     return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
  97.  
  98.     $value = undef if $value eq 'undef'; # So we can say "if $Config{'foo'}".
  99.     $value =~ s/\\n/\n/g;         # macintosh
  100.     $_[0]->{$_[1]} = $value; # cache it
  101.     return $value;
  102. }
  103.  
  104. my $prevpos = 0;
  105.  
  106. sub FIRSTKEY {
  107.     $prevpos = 0;
  108.     my($key) = $config_sh =~ m/^(.*?)=/;
  109.     $key;
  110. }
  111.  
  112. sub NEXTKEY {
  113.     my $pos = index($config_sh, "\n", $prevpos) + 1;
  114.     my $len = index($config_sh, "=", $pos) - $pos;
  115.     $prevpos = $pos;
  116.     $len > 0 ? substr($config_sh, $pos, $len) : undef;
  117. }
  118.  
  119. sub EXISTS { 
  120.      exists($_[0]->{$_[1]})  or  $config_sh =~ m/^$_[1]=/m; 
  121. }
  122.  
  123. sub STORE  { die "\%Config::Config is read-only\n" }
  124. sub DELETE { &STORE }
  125. sub CLEAR  { &STORE }
  126.  
  127.  
  128. sub config_sh {
  129.     $config_sh
  130. }
  131. sub config_vars {
  132.     foreach(@_){
  133.     my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
  134.     $v='undef' unless defined $v;
  135.     print "$_='$v';\n";
  136.     }
  137. }
  138.  
  139. 1;
  140. __END__
  141.  
  142. =head1 NAME
  143.  
  144. Config - access Perl configuration information
  145.  
  146. =head1 SYNOPSIS
  147.  
  148.     use Config;
  149.     if ($Config{'cc'} =~ /gcc/) {
  150.     print "built by gcc\n";
  151.     } 
  152.  
  153.     use Config qw(myconfig config_sh config_vars);
  154.  
  155.     print myconfig();
  156.  
  157.     print config_sh();
  158.  
  159.     config_vars(qw(osname archname));
  160.  
  161.  
  162. =head1 DESCRIPTION
  163.  
  164. The Config module contains all the information that was available to
  165. the C<Configure> program at Perl build time (over 900 values).
  166.  
  167. Shell variables from the F<config.sh> file (written by Configure) are
  168. stored in the readonly-variable C<%Config>, indexed by their names.
  169.  
  170. Values stored in config.sh as 'undef' are returned as undefined
  171. values.  The perl C<exists> function can be used to check is a
  172. named variable exists.
  173.  
  174. =over 4
  175.  
  176. =item myconfig()
  177.  
  178. Returns a textual summary of the major perl configuration values.
  179. See also C<-V> in L<perlrun/Switches>.
  180.  
  181. =item config_sh()
  182.  
  183. Returns the entire perl configuration information in the form of the
  184. original config.sh shell variable assignment script.
  185.  
  186. =item config_vars(@names)
  187.  
  188. Prints to STDOUT the values of the named configuration variable. Each is
  189. printed on a separate line in the form:
  190.  
  191.   name='value';
  192.  
  193. Names which are unknown are output as C<name='UNKNOWN';>.
  194. See also C<-V:name> in L<perlrun/Switches>.
  195.  
  196. =back
  197.  
  198. =head1 EXAMPLE
  199.  
  200. Here's a more sophisticated example of using %Config:
  201.  
  202.     use Config;
  203.  
  204.     defined $Config{sig_name} || die "No sigs?";
  205.     foreach $name (split(' ', $Config{sig_name})) {
  206.     $signo{$name} = $i;
  207.     $signame[$i] = $name;
  208.     $i++;
  209.     }   
  210.  
  211.     print "signal #17 = $signame[17]\n";
  212.     if ($signo{ALRM}) { 
  213.     print "SIGALRM is $signo{ALRM}\n";
  214.     }   
  215.  
  216. =head1 WARNING
  217.  
  218. Because this information is not stored within the perl executable
  219. itself it is possible (but unlikely) that the information does not
  220. relate to the actual perl binary which is being used to access it.
  221.  
  222. The Config module is installed into the architecture and version
  223. specific library directory ($Config{installarchlib}) and it checks the
  224. perl version number when loaded.
  225.  
  226. =head1 NOTE
  227.  
  228. This module contains a good example of how to use tie to implement a
  229. cache and an example of how to make a tied variable readonly to those
  230. outside of it.
  231.  
  232. =cut
  233.  
  234. ENDOFEND
  235.  
  236. close(CONFIG);
  237.  
  238. # Now do some simple tests on the Config.pm file we have created
  239. unshift(@INC,'lib');
  240. require $config_pm;
  241. import Config;
  242.  
  243. die "$0: $config_pm not valid"
  244.     unless $Config{'CONFIG'} eq 'true';
  245.  
  246. die "$0: error processing $config_pm"
  247.     if defined($Config{'an impossible name'})
  248.     or $Config{'CONFIG'} ne 'true' # test cache
  249.     ;
  250.  
  251. die "$0: error processing $config_pm"
  252.     if eval '$Config{"cc"} = 1'
  253.     or eval 'delete $Config{"cc"}'
  254.     ;
  255.  
  256.  
  257. exit 0;
  258.