blib/lib/Win32/Env/Path.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 183 CC 16 SCAR 22.1
CC
16
Cov
0%
new
30.0
clean
20.0
remove
12.0
CRAP 9.1
1 package Win32::Env::Path;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Win32::Env::Path - Manipulate environment PATH strings
8  
9 =head1 SYNOPSIS
10  
11 use Win32::Env::Path;
12
13 my $path = Win32::Env::Path->new(
14 name => 'PATH',
15 );
16
17 $path->add('C:\\strawberry');
18 $path->remove('C:\\strawberry');
19  
20 =head1 DESCRIPTION
21  
22 B<Win32::Env::Path> is a simple module for inspecting and
23 manipulating environment path lists on Win32, with a particular
24 focus on the B<PATH>, B<LIB> and B<INCLUDE> environment variables.
25  
26 It was designed to allow for intelligent path behaviours during
27 the installation and removal of software applications, and was
28 originally written for use in the the Strawberry Perl installer
29 and other L<Perl::Dist>-derived Perl distribution installers.
30  
31 =head1 METHODS
32  
33 For the moment, the specifics of this class are remaining undocumentated.
34  
35 Please read the code for more information, API is subject to change.
36  
37 =cut
38  
39 use 5.008;
40 use strict;
41 use warnings;
42 use Carp 'croak';
43 use Win32::TieRegistry ( FixSzNulls => 1 );
44 use Params::Util '_STRING';
45  
46 use vars qw{$VERSION};
47 BEGIN {
48 $VERSION = '0.03';
49 }
50  
51 my $USER_ENV = 'HKEY_CURRENT_USER\\Environment';
52 my $SYSTEM_ENV = 'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment';
53  
54  
55  
56  
57  
58 #####################################################################
59 # Constructor and Accessors
60  
61 sub new {
62 my $class = shift;
63  
64 # Create the empty object
65 my $self = bless { @_ }, $class;
66  
67 # Check params and provide defaults
68 $self->{name} ||= 'PATH';
69 $self->{autosave} = defined $self->{autosave} ? !! $self->{autosave} : 1;
70 $self->{autoset} = defined $self->{autoset} ? !! $self->{autoset} : 0;
71 $self->{user} = !! $self->{user};
72 $self->{env} = $self->user ? $Registry->{$USER_ENV} : $Registry->{$SYSTEM_ENV};
73 ($self->{value},$self->{type}) = $self->env->GetValue($self->name);
74 $self->{array} = undef;
75 $self->{array} = [ split /;/, $self->value ] if defined($self->value);
76 $self->{changed} = 0;
77  
78 return $self;
79 }
80  
81 sub name {
82 $_[0]->{name};
83 }
84  
85 sub autosave {
86 $_[0]->{autosave};
87 }
88  
89 sub user {
90 $_[0]->{user};
91 }
92  
93 sub env {
94 $_[0]->{env};
95 }
96  
97 sub value {
98 $_[0]->{value};
99 }
100  
101 sub type {
102 $_[0]->{type};
103 }
104  
105 sub array {
106 $_[0]->{array};
107 }
108  
109 sub changed {
110 $_[0]->{changed};
111 }
112  
113 sub elements {
114 my $self = shift;
115 my $array = $self->array;
116 return defined $array ? 0 : scalar(@$array);
117 }
118  
119  
120  
121  
122  
123 #####################################################################
124 # Main Interface Methods
125  
126 sub add {
127 my $self = shift;
128 my $path = shift;
129 unless ( defined _STRING($path) ) {
130 croak("Did not provide a path to ->add");
131 }
132 die "CODE INCOMPLETE";
133 }
134  
135 sub push {
136 my $self = shift;
137 my $path = shift;
138 unless ( defined _STRING($path) ) {
139 croak("Did not provide a path to ->push");
140 }
141 die "CODE INCOMPLETE";
142 }
143  
144 sub unshift {
145 my $self = shift;
146 my $path = shift;
147 unless ( defined _STRING($path) ) {
148 croak("Did not provide a path to ->unshift");
149 }
150 die "CODE INCOMPLETE";
151 }
152  
153 sub remove {
154 my $self = shift;
155 my $path = shift;
156 unless ( defined _STRING($path) ) {
157 croak("Did not provide a path to ->remove");
158 }
159  
160 # Shortcut if the list is empty
161 my $before = $self->elements or return 1;
162  
163 # Filter out the path if it is in the array
164 my $array = $self->array;
165 @$array = grep { lc $path ne lc $_ } @$array;
166  
167 # Did we remove anything?
168 if ( $self->elements == $before ) {
169 # No change
170 return 1;
171 }
172  
173 # Synchronise and save if needed
174 $self->sync;
175 }
176  
177 # Removes all paths that do not exist
178 sub clean {
179 my $self = shift;
180  
181 # Look for duplicates and non-existant paths and remove them.
182 my $new = ();
183 my %seen = ();
184 my $array = $self->array;
185 foreach my $path ( 0 .. $#$array ) {
186 my $full = $self->resolve($array->[$path]);
187 next if $seen{$full}++;
188 next unless -d $full;
189 CORE::push @$new, $array->[$path];
190 }
191  
192 # Did we make any changes?
193 return 1 if @$new == @$array;
194  
195 # Store and sync
196 @$array = @$new;
197 $self->sync;
198 }
199  
200 sub sync {
201 my $self = shift;
202  
203 # Flag as dirty
204 $self->{changed} = 1;
205  
206 # Convert the list to the string
207 $self->{value} = join(';', @{$self->array});
208  
209 # Save to the registry if needed
210 $self->save if $self->autosave;
211  
212 return 1;
213 }
214  
215 sub save {
216 my $self = shift;
217  
218 # The string is already set correctly, just write it
219 $self->env->SetValue( $self->name, $self->value, $self->type );
220  
221 # Remove the changed flag
222 $self->{changed} = 0;
223  
224 return 1;
225 }
226  
227  
228  
229  
230  
231 #####################################################################
232 # Synchronisation Methods
233  
234 sub resolve {
235 my $self = shift;
236 my $path = shift;
237 if ( $self->type == Win32::TieRegistry::REG_EXPAND_SZ() ) {
238 $path =~ s/\%(\w+)\%/$ENV{uc("$1")}/g;
239 }
240 return lc $path;
241 }
242  
243 1;
244  
245 =pod
246  
247 =head1 SUPPORT
248  
249 Bugs should be reported via the CPAN bug tracker at
250  
251 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Win32-Env-Path>
252  
253 For other issues, or commercial enhancement or support, contact the author.
254  
255 =head1 AUTHOR
256  
257 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
258  
259 =head1 SEE ALSO
260  
261 L<Win32::Env>, L<Perl::Dist::Inno>
262  
263 =head1 COPYRIGHT
264  
265 Copyright 2007 - 2009 Adam Kennedy.
266  
267 This program is free software; you can redistribute
268 it and/or modify it under the same terms as Perl itself.
269  
270 The full text of the license can be found in the
271 LICENSE file included with this module.
272  
273 =cut