Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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'

In reply to Re: How to insert the content of a file into another file before/after a pattern match? by shmem
in thread How to insert the content of a file into another file before/after a pattern match? by sumathigokul

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (8)
    As of 2021-04-14 11:32 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found