Documentation
[Config-Perl-V.git] / V.pm
blob5aa36922bfb3fd6102274d39e927dca4e02dd0ab
1 #!/pro/bin/perl
3 package Config::Perl::V;
5 use strict;
6 use warnings;
8 our $VERSION = "0.02";
10 use Config;
12 # Characteristics of this binary (from libperl):
13 # Compile-time options: DEBUGGING PERL_DONT_CREATE_GVSV PERL_MALLOC_WRAP
14 # USE_64_BIT_INT USE_LARGE_FILES USE_PERLIO
16 # The list are as the perl binary has stored it in PL_bincompat_options
17 # search for it in
18 # perl.c line 1768 (first block)
19 # perl.h line 4454 (second block),
20 my %BTD = map { $_ => 0 } qw(
22 DEBUGGING
23 NO_MATHOMS
24 PERL_DONT_CREATE_GVSV
25 PERL_MALLOC_WRAP
26 PERL_MEM_LOG
27 PERL_MEM_LOG_ENV
28 PERL_MEM_LOG_ENV_FD
29 PERL_MEM_LOG_STDERR
30 PERL_MEM_LOG_TIMESTAMP
31 PERL_USE_DEVEL
32 PERL_USE_SAFE_PUTENV
33 USE_FAST_STDIO
34 USE_SITECUSTOMIZE
36 DEBUG_LEAKING_SCALARS
37 DEBUG_LEAKING_SCALARS_FORK_DUMP
38 DECCRTL_SOCKETS
39 FAKE_THREADS
40 MULTIPLICITY
41 MYMALLOC
42 PERL_DEBUG_READONLY_OPS
43 PERL_GLOBAL_STRUCT
44 PERL_IMPLICIT_CONTEXT
45 PERL_IMPLICIT_SYS
46 PERL_MAD
47 PERL_NEED_APPCTX
48 PERL_NEED_TIMESBASE
49 PERL_OLD_COPY_ON_WRITE
50 PERL_POISON
51 PERL_TRACK_MEMPOOL
52 PERL_USES_PL_PIDSTATUS
53 PL_OP_SLAB_ALLOC
54 THREADS_HAVE_PIDS
55 USE_64_BIT_ALL
56 USE_64_BIT_INT
57 USE_IEEE
58 USE_ITHREADS
59 USE_LARGE_FILES
60 USE_LONG_DOUBLE
61 USE_PERLIO
62 USE_REENTRANT_API
63 USE_SFIO
64 USE_SOCKS
65 VMS_DO_SOCKETS
66 VMS_SYMBOL_CASE_AS_IS
69 # These are all the keys that are
70 # 1. Always present in %Config (first block)
71 # 2. Reported by 'perl -V' (the rest)
72 my @config_vars = qw(
74 archlibexp
75 dont_use_nlink
76 d_readlink
77 d_symlink
78 exe_ext
79 inc_version_list
80 ldlibpthname
81 path_sep
82 privlibexp
83 scriptdir
84 sitearchexp
85 sitelibexp
86 usevendorprefix
87 version
89 package revision version_patchlevel_string
91 osname osvers archname
92 myuname
93 config_args
94 hint useposix d_sigaction
95 useithreads usemultiplicity
96 useperlio d_sfio uselargefiles usesocks
97 use64bitint use64bitall uselongdouble
98 usemymalloc bincompat5005
100 cc ccflags
101 optimize
102 cppflags
103 ccversion gccversion gccosandvers
104 intsize longsize ptrsize doublesize byteorder
105 d_longlong longlongsize d_longdbl longdblsize
106 ivtype ivsize nvtype nvsize lseektype lseeksize
107 alignbytes prototype
109 ld ldflags
110 libpth
111 libs
112 perllibs
113 libc so useshrplib libperl
114 gnulibc_version
116 dlsrc dlext d_dlsymun ccdlflags
117 cccdlflags lddlflags
120 my %empty_build = (
121 osname => "",
122 stamp => 0,
123 options => { %BTD },
124 patches => [],
127 sub plv2hash
129 my %config;
130 for (split m/\n+/ => join "\n", @_) {
132 if (s/^Summary of my\s+(\S+)\s+\(\s*(.*?)\s*\)//) {
133 $config{"package"} = $1;
134 my $rev = $2;
135 $rev =~ s/^ revision \s+ (\S+) \s*//x and $config{revision} = $1;
136 $rev and $config{version_patchlevel_string} = $rev;
137 next;
140 my %kv = m/\G,?\s*([^=]+)=('[^']+?'|\S+)/gc;
142 while (my ($k, $v) = each %kv) {
143 $k =~ s/\s+$//;
144 $v =~ s/,$//;
145 $v =~ m/^'(.*)'$/ and $v = $1;
146 $v =~ s/^\s+//;
147 $v =~ s/\s+$//;
148 $config{$k} = $v;
151 my $build = { %empty_build };
152 $build->{osname} = $config{osname};
153 return {
154 build => $build,
155 environment => {},
156 config => \%config,
157 inc => [],
159 } # plv2hash
161 sub myconfig
163 my $args = shift;
164 my %args = ref $args eq "HASH" ? %$args :
165 ref $args eq "ARRAY" ? @$args : ();
167 #y $pv = qx[$^X -e"sub Config::myconfig{};" -V];
168 my $pv = qx[$^X -V];
169 $pv =~ s{.*?\n\n}{}s;
170 $pv =~ s{\n(?: \s+|\t\s*)}{ }g;
172 #print $pv;
174 my $build = { %empty_build };
175 $pv =~ m{^\s+Built under (.*)}m and $build->{osname} = $1;
176 $pv =~ m{^\s+Compiled at (.*)}m and $build->{stamp} = $1;
177 $pv =~ m{^\s+Locally applied patches:\s+(.*)}m and $build->{patches} = [ split m/\s+/, $1 ];
178 $pv =~ m{^\s+Compile-time options:\s+(.*)}m and map { $build->{options}{$_} = 1 } split m/\s+/, $1;
180 my @KEYS = keys %ENV;
181 my %env =
182 map { $_ => $ENV{$_} } grep m/^PERL/ => @KEYS;
183 $args{env} and
184 map { $env{$_} = $ENV{$_} } grep m{$args{env}} => @KEYS;
186 my %config = map { $_ => $Config{$_} } @config_vars;
188 return {
189 build => $build,
190 environment => \%env,
191 config => \%config,
192 inc => \@INC,
194 } # myconfig
198 __END__
200 =head1 NAME
202 Config::Perl::V - Structured data retreival of perl -V output
204 =head1 SYNOPSIS
206 use Config::Perl::V;
208 my $local_config = Config::Perl::V::myconfig ();
209 print $local_config->{config}{osname};
211 =head1 DESCRIPTION
213 =head2 myconfig ()
215 This function will collect the data decribed in L<the hash structure> below,
217 =head2 plv2hash ()
219 Convert a sole 'perl -V' text block to a complete myconfig hash.
220 All unknown entries are defaulted.
222 =head2 The hash structure
224 The returned hash consists of 4 parts:
226 =over 4
228 =item build
230 This information is extracted from the second block that is emitted by
231 C<perl -V>, and usually looks something like
233 Characteristics of this binary (from libperl):
234 Compile-time options: DEBUGGING USE_64_BIT_INT USE_LARGE_FILES
235 Locally applied patches:
236 defined-or
237 MAINT24637
238 Built under linux
239 Compiled at Jun 13 2005 10:44:20
240 @INC:
241 /usr/lib/perl5/5.8.7/i686-linux-64int
242 /usr/lib/perl5/5.8.7
243 /usr/lib/perl5/site_perl/5.8.7/i686-linux-64int
244 /usr/lib/perl5/site_perl/5.8.7
245 /usr/lib/perl5/site_perl
250 Characteristics of this binary (from libperl):
251 Compile-time options: DEBUGGING MULTIPLICITY
252 PERL_DONT_CREATE_GVSV PERL_IMPLICIT_CONTEXT
253 PERL_MALLOC_WRAP PERL_TRACK_MEMPOOL
254 PERL_USE_SAFE_PUTENV USE_ITHREADS
255 USE_LARGE_FILES USE_PERLIO
256 USE_REENTRANT_API
257 Built under linux
258 Compiled at Jan 28 2009 15:26:59
260 This information is not available anywhere else, including C<%Config>,
261 but it is the information that is only known to the perl binary.
263 The extracted information is stored in 4 entries in the C<build> hash:
265 =over 4
267 =item osname
269 This is most likely the same as C$Config{osname}>, and was the name
270 known when perl was built. It might be different if perl was cross-compiled.
272 The default for this field, if it cannot be extracted, is to copy C<$Config{osname}>.
274 =item stamp
276 This is the time string for which the perl binary was compiled. The default
277 value is 0.
279 =item options
281 This is a hash with all the known defines as keys. The value is either 0,
282 which means unknown or unset, or 1, which means defined.
284 =item patches
286 This is a list of optionally locally applied patches. Default is an empty list.
288 =back
290 This include the flags that define the binary compatibility, like if
291 this perl was compiled for 64bitall (C<USE_64_BIT_ALL>), and basically is
293 =item environment
295 By default this hash is only filled with the environment variables
296 out of %ENV that start with C<PERL>, but you can pass the C<env> option
297 to myconfig to get more
299 my $conf = Config::Perl::V::myconfig ({ env => qr/^ORACLE/ });
300 my $conf = Config::Perl::V::myconfig ([ env => qr/^ORACLE/ ]);
302 =item config
304 This hash is filled with the variables that C<perl -V> fils it's report
305 with, and it has the same variables that C<Config::myconfig> returns
306 from C<%Config>.
308 =item inc
310 This is the list of default @INC.
312 =back
314 =head1 REASONING
316 This module was written to be able to return the configuration for the
317 currently used perl as deeply as needed for the CPANTESTERS framework.
318 Up until now they used the output of myconfig as a single text blob,
319 and so it was missing the vital binary characteristics of the running
320 perl and the optional applied patches.
322 =head1 BUGS
324 Please feedback what is wrong
326 =head1 TODO
328 * Implement retrieval functions/methods
329 * Documentation
330 * Include the perl -V parse block from Andreas
332 =head1 AUTHOR
334 H.Merijn Brand <h.m.brand@xs4all.nl>
336 =head1 COPYRIGHT AND LICENSE
338 Copyright (C) 1999-2009 H.Merijn Brand
340 This library is free software; you can redistribute it and/or modify
341 it under the same terms as Perl itself.
343 =cut