Java-Build
view release on metacpan or search on metacpan
Build/Tasks.pm view on Meta::CPAN
=head1 read_prop_file
Opens the given file, reads it, and returns a hash of the var=value pairs.
The config file may have blank lines or comments which start with a #
at the left margin. For example:
# This is a config file....
base_dir=/some/path
=cut
sub read_prop_file {
my $file = shift;
my %config;
open CONFIG, "$file" or return;
_my_log("reading prop file: $file", 0);
while (<CONFIG>) {
next if (/^#/);
next if (/^\s*$/);
chomp;
my ($var, $value) = split /\s*=\s*/;
$config{$var} = $value;
}
close CONFIG;
return \%config;
}
=head1 update_prop_file
The method reads a properties file, updates its keys with new supplied
values (adding keys when necessary) and writes the result back to the
disk.
At present, it does not preserve comments or the order of the keys.
There are two parameters, both are required:
=over 4
=item NAME
The name of the properties file, it will be created if it does not exist.
This name should probably be an absolute path, though it could be relative
to the directory from which the script was launched.
=item NEW_PROPS
A hash reference with the keys you want to change or add and their new values.
=back
=cut
sub update_prop_file {
my %args = @_;
my $name = delete $args{NAME}
or _my_croak "You didn't supply a NAME to update_prop_file";
my $new_props = delete $args{NEW_PROPS}
or _my_croak "You didn't supply a NEW_PROPS to update_prop_file";
_my_croak "Bad argument to update_prop_file: ", keys %args if (keys %args);
my $old_props = read_prop_file($name) || {};
my %output_props = ( %$old_props, %$new_props );
open PROP_FILE, ">$name" or _my_croak "Couldn't write to $name $!\n";
_my_log("writing updated prop file: $name", 0);
foreach my $key (keys %output_props) {
print PROP_FILE "$key=$output_props{$key}\n";
}
close PROP_FILE;
}
=head1 filter_file
The method reads a file one line at a time, filtering it according
to your rules before writing it out.
There are three parameters, two are required: INPUT and FILTERS.
OUTPUT is optional, if it is omitted, the result will overwrite the
INPUT.
Note Well: This function is line oriented. It won't work on multi-line
patterns. Suggestions for a more general approach are welcome.
=over 4
=item INPUT
The name of a file to filter.
=item FILTERS
A list of filtering functions. The function receives one line at a time.
It should modify the received line (change $_[0]) if needed. It is fine
to leave the parameter unchanged.
=item OUTPUT
The name of the filtered file. If this is omitted, or is the same
as INPUT, the result will overwrite the original. (A temporarly file
is used so that data is not lost.)
=back
=cut
sub filter_file {
my %args = @_;
my $input = delete $args{INPUT}
or _my_croak "You didn't supply a INPUT to filter_file";
( run in 0.874 second using v1.01-cache-2.11-cpan-bbe5e583499 )