In File1 I see a pattern: the component blocks, architecture and signal declarations are separated by an empty line. Thus, the file is organized into paragraphs.
There is a command line switch for reading files in paragraph mode: -00 (see perlrun). The -n and -p switches construct an implicit loop around your code which does the read for you.
So, one way to achieve your goal (remember TIMTOWTDI - There Is More Than One Way To D It) would be something along the following:
#!/usr/bin/perl -n00
use strict;
use warnings;
our $snippet;
BEGIN {
my $snippetfile = shift; # first file on command line
open my $fh, '<', $snippetfile
or die "Can't read '$snippetfile': $!\n";
$snippet = <$fh>; # get entire block into $snippet
close $fh or die "Can't close filehandle of '$snippetfile' properl
+y: $!\n";
}
if (/architecture/s) {
print; # print current block
print $snippet; # print snippet from file 1
} elsif (/signal/s) {
print $snippet; # print snippet first
print; # then the current block
} else {
print;
}
Code is untested. Note that the second file containing the snippet comes first on the command line.
See perlmod for information about the BEGIN block. See perlfunc and perlop for open, close, my, our, shift, print and // (or m//) along with its modifiers, see also perlre.
Please read the docs. Please read the docs!
Doing the same using the implicit print of the
-p switch is left as an exercise to the reader.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
-
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.