That doesn't sound like a text file to me, but like an HTML file. HTML is not a line-oriented format. What if the <body tag has attributes on the next line? What if the line that contains <body has tags immediately following it on the same line, or even the entire HTML file on one line, which is also possible? What I am trying to say is, use a proper parser. Also, why are you not inserting the <link> into <head>? Anyway, here's an example using Mojo::DOM:
use warnings;
use strict;
use Mojo::DOM;
my $html = q{<html><head><link id="1"/></head><body></body></html>};
my $insert = q{<link id="0"/>};
my $dom = Mojo::DOM->new($html);
$dom->find('head')->each(sub {
$_->child_nodes->first->prepend($insert) });
print "$dom\n";
__END__
<html><head><link id="0"><link id="1"></head><body></body></html>
|