PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:
here is the simple find and replace code I wrote to find and replace a line in config files.</P
when I run this code I get the following error message:#!/usr/bin/perl use warnings; use strict; use File::Copy; my($fileName) = $ARGV[0]; print("$fileName \n"); my($find) = "cps***"; my($replace) = "nat***"; if(-e $ARGV[0]) { my($copy) = "$ARGV[0].bak"; copy($ARGV[0], $copy) or die "File cannot be copied. \n"; } else { print "File does not exist. \n"; exit; } open(INPUT,"$copy") or die 'Cannot open file: $!\n'; open(OUTPUT,">$ARGV[0]"); while(<INPUT>){ $_ =~ s/$find/$replace/g; print OUTPUT $_; } close INPUT; close OUTPUT; unlink($copy); print("File $fileName correctly pointed\n"); exit 0;
Variable "$copy" is not imported at /home/test/.put_on_nat.pl line 25. (Did you mean © instead?) Variable "$copy" is not imported at /home/test/.put_on_nat.pl line 35. (Did you mean © instead?) Global symbol "$copy" requires explicit package name at /home/test/.pu +t_on_nat.pl line 25. Global symbol "$copy" requires explicit package name at /home/test/.pu +t_on_nat.pl line 35.
so I tried to put 'my()' on the variable name and recieved this error message:
"my" variable $copy masks earlier declaration in same scope at /home/t +est/.put_on_nat.pl line 35. Can't declare string in "my" at /home/test/.put_on_nat.pl line 25, nea +r ")) "
am I misunderstanding the package name? i thought that declaring it as 'my()' was how you declared a package name, but I am new to perl and still trying to wrap my head around it all. Thanks in advance for your help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Find and Replace
by toolic (Bishop) on Mar 29, 2010 at 19:15 UTC | |
|
Re: Find and Replace
by moritz (Cardinal) on Mar 29, 2010 at 19:16 UTC | |
|
Re: Find and Replace
by kennethk (Abbot) on Mar 29, 2010 at 19:22 UTC | |
|
Re: Find and Replace
by amedico (Sexton) on Mar 30, 2010 at 04:28 UTC | |
by planetscape (Chancellor) on Mar 30, 2010 at 22:05 UTC |