while (@array)
This will not do what you expect it to do (specifically, the loop will not terminate.) Please fix your code before posting it.
Based on the rest of your code, I assume that your stanza is contained in a file, and that your data is laid out just the way you specify - tags, etc. each on a line of its own. In that case, the answer is fairly simple:
#!/usr/bin/perl -w
use strict;
open my $config, '+<', 'stanza' or die "stanza: $!\n";
my @all = <$config>;
seek $config, 0, 0;
splice @all, -1, 0, "\tMy new data\n";
print $config @all;
close $config;
This will insert the "\tMy new data\n" string just before the closing stanza.
Update: Re-read the OP's code, added another option for multiple stanzas in a file.
In case you need to select one of multiple stanzas by IP, here's a different version:
#!/usr/bin/perl -w
use strict;
open my $config, '+<', 'stanza' or die "stanza: $!\n";
my $ip = '12.34.56.78';
my $seen;
while (<$config>){
if (m{^<stanza $ip>$}){
$seen = 1;
}
if ($seen && m{</stanza>}){
print "\tMy new data\n";
$seen = 0;
}
print;
}
close $config;
--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.