http://qs1969.pair.com?node_id=1163382


in reply to Perl - Remove duplicate based on substring and check on delimiters

Hi bopibopi,

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 :-)