It's a few days since I posted and I don't know if you're still looking for support, but I thought I'd follow up and close the loop. I'd like to mention that the example input/output file pair posted here seems to contain much irrelevant data and thus to be overlong. The goal in composing such example files is to include all that is necessary (including, in this case, a bit of general text that is not subject to alteration) and little else. Please see Short, Self-Contained, Correct Example.
In any event, here's a version of the full script previously posted, updated to reflect my current understanding of your requirements based upon this. As before, it is only minimally tested and I have made no attempt to provide a GUI wrapper. And if you're using a version of Perl prior to 5.10, a fix to the regex that uses the \K operator can easily be made.
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 nam
+es
) = @_;
exists $hr_params->{$_} or die "no '$_' parameter" for qw(NAME TAS
+K);
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 fil
+e
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;
}
PerlMonks exists to provide support and assistance to Perl users at all levels of expertise and is not, in general, a free, on-line code writing service. You say that you are a Perl novice, so I have provided a fair amount of code that, in other circumstances, I would have expected you to have contributed to substantially. I have no hesitation about providing help to you, but in future if you have any qustions, please provide the code with which you are working (or at least a reference to it), and please try to provide short, pertinent example files for development and testing.
Give a man a fish: <%-{-{-{-<
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.