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 "$_" ;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.