Parmenides has asked for the wisdom of the Perl Monks concerning the following question:
I am working on a perl script as part of a book that I am reading through for fun. The first part of the script (which is all that I have completed so far) asks the user to name a file. It then coppies that file into a different file. When it is finished, it will search the file for a particular string and then replace that string with a new string.
Anyways, when I run the program, before anything displays, I get a message in my terminal saying:
"Name "main::READFILE" used only once: possible typo at ./findandreplace.plx line 17"
Then the program creates the write file as exptected, but at the end of the program the file is blank. Also, my terminal gives me this message just before closing the program:
"/home/rootless/Documents/test2.txt
readline() on unopened filehandle at ./findandreplace.plx line 19."
I am definitely opening a valid file with text. It is a plain text doccument that I created using cat > test.txt. Here is my code:
#!/usr/bin/perl -w use strict; #This program asks for an input file, output file, a string, and a rep +lacement string. It then searches the input file for the string and +substitutes it with a phrase of the user's choice. #Get fielnaem and strings my ($infile, $outfile, $string, $rstring) ; print "What file should I parse?\n" ; chomp($infile = <STDIN>) ; print "What expression should I look for?\n" ; chomp($string = <STDIN>) ; print "What should I replace that string with?\n" ; chomp($rstring = <STDIN>) ; print "What new file should I create with your replacement string?\n" +; chomp($outfile = <STDIN>) ; #Open the file, perform the substitutions, and create the new user nam +ed file. open (READFILE, "$infile") || die "Can not open your file for parsing: + $!" ; open (WRITEFILE, ">$outfile") || die "Can not open new replacement fil +e: $!" ; while (<$infile>) { print WRITEFILE "$_" ; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Newby question
by Athanasius (Archbishop) on Aug 05, 2012 at 07:30 UTC | |
Re: Newby question
by CountZero (Bishop) on Aug 05, 2012 at 06:59 UTC | |
by Parmenides (Novice) on Aug 05, 2012 at 08:09 UTC | |
by Old_Gray_Bear (Bishop) on Aug 05, 2012 at 10:44 UTC | |
by Parmenides (Novice) on Aug 08, 2012 at 04:57 UTC | |
Re: Newby question
by aitap (Curate) on Aug 05, 2012 at 10:04 UTC | |
Re: Newby question
by davido (Cardinal) on Aug 05, 2012 at 07:01 UTC | |
Re: Newby question
by Anonymous Monk on Aug 05, 2012 at 06:36 UTC | |
Re: Newby question
by Marshall (Canon) on Aug 05, 2012 at 16:24 UTC | |
by Parmenides (Novice) on Aug 08, 2012 at 04:56 UTC | |
by Marshall (Canon) on Aug 10, 2012 at 10:30 UTC |
Back to
Seekers of Perl Wisdom