Some other thoughts on the OPed code.

my ($key, $value) = split /\s/, $line;
next LINE if not $key;

split returns its input string ($line in this case) if it cannot split according to the given pattern, so  $key will almost always have a true value no matter what happens. Better to use
    my ($k, $v) = split /\s/, $line;
    next LINE if not defined $v;
I use defined to avoid false negatives from  '0' and  '' (the empty string), both of which evaluate as boolean false. I also use the variable names  $k $v because I don't like using variaable names that are the same as Perl built-in functions, etc.

$hash{$key} = $value;

The hash  %hash is presumably a package global. Better to use a pre-declared my lexical. (Better in general to avoid globals.)

chomp (%hash);

The chomp is done on all the values of the hash on every iteration through the while-loop. This does no harm, but needlessly burns cycles. chomp can be done just once on the hash after it is populated, i.e., after the while-loop.

Update: This code doesn't seem to really do anything:

@file_array =<*.txt> or die $!; foreach $file (@file_array) { open FH, "$file", or die $!; while (<FH>) { if (/(\S+):(\S+).*\n/) { s/$1/$hash{'$1'}_$2/g; } } close FH; }
To be sure, it opens a bunch of files and reads and potentially alters each line of each file... but then what? The (potentially) altered line is never written out to any file. Is this some sort of tied filehandle (see, e.g., Tie::File)? There's no indication of this if so. Also, I don't understand what you mean by "... I want to change the files in place." As I understand the phrase, this can be done only if the replacement strings are guaranteed to be exactly the same length as the sub-strings that are replaced. Again, no indication this is the case.

Further Update: The process of "editing a file" (that has not been read into a tied array) on a line-by-line basis generally goes something like this (every step is assumed to be checked for success, i.e., it produces no error code):

  1. open file to be edited (input file) in read mode ('<').
  2. Generate temporary name of output file (usually based on input file name) and open for write ('>')
  3. Until all lines are read from input file, read input file line-by-line, alter line as appropriate, write line to output file.
  4. Close input file. Close output file.
  5. Do whatever is necessary to be absolutely sure the newly-written output file is intact.
  6. Change name of input file to some backup name, e.g., 'input.txt.bak'.
  7. If the input file name change produces no errors, rename output file to original name of input file. If the output file rename is successful, the "backup" input file may be deleted; often, this file is kept in existence "just in case."
Even if it looks a lot simpler than this, this is more or less what is happening "behind the scenes." Can you present some code that resembles this process?


Give a man a fish:   <%-(-(-(-<


In reply to Re: In place search and replace with a hash by AnomalousMonk
in thread In place search and replace with a hash by hkates

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.