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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't write to file when it's read into script as a variable

Status
Not open for further replies.

J1mbo

Programmer
Dec 12, 2002
93
0
0
US
I have a configuration file which contains many 'name = value' statements. This configuration file is used by several related cgi scripts.

I seem to be having an issue with a few lines in my config file being read into my perl script and then being written to by a system command. The file is appended with a '?' at the end. So if i have these statements:

entrylist = /home/testuser/tempfile = /tmp/entrylist

$tempfile is written to (empty file) as /tmp/entrylist?

Has anyone seen this before? The operating system is redhat linux if that matters.

here is the code that reads in the config file:

Code:
#! /bin/perl -wT

$config_file = "./AUTO.cfg";

print header;

open ( CONFIG_FILE, "< $config_file" ) ;
while (  ) {
  chomp;
  next if ( /^#/ ); 
  if ( /^\s*(\S+)\s*\=\s*(.+)\s*$/ ) {
      eval( "\$$1 \= \"$2\"" );
      $DEBUG && printf( "%-25s = %s\n", $1, $2 );
  }
}
close( CONFIG_FILE );

###$entrylist = "/home/testuser/[URL unfurl="true"]www/cgi-bin/DIR1/Sept2005";[/URL]
###$tempfile = "/tmp/entrylist";


open(LIST, ">>$entrylist") or die "Entry List ($entrylist) is not available at this time";
print LIST "$FirstName,$LastName,$Number,$CarClass$Group,$CarMakeandModel,$SponsorTeam,$Website,$Email\n";
close(LIST);

system " sort -t, -nu -k 3,3 $entrylist > $tempfile ";
system " mv $tempfile $entrylist ";


if i uncomment the $entrylist and $tempfile statements in the above code, everything works fine. I've checked and rechecked permissions too. Any help would be appreciated.

thanks,

jim
 
I can see what your code is doing but I don't understand your explanation of the problem.

One problem with your code is the while loop - it's not reading your input file.
Code:
while()
should be
Code:
while(<CONFIG_FILE>)

Another serious point: the eval and soft reference technique is very dangerous. A typo in the config file could clobber a crucial system variable such as $/, and debugging this sort of problem is a nightmare - especially if it occurs years after you wrote the script. A much safer technique is to use a simple hash to store the config variables. They then have their own, contained, insulated name space where they cannot break anything.

If you do go for the hash (and you really should), the relevant code would be
Code:
my %conf;
while (<CONFIG_FILE>) {
  chomp;
  next if ( /^#/ ); 
  if ( /^\s*(\S+)\s*\=\s*(.+)\s*$/ ) {
      $conf{$1} = $2;
      $DEBUG && printf( "%-25s = %s\n", $1, $2 );
  }
}
close( CONFIG_FILE );

###$entrylist = "/home/testuser/[URL unfurl="true"]www/cgi-bin/DIR1/Sept2005";[/URL]
###$tempfile = "/tmp/entrylist";


open(LIST, ">>$entrylist") or die "Entry List ($entrylist) is not available at this time";
print LIST join( ',', @conf{qw{
      FirstName LastName Number CarClass Group CarMakeandModel SponsorTeam Website Email
   }), "\n";
The hash slice makes this quite pretty.

Some general observations: you should probably generate a tempfile name yourself using $$ to guard against multiple invocation; you'd probably be more efficient reading the entryfile and folding the new line into it within the script rather than using the sort and mv binaries; and you don't need to \-escape the '=' in your regex;

HTH,

fish

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
XML:Simple provides a clean interface for reading and writing simple (XML) configuration files. It reads the whole thing into a hash. You change it, and write it back with XML:Simple.
 
It seems the while statement didn't paste well into my post. I do have it that way though. ??

Thanks for the other suggestions, i'll implement them into the code. My main problem is that my $tempfile variable is
not working when being read in with the while loop code. The system call to sort appends a question mark on the end of the variable. This does not happen when the $tempfile variable is hard coded into the script.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top