Hmmm... if this is the entire script, you could simply pass the filename as a parameter and use the -p and -i switches, something like this:
#/usr/bin/perl -pi.bak $count++ if s/^go/\//; END { print STDERR "There were $count lines starting with 'go' in the +file\n"; }
This will automatically overwrite your original file with the new, substituted lines. It will also create a backup file named "<file>.bak" just in case. :-)

If this is not your entire program, then you can just capture its output to a new file, then rename the new file to have the same name as the old one. You could have your program do this itself by printing its output to a new file, then renaming that file. Maybe something like this (this is not the whole program, of course):

open FILE, "< $file" or die "Can't open input file: $!\n"; open OUT, "> '$file.new'" or die "Can't open output file: $!\n"; while (<FILE>) { $count++ if s/^go/\//; print OUT; } close OUT; close FILE; print STDERR "There were $count lines starting with 'go' in the file\n +"; rename $file, "$file.bak"; # make backup copy rename "$file.new", $file; # overwrite original file

-- Mike

--
just,my${.02}


In reply to Re: Re: Re: Matching Question by thelenm
in thread Matching Question by curtisb

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.