Your code has a couple of issues: You don't check your open calls for errors (open(...) or die $!;), you seem to have a typo on the line print fh1 $_; (should be $fh1), and
there's a closing brace missing (a copy/paste mistake I assume) (apparently fixed by ninja edit... It is uncool to update a node in a way that renders replies confusing or meaningless). Also, print $fh2; prints the filehandle to standard output, if you want to print the current line to $fh2 you have to be explicit: print $fh2 $_;
They were just set on read, is there a way to change this from inside the code?
I'd recommend you don't, because write-protection is supposed to be exactly that! Someone someday (including you) might set a file to read-only for a good reason and the script would clobber it anyways. I recommend you output a descriptive error message instead, e.g. die "I can't write to $filename\n" unless -w $filename; (see -X). But if you must ("just enough rope" and all that), there's chmod.
Hope this helps,
-- Hauke D
P.S. Just saw stevieb and linuxer were a little faster than me on several points :-)
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>
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
Want more info? How to link or
or How to display code and escape characters
are good places to start.