title: perl + inserting text
body: <p>the following script text.pl (described below) define to append the $insert text between $first_line and $second_line in the file - myfile.txt</p>
  
  <p>While:</p>
  
  <pre><code>$first_line=A 
  $second_line=B
  $insert = "hello world"
  </code></pre>
  
  <p>for example</p>
  
  <p>before test.pl running</p>
  
  <pre><code>A
  B
  </code></pre>
  
  <p>After I run  test.pl we get:</p>
  
  <pre><code>A
  hello world
  B
  </code></pre>
  
  <p>the problem: but if there line space between A line and B line then it doesn't append the "hello world" as the following , what need to change in the script in order to append the $insert param also if I have in the file space line between A to B ?</p>
  
  <pre><code>A
  
  B
  </code></pre>
  
  <p>test.pl script</p>
  
  <pre><code>use strict; 
  use warnings; 
  
  # Slurp file myfile.txt into a single string 
  open(FILE,"myfile.txt") || die "Can't open file: $!"; 
  undef $/; 
  my $file = &lt;FILE&gt;; 
  
  # Set strings to find and insert 
  my $first_line = "A"; 
  my $second_line = "B"; 
  my $insert = "hello world"; 
  
  # Insert our text 
  $file =~ s/\Q$first_line\E\n\Q$second_line\E/$first_line\n$insert\n$second_line/; 
  
  # Write output to output.txt 
  open(OUTPUT,"&gt;output.txt") || die "Can't open file: $!"; 
  print OUTPUT $file; 
  close(OUTPUT); 
  </code></pre>