Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

novice needs help with file I/O

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I have the following partially written script:
Code:
#!/usr/bin/perl -w
#
# This script fixes the AWStats config file so the current IP address is
# included in the statistics
#

use strict;

my $awcfgfile = "awstats.conf";
my $awcfgdir = "/etc/awstats";
my $tmpcfgfile = "awstatsnew.conf";

open(FHIN,"< $awcfgdir/$awcfgfile") || die "Unable to open $awcfgfile for input:
 $!\n";
open(FHOUT,"> /tmp/$tmpcfgfile") || die "Unable to open /tmp/$tmpcfgfile for out
put: $!\n";

my $line="";
my $indx=0;
while (! undef ($line=readline(FHIN))) {
        print $line;
        if (($indx=index($line,"HostAliases"))>=0) {
                print "Found it!\n";
        }
        print FHOUT $line || die "Write error: $!";
}
close(FHOUT);
close(FHIN);
I am getting 3 errors when I run this:

Use of uninitialized value in print at /usr/local/bin/fixawconf.pl line 19, <FHIN> line 1.
Use of uninitialized value in index at /usr/local/bin/fixawconf.pl line 20, <FHIN> line 1.
Write error: Bad file descriptor at /usr/local/bin/fixawconf.pl line 23, <FHIN> line 1.

It seems that it doesn't like something about my file descriptor (FHIN) although it is '$line' that is the common element in the lines the error is complaining about. Can someone tell me what I am doing wrong? TIA.
 
if (($indx==index($line,"HostAliases"))>=0) { is one error you're lookig at, a single = is assignment and always returns a true value.

also drop the spaces between filename and director
">/tmp/$tmpcfgfile" instead of "> /tmp/$tmpcfgfile"

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Assignment returns the value of whatever was being assigned, so it's not necessarily always a true value.
 
Thanks for the replies. Removing the white space didn't change anything and indeed the docs say it shouldn't matter. As for the '=' vs. '==', I believe my single '=' is the correct syntax. I want the value returned set to '$indx' and then the value of '$indx' compared with '0'. If the substring is not found, 'index' is supposed to return -1 in which case the expression is false, as intended. Am I misunderstand the perl syntax?
 
I may have figured out what is wrong but I don't know how to fix it. I think 'undef' is not being used as a unary logical operator but rather as a function. My variable is being 'undef'ed. What is the syntax for testing for an 'undef' variable in my 'while' loop? TIA.
 
Use of uninitialized value means that the variable is empty and no information was passed to it.

Bad file descriptor indicates a problem with the path to the file or the file does not exist.

You guess about $line being the problem is correct. If no value is assigned to $line during "readline(FHIN)" then everything else after that point that depends on $line will fail also.
 
Got it! I was right, my 'while' test was not testing for 'undef' it was 'undef'ing the variable. Since 'defined' is NOT a funtion, I changed my test from '! undef' to 'defined' and it works. Thanks anyway.
 
== is for use as numeric comparison
eq is for string operations

= is an assignment, error mine previously, thanks ishnid, NOT the correct syntax

--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top