File Coverage

blib/lib/AppConfig/Sys.pm
Criterion Covered Total %
statement 34 67 50.7
branch 11 32 34.3
condition 2 6 33.3
subroutine 6 8 75.0
pod 0 1 0.0
total 53 114 46.4


line stmt bran cond sub pod time code
1             #============================================================================
2             #
3             # AppConfig::Sys.pm
4             #
5             # Perl5 module providing platform-specific information and operations as
6             # required by other AppConfig::* modules.
7             #
8             # Written by Andy Wardley
9             #
10             # Copyright (C) 1997-2003 Andy Wardley. All Rights Reserved.
11             # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
12             #
13             # $Id: Sys.pm,v 1.61 2004/02/04 10:11:23 abw Exp $
14             #
15             #============================================================================
16              
17             package AppConfig::Sys;
18 3     3   391 use strict;
  3         6  
  3         99  
19 3     3   11 use warnings;
  3         3  
  3         89  
20 3     3   1414 use POSIX qw( getpwnam getpwuid );
  3         14814  
  3         12  
21              
22             our $VERSION = '1.70';
23             our ($AUTOLOAD, $OS, %CAN, %METHOD);
24              
25              
26             BEGIN {
27             # define the methods that may be available
28 3 50   3   2930 if($^O =~ m/win32/i) {
29             $METHOD{ getpwuid } = sub {
30             return wantarray()
31 0 0       0 ? ( (undef) x 7, getlogin() )
32             : getlogin();
33 0         0 };
34             $METHOD{ getpwnam } = sub {
35 0         0 die("Can't getpwnam on win32");
36 0         0 };
37             }
38             else
39             {
40             $METHOD{ getpwuid } = sub {
41 3 50       24 getpwuid( defined $_[0] ? shift : $< );
42 3         22 };
43             $METHOD{ getpwnam } = sub {
44 3 50       21 getpwnam( defined $_[0] ? shift : '' );
45 3         6 };
46             }
47              
48             # try out each METHOD to see if it's supported on this platform;
49             # it's important we do this before defining AUTOLOAD which would
50             # otherwise catch the unresolved call
51 3         8 foreach my $method (keys %METHOD) {
52 6         9 eval { &{ $METHOD{ $method } }() };
  6         6  
  6         16  
53 6         4207 $CAN{ $method } = ! $@;
54             }
55             }
56              
57              
58              
59             #------------------------------------------------------------------------
60             # new($os)
61             #
62             # Module constructor. An optional operating system string may be passed
63             # to explicitly define the platform type.
64             #
65             # Returns a reference to a newly created AppConfig::Sys object.
66             #------------------------------------------------------------------------
67              
68             sub new {
69 4     4 0 18 my $class = shift;
70              
71 4         11 my $self = {
72             METHOD => \%METHOD,
73             CAN => \%CAN,
74             };
75              
76 4         11 bless $self, $class;
77              
78 4         9 $self->_configure(@_);
79            
80 4         8 return $self;
81             }
82              
83              
84             #------------------------------------------------------------------------
85             # AUTOLOAD
86             #
87             # Autoload function called whenever an unresolved object method is
88             # called. If the method name relates to a METHODS entry, then it is
89             # called iff the corresponding CAN_$method is set true. If the
90             # method name relates to a CAN_$method value then that is returned.
91             #------------------------------------------------------------------------
92              
93             sub AUTOLOAD {
94 0     0   0 my $self = shift;
95 0         0 my $method;
96              
97              
98             # splat the leading package name
99 0         0 ($method = $AUTOLOAD) =~ s/.*:://;
100              
101             # ignore destructor
102 0 0       0 $method eq 'DESTROY' && return;
103              
104             # can_method()
105 0 0 0     0 if ($method =~ s/^can_//i && exists $self->{ CAN }->{ $method }) {
    0          
    0          
106 0         0 return $self->{ CAN }->{ $method };
107             }
108             # method()
109             elsif (exists $self->{ METHOD }->{ $method }) {
110 0 0       0 if ($self->{ CAN }->{ $method }) {
111 0         0 return &{ $self->{ METHOD }->{ $method } }(@_);
  0         0  
112             }
113             else {
114 0         0 return undef;
115             }
116             }
117             # variable
118             elsif (exists $self->{ uc $method }) {
119 0         0 return $self->{ uc $method };
120             }
121             else {
122 0         0 warn("AppConfig::Sys->", $method, "(): no such method or variable\n");
123             }
124              
125 0         0 return undef;
126             }
127              
128              
129             #------------------------------------------------------------------------
130             # _configure($os)
131             #
132             # Uses the first parameter, $os, the package variable $AppConfig::Sys::OS,
133             # the value of $^O, or as a last resort, the value of
134             # $Config::Config('osname') to determine the current operating
135             # system/platform. Sets internal variables accordingly.
136             #------------------------------------------------------------------------
137              
138             sub _configure {
139 4     4   4 my $self = shift;
140              
141             # operating system may be defined as a parameter or in $OS
142 4   66     14 my $os = shift || $OS;
143              
144              
145             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146             # The following was lifted (and adapated slightly) from Lincoln Stein's
147             # CGI.pm module, version 2.36...
148             #
149             # FIGURE OUT THE OS WE'RE RUNNING UNDER
150             # Some systems support the $^O variable. If not
151             # available then require() the Config library
152 4 100       7 unless ($os) {
153 3 50       12 unless ($os = $^O) {
154 0         0 require Config;
155 0         0 $os = $Config::Config{'osname'};
156             }
157             }
158 4 100       25 if ($os =~ /win32/i) {
    50          
    50          
    50          
159 1         2 $os = 'WINDOWS';
160             } elsif ($os =~ /vms/i) {
161 0         0 $os = 'VMS';
162             } elsif ($os =~ /mac/i) {
163 0         0 $os = 'MACINTOSH';
164             } elsif ($os =~ /os2/i) {
165 0         0 $os = 'OS2';
166             } else {
167 3         4 $os = 'UNIX';
168             }
169              
170              
171             # The path separator is a slash, backslash or semicolon, depending
172             # on the platform.
173 4         15 my $ps = {
174             UNIX => '/',
175             OS2 => '\\',
176             WINDOWS => '\\',
177             MACINTOSH => ':',
178             VMS => '\\'
179             }->{ $os };
180             #
181             # Thanks Lincoln!
182             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183              
184              
185 4         20 $self->{ OS } = $os;
186 4         9 $self->{ PATHSEP } = $ps;
187             }
188              
189              
190             #------------------------------------------------------------------------
191             # _dump()
192             #
193             # Dump internals for debugging.
194             #------------------------------------------------------------------------
195              
196             sub _dump {
197 0     0     my $self = shift;
198              
199 0           print "=" x 71, "\n";
200 0           print "Status of AppConfig::Sys (Version $VERSION) object: $self\n";
201 0           print " Operating System : ", $self->{ OS }, "\n";
202 0           print " Path Separator : ", $self->{ PATHSEP }, "\n";
203 0           print " Available methods :\n";
204 0           foreach my $can (keys %{ $self->{ CAN } }) {
  0            
205 0           printf "%20s : ", $can;
206 0 0         print $self->{ CAN }->{ $can } ? "yes" : "no", "\n";
207             }
208 0           print "=" x 71, "\n";
209             }
210              
211              
212              
213             1;
214              
215             __END__