[comp.lang.perl] Strange file-opening problem with perl 3.0@44

mfrost@sword.pyramid.com (Mark Frost) (01/29/91)

I had written a script to manage some configuration files. This script opened
and read data from a set of configuration files. It worked fine at patchlevel
41. But then........ came patchlevel 44.

The script seems to work fine now, except that at one particular point in the
file, it fails to open one particular file. The file-exists and is world
readable. If I put perl v41 back into place, the file works fine. I have
single-stepped through and found that it indeed misses the loop. Here's
the hunk o' code.

# Figure out what host this script is being run on
$thishost = &gethostname;

# Set host-specific configfile name
#	Config file name is $thishost + ".config"
$hostconfigfile = "configs/". $thishost . ".config";

	open(HOSTFILE, "$hostconfigfile" || die "Can't open $hostconfigfile\n");
	while (<HOSTFILE>)
		{
		<BUNCH O' STUFF DELETED>
		} # while
	close(HOSTFILE);

It never enters the while loop. I read a bunch of other files in this same
manner, but it's always this one file that fails (and only under v 44).

Perhaps this is also a good place to mention that the "die" command given
above NEVER works. I believe I got this expression from the man page (a
recommended command for opening files). Whether the file is there or not, 
the die will never execute (this is not just under v 44, either). I still
put them in there hoping that one day, just maybe...

Any idears about what might be going on?


          -m-----------  Mark Frost    (mfrost@pyramid.com)
        ---mmm---------  System Administrator - Hardware Engineering
      -----mmmmm-------  Pyramid Technology Corporation
    -------mmmmmmm-----  1295 Charleston Rd, P.O. Box 7295
  ---------mmmmmmmmm---  Mountain View, California 94039-7295 
-----------mmmmmmmmmmm-  (415) 335-8163

tchrist@convex.COM (Tom Christiansen) (01/29/91)

From the keyboard of mfrost@sword.pyramid.com (Mark Frost):
:The script seems to work fine now, except that at one particular point in the
:file, it fails to open one particular file. The file-exists and is world
:readable. If I put perl v41 back into place, the file works fine. I have
:single-stepped through and found that it indeed misses the loop. Here's
:the hunk o' code.
:
:# Figure out what host this script is being run on
:$thishost = &gethostname;
:
:# Set host-specific configfile name
:#	Config file name is $thishost + ".config"
:$hostconfigfile = "configs/". $thishost . ".config";
:
:	open(HOSTFILE, "$hostconfigfile" || die "Can't open $hostconfigfile\n");
:	while (<HOSTFILE>)
:		{
:		<BUNCH O' STUFF DELETED>
:		} # while
:	close(HOSTFILE);

Try this instead:

    open(HOSTFILE, $hostconfigfile) || die "can't open $hostconfigfile: $!";
    while (<HOSTFILE>) {
	# <BUNCH O' STUFF DELETED>
    } 
    close(HOSTFILE):

The 2nd arg to open should be the name of the file.  If you look 
carefully, you have:

    open(X,$x || die);

instead of

    open(X,$x) || die;

which changes things a great deal.

--tom
--
"Hey, did you hear Stallman has replaced /vmunix with /vmunix.el?  Now
 he can finally have the whole O/S built-in to his editor like he
 always wanted!" --me (Tom Christiansen <tchrist@convex.com>)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (01/30/91)

In article <142743@pyramid.pyramid.com> mfrost@sword.pyramid.com (Mark Frost) writes:
: # Set host-specific configfile name
: #	Config file name is $thishost + ".config"
: $hostconfigfile = "configs/". $thishost . ".config";
: 
: 	open(HOSTFILE, "$hostconfigfile" || die "Can't open $hostconfigfile\n");

But $hostconfigfile is always true, so the die will never happen.  You want:

    open(HOSTFILE, "$hostconfigfile") || die "Can't open $hostconfigfile: $!\n";

Put the closing paren in the right place and use $! to tell you why it
thought it couldn't open the file in question (probably because $thishost
got mangled somehow).

Without seeing &gethostname I couldn't tell you why pl44 might mangle
$thishost.  My first guess would be, however, that you said the equivalent of

	$thishost = `hostname`;

and forgot to chop the newline.

Larry