sub store_user_prefs { my ( $raw_opts, # Options hash, amalgamanted from system defaults, prevously stored preferences, and the command line. $user_prefs_file, # File to store the option in. $system_defaults, # System defaults. Used to provide an ordered list of options to store with comments to store against them. ) = @_; open OPTS, '>', $user_prefs_file or die "Error saving preferences in $user_prefs_file, $!"; print OPTS "#!/bin/perl\n"; print OPTS "#\n"; print OPTS "# user preferences file. Edit with care.\n"; print OPTS "\n{\n"; foreach my $ent ( @$system_defaults ) { my($key, $value, $comment) = @$ent; print OPTS " # $comment\n"; if( 'ARRAY' eq ref $raw_opts->{$key} ) { my $list = '\''.join('\', \'', @{$raw_opts->{$key}} ).'\''; printf OPTS " '%s' => [%s],\n\n", $key, $list; } elsif( 'HASH' eq ref $raw_opts->{$key} ) { printf OPTS " '%s' => {\n", $key; printf OPTS " '%s' => '%s',\n", $_, $raw_opts->{$key}{$_} foreach keys %{$raw_opts->{$key}}; print OPTS " },\n\n"; } elsif( ref $raw_opts->{$key} ) { # Something else, probably the cookie jar. Use data dumper. my $dumpedInfo = Dumper( $raw_opts->{$key} ); $dumpedInfo =~ s/\$VAR1 = //; $dumpedInfo =~ s/;$/,/; printf OPTS " '%s' => %s\n", $key, $dumpedInfo, } else { # plain string, number etc. if( defined $raw_opts->{$key} ) { printf OPTS " '%s' => '%s',\n\n", $key, $raw_opts->{$key}; } else { printf OPTS " '%s' => undef,\n\n", $key; } } } print OPTS "};\n\n"; close OPTS; return; }