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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |