Assuming that your file is always balanced and there are no qouted { or }, it can be made much simpler. (I didnt bother looking into your regex's or the Text::Balanced more than breifly, so bare over with me if this is not what you want)
#!/usr/bin/perl -w use strict; #input print "Name of file to be inputted? :"; $infile=<>; print "What should the output file be named? The file will be automati +cally created. :"; $outfile=<>; chomp $infile; chomp $outfile; ##<<<<<<<<<You forgot this one open INFILE, "<$infile"; #output open FILEOUT, ">$outfile"; my @stack; while(<INFILE>) { my @op = (); my $len = length($_); my $minsta = 0; my $minsto = 0; my $lastpos = 0; while(true) { $minsta = index $_,'{',$minsta; $minsto = index $_,'}',$minsto; last if ($len == $lastpos || ($minsta == -1 && $minsto == -1)); if ($minsta < $minsto) { my $nextsp = index $_,' ',$minsta; $nextsp = length($_) if ($nextsp == -1); my $stack = substr $_,$minsta+1,$nextsp-$minsta; push @op, (substr $_,$lastpos,$minsta-$lastpos), '<',$stack,'>'; push @stack, $stack; $lastpos = $minsta+1; } else { my $stack = pop @stack; die "Not balanced" unless (defined $stack); push @op, (substr $_,$lastpos,$minsto-$lastpos), '</',$stack,'> +'; $lastpos = $minsto+1; } } print OUTFILE join "",@op,(substr $_,$lastpos,$len-$lastpos); } close INFILE; close FILEOUT; die "Not balanced" if (@stack); print "\nYour result is stored in file $outfile\nGoodbye.\n";
I may have some offby 1 errors, solve them if you find it usefull...

This will use almost no memory and prob do the same jobs a LOT faster.

Update:
Well, seems tilly had the same idea as I, and dinner interrupted me so he got it first ;)

T I M T O W T D I

In reply to Re: Code stalls...possible memory leak? by Cine
in thread Code stalls...possible memory leak? by tshabet

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.