Off we go (Release 0.01)
[p5sagit/Devel-PeekPoke.git] / lib / Devel / PeekPoke / Constants.pm
1 package Devel::PeekPoke::Constants;
2
3 use strict;
4 use warnings;
5
6 use Config;
7
8 BEGIN {
9   my $ptr_size = $Config{ptrsize};
10   eval "sub PTR_SIZE () { $ptr_size }";
11
12   my $ptr_pack_type = do {
13     if ($ptr_size == 4) {
14       'L'
15     }
16     elsif ($ptr_size == 8) {
17       'Q'
18     }
19     else {
20       die "Unsupported \$Config{ptrsize}: $ptr_size\n";
21     }
22   };
23   eval "sub PTR_PACK_TYPE () { $ptr_pack_type }";
24
25   my $big_endian = do {
26     my $ivnums = join '', (1 .. $Config{ivsize});
27     if ($Config{byteorder} eq $ivnums ) {
28       0
29     }
30     elsif ($Config{byteorder} eq scalar reverse $ivnums ) {
31       1
32     }
33     else {
34       die "Weird byteorder: $Config{byteorder}\n";
35     }
36   };
37   eval "sub BIG_ENDIAN () { $big_endian }";
38 }
39
40 use base 'Exporter';
41 our @EXPORT_OK = (qw/PTR_SIZE PTR_PACK_TYPE BIG_ENDIAN/);
42
43 =head1 NAME
44
45 Devel::PeekPoke::Constants - Some convenience constants based on your machine
46
47 =head1 DESRIPTION
48
49 This module provides some convenience constants based on your machine. It
50 provides the following constants (exportable on request)
51
52 =head2 PTR_SIZE
53
54 The size of your pointer, equivalent to L<$Config::ptr_size|Config>.
55
56 =head2 PTR_PACK_TYPE
57
58 The L<pack|perlfunc/pack> template type suitable for L</PTR_SIZE> pointers.
59 Either C<L> (32 bit) or C<Q> (64 bit).
60
61 =head2 BIG_ENDIAN
62
63 An indicator whether your system is big-endian (constant is set to C<1>) or
64 little-endian (constant is set to C<0>).
65
66 =head1 COPYRIGHT
67
68 See L<Devel::PeekPoke/COPYRIGHT>.
69
70 =head1 LICENSE
71
72 See L<Devel::PeekPoke/LICENSE>.
73
74 =cut
75
76 1;