OK, I'm trying to modify a text file based on user input (one variable). The user input stuff works (thanks to suggestions here), but the file input/output keeps not working. The file exists and is populated, and the search pattern is in there, but it's still not modyfying my file. I've got STDERR redirected to a file, but it only gives me the following error when I'm trying to "use Strict;": "Can't use string ("c:\blah.txt") as a symbol ref while "strict refs" in use at c:\console.pl line 20, <STDIN> line 1." No other errors. Without "use Strict;" and removing the "my" references, I get no errors, but it still doesn't work.
Question no. 2: after the file read/write works, I need to generalize the search pattern - ie, instead of "serverport 111" I need it to search for "serverport until the newline". I was thinking something like s/serverport*$/serverport $port\n/ should work, but I'm just checking.
Thanks for your help.
Glenn
#!/usr/bin/perl
use Win32::Console;
use strict;
open(STDERR, ">error.log");
my $con = Win32::Console->new();
$con->Mode(ENABLE_WINDOW_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_LINE_
+INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_
+EOL_OUTPUT);
$con->Display;
$con->Write("Please enter the agent ID number: ");
chomp(my $port = <STDIN>);
$con->Write("The port number to use is $port.\n"); #Debug stuff, this
+works
open(CONFIG, "+<", "c:\\blah.txt" or die "Can't open blah.txt: $!");
my @configfile = <CONFIG>;
foreach my $configfile (@configfile) {
$configfile =~ s/serverport 111$/serverport $port\n/;
print CONFIG $configfile;
}
close(CONFIG);
close(STDERR);
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.