in reply to Edit a New file in place after reading it in
First uncomment the use warning and change 'warning' to 'warnings'. Your intent to use strictures (strict and warnings) is good, but ignoring errors is really bad.
Now consider the following self contained example script:
use strict; use warnings; my $template = <<FILE; --------------------------------------- Basic Information: --------------------------------------- Build Report Rev: 02 Builder: Xi Wong Part Name: MG5237ALL5X Customer Name: SI Route: S5H2-12A FILE my $outText; my %subs = ( 'Build Report Rev' => '01', 'Builder' => 'Lucca P.', 'Part Name' => 'MG5415DP', 'Customer Name' => 'SA', 'Route' => 'S5H2-12A' ); # Info to New Buld Report open my $base_fh, '<', \$template; open my $New_fh, '>', \$outText; while (my $line = <$base_fh>) { if ($line =~ /^([^:]+):(\s+)/ && exists $subs{$1}) { $line = "$1:$2$subs{$1}\n"; } print $New_fh $line; } close $base_fh; close $New_fh; print $outText;
Prints:
--------------------------------------- Basic Information: --------------------------------------- Build Report Rev: 01 Builder: Lucca P. Part Name: MG5415DP Customer Name: SA Route: S5H2-12A
which I think does what you want. Notice the hash used to store the substitution values. In a real life example that could be populated from a database or from a file containing the required edits. Although, in real life it feels like this whole exercise should be done using a database. You may be interested in Databases made easy for an introduction to using databases.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Edit a New file in place after reading it in
by perlynewby (Scribe) on Dec 22, 2021 at 15:54 UTC | |
|
Re^2: Edit a New file in place after reading it in
by perlynewby (Scribe) on Dec 29, 2021 at 02:24 UTC | |
|
Re^2: Edit a New file in place after reading it in
by perlynewby (Scribe) on Dec 28, 2021 at 19:50 UTC |