use 5.010; # needs regex \K operator use strict; use warnings; use autodie; use Data::Dump qw(dd); use constant USAGE => <<"EOUSAGE"; usage: perl $0 param_file_name where: param_file_name parameter file name EOUSAGE use constant EXT => '.txt'; my @tags = qw(NAME TASK CAPS PKG_TYPE STACK SHIP); my $rx_value_intro = qr{ \s* [|] \s* VALUE \s* = }xms; my $rx_value = qr{ [[:alnum:]]+ }xms; # define as needed my ($rx_tag) = map qr{ \b (?: $_) \b }xms, join ' | ', map quotemeta, reverse sort @tags ; # print "rx_tag $rx_tag \n"; # for debug die USAGE unless @ARGV == 1; my $param_file_name = shift; die "'$param_file_name' does not exist \n", USAGE unless -e $param_file_name; open my $fh_param, '<', $param_file_name; local $/ = ''; # "paragrep" mode my @non_existent_files; # NEW while (my $record = <$fh_param>) { my $got_params = my %params = $record =~ m{ \G \s* ($rx_tag) $rx_value_intro \s* ($rx_value) \s+ }xmsg; die "bad params record '$record'" unless $got_params; # dd \%params; # for debug # process_params(%params); process_params(\%params, \@non_existent_files); # NEW } close $fh_param; print "file '$_' does not exist" for @non_existent_files; # NEW exit; # subroutines ###################################################### sub process_params { my ($hr_params, # hash ref.: tags => values to edit $ar_non_exist, # NEW array ref.: return non-existent file names ) = @_; exists $hr_params->{$_} or die "no '$_' parameter" for qw(NAME TASK); my $input_file_name = "$hr_params->{'NAME'}_$hr_params->{'TASK'}${ \EXT }"; # die "input file '$input_file_name' does not exist" # unless -e $input_file_name; if (not -e $input_file_name) { # NEW push @$ar_non_exist, $input_file_name; return; } # - is it possible that the NAME or TASK tags may exist in # a file being processed? # - if they exist, would they ever need to be edited? # editing the values of these tags in a file does not seem # to make sense, but i am not familiar with the application. # - remove this step for now. # delete @hr_params{ qw(NAME TASK) }; # ??? may need to be restored ??? my ($rx_search) = map qr{ \b (?: $_) \b }xms, join ' | ', map quotemeta, reverse sort keys %$hr_params ; # print "rx_search $rx_search \n\n"; # for debug open my $fh_in_text, '<', $input_file_name; my $content = do { local $/; <$fh_in_text>; }; # read entire file close $fh_in_text; # $content =~ s{ ^ ($rx_search) $rx_value_intro \K } # { $hr_params->{$1}}xmsg; $content =~ s{ ^ ($rx_search) $rx_value_intro [^\S\n]* \K $rx_value? } {$hr_params->{$1}}xmsg; open my $fh_out_text, '>', $input_file_name; print $fh_out_text $content; close $fh_out_text; }