3 $config_pm = $ARGV[0] || 'lib/Config.pm';
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
14 # names of things which may need to have slashes changed to double-colons
15 @extensions = qw(dynamic_ext static_ext extensions known_extensions);
18 open CONFIG, ">$config_pm" or die "Can't open $config_pm: $!\n";
21 print CONFIG <<"ENDOFBEG";
25 \@EXPORT = qw(%Config);
26 \@EXPORT_OK = qw(myconfig config_sh config_vars);
29 or die "Perl lib version ($myver) doesn't match executable version (\$])";
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.
38 @extensions{@extensions} = @extensions;
45 next if m:^#!/bin/sh:;
46 # Catch CONFIG=true and PATCHLEVEL=n line from Configure.
47 s/^(\w+)=(true|\d+)\s*$/$1='$2'\n/;
48 unless ($in_v or m/^(\w+)='(.*\n)/){
49 push(@non_v, "#$_"); # not a name='value' line
52 if ($in_v) { $val .= $_; }
53 else { ($name,$val) = ($1,$2); }
54 $in_v = $val !~ /'\n/;
56 if ($extensions{$name}) { s,/,::,g }
57 if (!$fast{$name}){ push(@v_others, "$name='$val"); next; }
58 push(@v_fast,"$name='$val");
61 foreach(@non_v){ print CONFIG $_ }
64 "my \$config_sh = <<'!END!';\n",
65 join("", @v_fast, sort @v_others),
68 # copy config summary format from the myconfig script
70 print CONFIG "my \$summary = <<'!END!';\n";
72 open(MYCONFIG,"<myconfig") || die "open myconfig failed: $!";
73 1 while( ($_=<MYCONFIG>) !~ /^Summary of/);
74 do { print CONFIG $_ } until ($_ = <MYCONFIG>) =~ /^\s*$/;
77 print CONFIG "\n!END!\n", <<'EOT';
78 my $summary_expanded = 0;
81 return $summary if $summary_expanded;
82 $summary =~ s/\$(\w+)/$Config{$1}/ge;
83 $summary_expanded = 1;
90 print CONFIG <<'ENDOFEND';
93 # check for cached value (which may be undef so we use exists not defined)
94 return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
96 # Search for it in the big string
97 my($value, $start, $marker);
99 # return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
100 $start = index($config_sh, "\n$marker");
101 return undef if ( ($start == -1) && # in case it's first
102 (substr($config_sh, 0, length($marker)) ne $marker) );
103 if ($start == -1) { $start = length($marker) }
104 else { $start += length($marker) + 1 }
105 $value = substr($config_sh, $start,
106 index($config_sh, qq('\n), $start) - $start);
108 $value = undef if $value eq 'undef'; # So we can say "if $Config{'foo'}".
109 $_[0]->{$_[1]} = $value; # cache it
117 # my($key) = $config_sh =~ m/^(.*?)=/;
118 substr($config_sh, 0, index($config_sh, '=') );
123 my $pos = index($config_sh, qq('\n), $prevpos) + 2;
124 my $len = index($config_sh, "=", $pos) - $pos;
126 $len > 0 ? substr($config_sh, $pos, $len) : undef;
130 # exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m;
131 exists($_[0]->{$_[1]}) or
132 index($config_sh, "\n$_[1]='") != -1 or
133 substr($config_sh, 0, length($_[1])+2) eq "$_[1]='";
136 sub STORE { die "\%Config::Config is read-only\n" }
137 sub DELETE { &STORE }
147 my @matches = ($config_sh =~ /^$re=.*\n/mg);
148 @matches ? (print @matches) : print "$re: not found\n";
153 config_re($_), next if /\W/;
154 my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
155 $v='undef' unless defined $v;
163 print CONFIG <<'ENDOFSET';
166 my ($value, $v) = $config_sh =~ m/^used_aout='(.*)'\s*$/m;
167 for (split ' ', $value) {
168 ($v) = $config_sh =~ m/^aout_$_='(.*)'\s*$/m;
169 $preconfig{$_} = $v eq 'undef' ? undef : $v;
172 sub TIEHASH { bless {%preconfig} }
175 print CONFIG <<'ENDOFSET';
176 sub TIEHASH { bless {} }
180 print CONFIG <<'ENDOFTAIL';
182 tie %Config, 'Config';
189 Config - access Perl configuration information
194 if ($Config{'cc'} =~ /gcc/) {
195 print "built by gcc\n";
198 use Config qw(myconfig config_sh config_vars);
204 config_vars(qw(osname archname));
209 The Config module contains all the information that was available to
210 the C<Configure> program at Perl build time (over 900 values).
212 Shell variables from the F<config.sh> file (written by Configure) are
213 stored in the readonly-variable C<%Config>, indexed by their names.
215 Values stored in config.sh as 'undef' are returned as undefined
216 values. The perl C<exists> function can be used to check if a
217 named variable exists.
223 Returns a textual summary of the major perl configuration values.
224 See also C<-V> in L<perlrun/Switches>.
228 Returns the entire perl configuration information in the form of the
229 original config.sh shell variable assignment script.
231 =item config_vars(@names)
233 Prints to STDOUT the values of the named configuration variable. Each is
234 printed on a separate line in the form:
238 Names which are unknown are output as C<name='UNKNOWN';>.
239 See also C<-V:name> in L<perlrun/Switches>.
245 Here's a more sophisticated example of using %Config:
249 defined $Config{sig_name} || die "No sigs?";
250 foreach $name (split(' ', $Config{sig_name})) {
252 $signame[$i] = $name;
256 print "signal #17 = $signame[17]\n";
258 print "SIGALRM is $signo{ALRM}\n";
263 Because this information is not stored within the perl executable
264 itself it is possible (but unlikely) that the information does not
265 relate to the actual perl binary which is being used to access it.
267 The Config module is installed into the architecture and version
268 specific library directory ($Config{installarchlib}) and it checks the
269 perl version number when loaded.
273 This module contains a good example of how to use tie to implement a
274 cache and an example of how to make a tied variable readonly to those
283 # Now do some simple tests on the Config.pm file we have created
288 die "$0: $config_pm not valid"
289 unless $Config{'CONFIG'} eq 'true';
291 die "$0: error processing $config_pm"
292 if defined($Config{'an impossible name'})
293 or $Config{'CONFIG'} ne 'true' # test cache
296 die "$0: error processing $config_pm"
297 if eval '$Config{"cc"} = 1'
298 or eval 'delete $Config{"cc"}'