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";
19 $myver = sprintf("%.3f", $]);
21 print CONFIG <<"ENDOFBEG";
25 \@EXPORT = qw(%Config);
26 \@EXPORT_OK = qw(myconfig config_sh config_vars);
28 \$] == $myver or die sprintf
29 "Perl lib version ($myver) doesn't match executable version (%.3f)\\n", \$];
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;
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/^(\w+)='(.*)'\s*$/){
48 push(@non_v, "#$_"); # not a name='value' line
52 if ($extensions{$name}) { s,/,::,g }
53 if (!$fast{$name}){ push(@v_others, $_); next; }
57 foreach(@non_v){ print CONFIG $_ }
60 "my \$config_sh = <<'!END!';\n",
61 join("", @v_fast, sort @v_others),
64 # copy config summary format from the myconfig script
66 print CONFIG "my \$summary = <<'!END!';\n";
68 open(MYCONFIG,"<myconfig") || die "open myconfig failed: $!";
69 1 while( ($_=<MYCONFIG>) !~ /^Summary of/);
70 do { print CONFIG $_ } until ($_ = <MYCONFIG>) =~ /^\s*$/;
73 print CONFIG "\n!END!\n", <<'EOT';
74 my $summary_expanded = 0;
77 return $summary if $summary_expanded;
78 $summary =~ s/\$(\w+)/$Config{$1}/ge;
79 $summary_expanded = 1;
86 print CONFIG <<'ENDOFEND';
89 sub TIEHASH { bless {} }
91 # check for cached value (which maybe undef so we use exists not defined)
92 return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]});
94 my($value); # search for the item in the big $config_sh string
95 return undef unless (($value) = $config_sh =~ m/^$_[1]='(.*)'\s*$/m);
97 $value = undef if $value eq 'undef'; # So we can say "if $Config{'foo'}".
98 $_[0]->{$_[1]} = $value; # cache it
106 my($key) = $config_sh =~ m/^(.*?)=/;
111 my $pos = index($config_sh, "\n", $prevpos) + 1;
112 my $len = index($config_sh, "=", $pos) - $pos;
114 $len > 0 ? substr($config_sh, $pos, $len) : undef;
118 exists($_[0]->{$_[1]}) or $config_sh =~ m/^$_[1]=/m;
121 sub STORE { die "\%Config::Config is read-only\n" }
122 sub DELETE { &STORE }
131 my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOWN';
132 $v='undef' unless defined $v;
142 Config - access Perl configuration information
147 if ($Config{'cc'} =~ /gcc/) {
148 print "built by gcc\n";
151 use Config qw(myconfig config_sh config_vars);
157 config_vars(qw(osname archname));
162 The Config module contains all the information that was available to
163 the C<Configure> program at Perl build time (over 900 values).
165 Shell variables from the F<config.sh> file (written by Configure) are
166 stored in the readonly-variable C<%Config>, indexed by their names.
168 Values stored in config.sh as 'undef' are returned as undefined
169 values. The perl C<exists> function can be used to check is a
170 named variable exists.
176 Returns a textual summary of the major perl configuration values.
177 See also C<-V> in L<perlrun/Switches>.
181 Returns the entire perl configuration information in the form of the
182 original config.sh shell variable assignment script.
184 =item config_vars(@names)
186 Prints to STDOUT the values of the named configuration variable. Each is
187 printed on a separate line in the form:
191 Names which are unknown are output as C<name='UNKNOWN';>.
192 See also C<-V:name> in L<perlrun/Switches>.
198 Here's a more sophisticated example of using %Config:
202 defined $Config{sig_name} || die "No sigs?";
203 foreach $name (split(' ', $Config{sig_name})) {
205 $signame[$i] = $name;
209 print "signal #17 = $signame[17]\n";
211 print "SIGALRM is $signo{ALRM}\n";
216 Because this information is not stored within the perl executable
217 itself it is possible (but unlikely) that the information does not
218 relate to the actual perl binary which is being used to access it.
220 The Config module is installed into the architecture and version
221 specific library directory ($Config{installarchlib}) and it checks the
222 perl version number when loaded.
226 This module contains a good example of how to use tie to implement a
227 cache and an example of how to make a tied variable readonly to those
236 # Now do some simple tests on the Config.pm file we have created
241 die "$0: $config_pm not valid"
242 unless $Config{'CONFIG'} eq 'true';
244 die "$0: error processing $config_pm"
245 if defined($Config{'an impossible name'})
246 or $Config{'CONFIG'} ne 'true' # test cache
249 die "$0: error processing $config_pm"
250 if eval '$Config{"cc"} = 1'
251 or eval 'delete $Config{"cc"}'