1 #============================================================================
5 # Perl5 module providing platform-specific information and operations as
6 # required by other AppConfig::* modules.
8 # Written by Andy Wardley <abw@wardley.org>
10 # Copyright (C) 1997-2003 Andy Wardley. All Rights Reserved.
11 # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
13 # $Id: Sys.pm,v 1.61 2004/02/04 10:11:23 abw Exp $
15 #============================================================================
17 package AppConfig::Sys;
20 use POSIX qw( getpwnam getpwuid );
22 our $VERSION = '1.65';
23 our ($AUTOLOAD, $OS, %CAN, %METHOD);
27 # define the methods that may be available
28 if($^O =~ m/win32/i) {
29 $METHOD{ getpwuid } = sub {
31 ? ( (undef) x 7, getlogin() )
34 $METHOD{ getpwnam } = sub {
35 die("Can't getpwnam on win32");
40 $METHOD{ getpwuid } = sub {
41 getpwuid( defined $_[0] ? shift : $< );
43 $METHOD{ getpwnam } = sub {
44 getpwnam( defined $_[0] ? shift : '' );
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 foreach my $method (keys %METHOD) {
52 eval { &{ $METHOD{ $method } }() };
53 $CAN{ $method } = ! $@;
59 #------------------------------------------------------------------------
62 # Module constructor. An optional operating system string may be passed
63 # to explicitly define the platform type.
65 # Returns a reference to a newly created AppConfig::Sys object.
66 #------------------------------------------------------------------------
78 $self->_configure(@_);
84 #------------------------------------------------------------------------
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 #------------------------------------------------------------------------
98 # splat the leading package name
99 ($method = $AUTOLOAD) =~ s/.*:://;
102 $method eq 'DESTROY' && return;
105 if ($method =~ s/^can_//i && exists $self->{ CAN }->{ $method }) {
106 return $self->{ CAN }->{ $method };
109 elsif (exists $self->{ METHOD }->{ $method }) {
110 if ($self->{ CAN }->{ $method }) {
111 return &{ $self->{ METHOD }->{ $method } }(@_);
118 elsif (exists $self->{ uc $method }) {
119 return $self->{ uc $method };
122 warn("AppConfig::Sys->", $method, "(): no such method or variable\n");
129 #------------------------------------------------------------------------
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 #------------------------------------------------------------------------
141 # operating system may be defined as a parameter or in $OS
142 my $os = shift || $OS;
145 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146 # The following was lifted (and adapated slightly) from Lincoln Stein's
147 # CGI.pm module, version 2.36...
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
155 $os = $Config::Config{'osname'};
158 if ($os =~ /win32/i) {
160 } elsif ($os =~ /vms/i) {
162 } elsif ($os =~ /mac/i) {
164 } elsif ($os =~ /os2/i) {
171 # The path separator is a slash, backslash or semicolon, depending
182 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
186 $self->{ PATHSEP } = $ps;
190 #------------------------------------------------------------------------
193 # Dump internals for debugging.
194 #------------------------------------------------------------------------
199 print "=" x 71, "\n";
200 print "Status of AppConfig::Sys (Version $VERSION) object: $self\n";
201 print " Operating System : ", $self->{ OS }, "\n";
202 print " Path Separator : ", $self->{ PATHSEP }, "\n";
203 print " Available methods :\n";
204 foreach my $can (keys %{ $self->{ CAN } }) {
205 printf "%20s : ", $can;
206 print $self->{ CAN }->{ $can } ? "yes" : "no", "\n";
208 print "=" x 71, "\n";
221 AppConfig::Sys - Perl5 module defining platform-specific information and methods for other AppConfig::* modules.
226 my $sys = AppConfig::Sys->new();
228 @fields = $sys->getpwuid($userid);
229 @fields = $sys->getpwnam($username);
233 AppConfig::Sys is a Perl5 module provides platform-specific information and
234 operations as required by other AppConfig::* modules.
236 AppConfig::Sys is distributed as part of the AppConfig bundle.
240 =head2 USING THE AppConfig::Sys MODULE
242 To import and use the AppConfig::Sys module the following line should
243 appear in your Perl script:
247 AppConfig::Sys is implemented using object-oriented methods. A new
248 AppConfig::Sys object is created and initialised using the
249 AppConfig::Sys->new() method. This returns a reference to a new
250 AppConfig::Sys object.
252 my $sys = AppConfig::Sys->new();
254 This will attempt to detect your operating system and create a reference to
255 a new AppConfig::Sys object that is applicable to your platform. You may
256 explicitly specify an operating system name to override this automatic
259 $unix_sys = AppConfig::Sys->new("Unix");
261 Alternatively, the package variable $AppConfig::Sys::OS can be set to an
262 operating system name. The valid operating system names are: Win32, VMS,
263 Mac, OS2 and Unix. They are not case-specific.
265 =head2 AppConfig::Sys METHODS
267 AppConfig::Sys defines the following methods:
273 Calls the system function getpwnam() if available and returns the result.
274 Returns undef if not available. The can_getpwnam() method can be called to
275 determine if this function is available.
279 Calls the system function getpwuid() if available and returns the result.
280 Returns undef if not available. The can_getpwuid() method can be called to
281 determine if this function is available.
289 Andy Wardley, E<lt>abw@wardley.orgE<gt>
293 Copyright (C) 1997-2007 Andy Wardley. All Rights Reserved.
295 Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
297 This module is free software; you can redistribute it and/or modify it under
298 the term of the Perl Artistic License.
302 AppConfig, AppConfig::File