Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( [id://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":

  • 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.
Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-06-13 13:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.