Perhaps this is more a point of "style", but to me, your sub replace should either open the file and loop through the contents, or simply replace the text. Openening the file outside the sub, then letting the sub loop through it just feels odd to me. Here's how i might do it:
use strict; open (INFILE, $ARGV[0]) or die "Unable to open file '$ARGV[0]' - $!"; while(<INFILE>) { print replace($_); } close (INFILE); sub replace { my $line = shift; chomp ($line); $line =~ s/data|=|detector//g; return $line; }
Tested on this file:
xyzzy data abc detector = data hooplah = enddata
From the command line (note: i opted to use > to print to an OUTFILE rather than do it in the script):
C:\>perl a.pl test.txt > out.txt
Results:
xyzzy abc hooplah end
HTH

Update: Fixed some minor things with the code. Also, as you can see, my output is on one line due to the fact that we're chomping each line. You may wish to leave that out if you want to preserve the lines.

--
Rock is dead. Long live paper and scissors!

In reply to Re: sub-routines by LTjake
in thread sub-routines by Anonymous Monk

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.