I just want to say thank you to everyone for all of the information. I changed the script as Ikegami suggested and not only works good but it made it more clear how Perl loops through the data . It seems to me that the foreach statement only process one line at a time as with each item withen the loop and continues so until all lines in the orignal data file were processed. I was confused because I thought it processed all the lines for each item whiten the loop and only made one pass I hope my though process is clear on this now so I can move on

Hello Everyone , I am new to Perl . I am creating a program and the below code works the way I want it to .
I actually got this code snippet from the internet and use it for my program.
My problem is I do not quite understand how it works? I have done some experimentation on it.
Well the first part is easy . @main is a place holder for the data in date.txt the foreach seems to take the data from @main and put in $line_in
The if statement seems to search and remove any instance of "The marker line" Then proceeds to print "hello" into date.txt. then it loops again ???? and does not
find "The marker line" so it returns an exit code of 0 ???? and moves to the else statement. ???? The else statement then copies the contents of $line_in to date.txt ,
the contents after the if statement removes "The marker line" ????
I am really confused and dont know if I am on the right track can anyone help?

open(FILE,"date.txt") || die $!; @main = <FILE>; close(FILE); open(FILE,">date.txt") || die $!; foreach $line_in (@main) { if ($line_in =~ /The marker line/) { print FILE "The marker line\n"; print FILE "hello\n"; } else { print FILE "$line_in"; }

In reply to regular expression by toniax

Title:
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, details, 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, summary, 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.