changing from command line to perl script

I had posted previously about this problem I had.

I have multiple text files with hundreds of lines of the following type:

2000001 34 54 234 2000001
32 545 2000001 -2000001 77 2000001 44 2000001 998 2000001
77 32 2000001 45 23 111 89
98 75 23 34 999
.
.
.
etc...

What I wanted was for each line, if 2000001 appears 3 or less times (<= 3) then I wanted to replace all instances of this value with a 0 at the beginning of the line. If there were more than 3 of these values, then I would not do anything to that line (keep all instances as they were).

Previously, someone gave me this perl command to be used on the command line:

Code:
---------
$
$ cat f1
Line1) 2000001 12 34 42.5 122 204 2000001 -2000001 15
Line2) 2000001 14 2000001 38.3 2000001 88 2000001
Line3) 45 2000001 446 2000001 88 2000001
Line4) 2000001 2000001 65 883 2000001 34 2000001 5000 2000001
$
$
$ ##
$ perl -lne 'chomp; while(/ 2000001( |$)/g){$i++};
> if ($i<=3) {s/ 2000001( |$)/$1/g; printf("%d ",0)} print; $i=0' f1
0 Line1) 12 34 42.5 122 204 -2000001 15
Line2) 2000001 14 2000001 38.3 2000001 88 2000001
0 Line3) 45 446 88
Line4) 2000001 2000001 65 883 2000001 34 2000001 5000 2000001
$
$
---------

However, with multiple files and hundreds of lines on each file of this sort, it is quite difficult to continually executing this on the command line. So I would prefer a perl script where I can change a couple variables and be able to run it over and over again. What I want is to basically open one file, read in one line at a time and if there are 3 or less occurrences of 2000001 on that line, then replace ALL of them with a 0 at the beginning. Do this for all the lines in the file and then output the results into a new file (results.txt for example).

I am quite new to Perl and anything I have tried has not succeeded, so I am hoping for some help or at least a point in the right direction. Thanks for all your help in advanced!

one way:
Assuming you want to keep perl just embed the command in a script:

perl_it()
{
    perl -lne 'chomp; while(/ 2000001( |$)/g){$i++};
      > if ($i<=3) {s/ 2000001( |$)/$1/g; printf("%d ",0)} print; $i=0' $1
}

for fname in  /path/to/files/*
do
      perl_it $fname > tmp
      mv tmp $fname
done 

Is it possible to not embed the command in a script?

I want a perl script (using perl notation) that does the exact same thing.

Thanks