# load_ini # returns undef on error, hashref on success sub load_ini($) { my ($ini) = @_; # first, check for all of those special conditions # that you wanted to avoid, like directories, etc: return undef if ( -d $ini or ! -r $ini or ! -f $ini ); my $fh = new FileHandle($ini); return undef unless defined $fh; # read INI here, represent it in a data struct my $ini_data = parse_ini($fh); # return handy structure (could be an object) # that keeps the INI filename in it for later use return { ini => $file, data => $ini_data }; } # load_ini_safe # same as load_ini, but never "fails" sub load_ini_safe($) { my ($ini) = @_; # provide a default if load_ini() fails # This would prevent you from saving to the INI file # if we load defaults. But see below... # return load_ini($ini) # or { ini => undef, ini_data => default_ini() }; # This will allow you to SAVE the ini settings if you # ONLY have write permissions... (yeah, odd case, # that). return load_ini($ini) or { ini => $ini, ini_data => default_ini() }; } # save_ini # returns boolean 1 on success, 0 on failure sub save_ini($) { my ( $ini_struct ) = @_; # this is a hash slice, btw... my ( $ini, $ini_data ) = %{ $ini_struct }{ qw/ini ini_data/ }; # check for a valid filename # (could be undef, see load_ini_safe for details) return 0 unless $ini; # If we keep the filename around from load_ini_safe, # then we have to perform the same checks as in # load_ini(). # If we only keep the filename if we could open it, # then we don't need to perform these tests return 0 if ( -d $ini or ! -r $ini or ! -f $ini ); # Now, try to open the file for writing. my $fh = new FileHandle(">$ini"); return 0 unless defined $fh; # now write the contents out to the filehandle # presumably, this returns 1 on success, 0 on failure return write_ini( $fh, $ini_data ); } # get_default_ini_data # provide default INI hashref sub default_ini() {} # parse_ini # converts text from filehandle into INI hashref sub parse_ini($) {} # write_ini # writes in-memory INI data structure to file as text sub write_ini($\%) {}