#!/usr/bin/perl use warnings; use strict; # if the argument to die ends with newline, die # won't print the line number: # open (READ, "test.xml") || die "ERROR: $!\n"; # you probably want to know which file the error # relates to and the mode (read or write): open READ, "< test.xml" or die "ERROR opening test.xml for reading: $!"; # NOTE that we use 'or', a lower-precedence logical # operator so that we can leave off the parentheses # around the arguments to open() my @array = ; # close can fail. if it does, you probably want to know # about it: close READ or die "ERROR closing READ filehandle: $!"; # see above comments # open (WRITE, ">new.xml") || die "ERROR: $!\n"; open WRITE, "> new.xml" or die "ERROR creating new.xml: $!"; # make WRITE the default filehandle select WRITE; foreach (@array) { # '<' and '!' are not meta-characters so there's no # need to escape them: if (//) { # use a here-document (see the perlop man page) print <<_EOF_; TEST Foo 0 _EOF_ } # in /PATTERN/, the '/' character is your delimiter. # if the pattern delimiter occurs in the pattern, # instead of escaping the delimiter, use a different # delimiter with the explicit m operator. here we # use '!' as our delimiter: if (m!!) { print " TEST \n"; } # \n$ : this is redundant. the $ anchor matches an optional newline if (m!$!) { # note the use of '@' instead of '/' as the delimiter: s@$@@; $_ .= " UnKnown \n"; print; # prints $_ by default } else { print; } } close WRITE or die "ERROR closing WRITE filehandle: $!";