Wizard/SaveAble/ShellVars.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 138 CC 10 SCAR 26.4
CC
10
Cov
0%
Store
30.0
new
20.0
_load
6.0
CRAP 14.0
1 # -*- perl -*-
2 #
3 # Wizard - A Perl package for implementing system administration
4 # applications in the style of Windows wizards.
5 #
6 #
7 # This module is
8 #
9 # Copyright (C) 1999 Jochen Wiedmann
10 # Am Eisteich 9
11 # 72555 Metzingen
12 # Germany
13 #
14 # Email: joe@ispsoft.de
15 # Phone: +49 7123 14887
16 #
17 # and Amarendran R. Subramanian
18 # Grundstr. 32
19 # 72810 Gomaringen
20 # Germany
21 #
22 # Email: amar@ispsoft.de
23 # Phone: +49 7072 920696
24 #
25 # All Rights Reserved.
26 #
27 # You may distribute under the terms of either the GNU General Public
28 # License or the Artistic License, as specified in the Perl README file.
29 #
30 # $Id$
31 #
32  
33 use strict;
34  
35 use Symbol ();
36 use Data::Dumper ();
37 use IO::AtomicFile ();
38 use File::Basename ();
39 use File::Path ();
40  
41 package Wizard::SaveAble::ShellVars;
42  
43 @Wizard::SaveAble::ShellVars::ISA = qw(Wizard::SaveAble);
44 $Wizard::SaveAble::VERSION = '0.01';
45  
46  
47 sub _load {
48 my $proto = shift; my $file = shift; my $prefix = shift;
49 my $self = { 'prefix' => $prefix };
50 my $line;
51 my $fh = Symbol::gensym;
52 open($fh, $file) or die "Failed to open file $file: $!";
53 while($line=<$fh>) {
54 chop($line);
55 if($line =~ /^([^\=]+)\=\"?(.*[^\"])\"?/) {
56 $self->{$prefix . $1} = $2 ;
57 }
58 }
59 close(FH);
60 bless($self, (ref($proto) || $proto));
61  
62 $self->Modified(0);
63 $self->File($file);
64 $self;
65 }
66  
67 sub new {
68 my $proto = shift;
69 my $self = { @_ };
70 my $file = delete $self->{'file'} if (exists($self->{'file'}));
71 my $prefix = $self->{'prefix'};
72 if (exists($self->{'load'}) and delete $self->{'load'}) {
73 return $proto->_load($file, $prefix) if $file and -f $file;
74 }
75 bless($self, (ref($proto) || $proto));
76 $self->Modified(1);
77 $self->File($file);
78 $self->CreateMe($file);
79 $self;
80 }
81  
82 sub File {
83 my $self = shift;
84 $self->{'_wizard_saveable_file'} = shift if @_;
85 $self->{'_wizard_saveable_file'};
86 }
87  
88  
89 sub Store {
90 my $self = shift;
91 my $prefix = $self->{'prefix'};
92  
93 return unless $self->{'_wizard_saveable_modified'};
94  
95 my $file = $self->{'_wizard_saveable_file'};
96 my $dir = File::Basename::dirname($file);
97 die "Failed to create directory $dir: $!"
98 unless -d $dir || File::Path::mkpath([$dir], 0, 0644);
99  
100 my $tow = '';
101 foreach my $key (keys %$self) {
102 if($key =~ /^$prefix(.*)$/) {
103 $tow .= "$1=" . '"' . $self->{$key} . '"' . "\n";
104 }
105 }
106  
107 my $fh = IO::AtomicFile->open($file, "w")
108 or die "Failed to create file $file: $!";
109 if (!$fh->print($tow) || !$fh->close()) {
110 my $msg = $!;
111 $fh->delete();
112 die "Failed to write file $file: $msg";
113 }
114 $self->Modified(0);
115 }
116  
117  
118 =pod
119  
120 =head1 AUTHORS AND COPYRIGHT
121  
122 This module is
123  
124 Copyright (C) 1999 Jochen Wiedmann
125 Am Eisteich 9
126 72555 Metzingen
127 Germany
128  
129 Email: joe@ispsoft.de
130 Phone: +49 7123 14887
131  
132 and Amarendran R. Subramanian
133 Grundstr. 32
134 72810 Gomaringen
135 Germany
136  
137 Email: amar@ispsoft.de
138 Phone: +49 7072 920696
139  
140 All Rights Reserved.
141  
142 You may distribute under the terms of either the GNU General Public
143 License or the Artistic License, as specified in the Perl README file.
144  
145  
146 =head1 SEE ALSO
147  
148 L<Wizard(3)>, L<Wizard::State(3)>
149  
150 =cut
151