[comp.lang.perl] simple script to write a file core dumps, why?

garvey@cmic.UUCP (Joe Garvey) (03/08/90)

I've run into a strange problem writing to files with perl 3.0, pl 12.
It was compiled with the +O1 and not the -O (which is know to cause problems
on HP machines).

It sends information to the screen instead of the file, and then core
dumps (ie stops dead). The requested file is created, but has 0 bytes.

Any ideas? Any ideas on where to look in the perl sources. It passed all its
tests ok. I'm still kinda new to perl, have I done something wrong?

Exhibit A: Offending perl script.

#!/usr/contrib/bin/perl

$header[0] = "sometimes you feel like a nut";
$header[1] = "sometimes you don't";

open(wow, ">zzz");

for($i = 0; $i <= $#header; $i++)
   {
   printf("%s\n", $header[$i]);                         #this goes to the screen
   printf(<wow>, "%s\n", $header[$i]);             #this goes to the screen, too
   }
#end_for

close (<wow>);

exit 0;



Exhibit B: output of perl -v (less copyright, etc)

$Header: perly.c,v 3.0.1.4 90/02/28 18:06:41 lwall Locked $
Patch level: 12

---

Joe Garvey                       UUCP: {apple,backbone}!versatc!mips!cmic!garvey
California Microwave             Internet: garvey%cmic@mips.com
990 Almanor Ave                  HP Desk: garvey (cmic@mips.com) /hp1900/ux
Sunnyvale, Ca, 94086             800-831-3104 (outside CA)
408-720-6439 (let it ring)       800-824-7814 (inside CA)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/08/90)

In article <257@cmic.UUCP> garvey%cmic@mips.com writes:
: Followup-To: garvey%cmic@mips.com

Don't do that.  The inews program has no idea what newsgroup
garvey%cmic@mips.com is, and I don't blame it.  You just wasted about
5 minutes of my time.  Followup-To: is used for redirecting the
discussion to another newsgroup.

:    printf(<wow>, "%s\n", $header[$i]);           #this goes to the screen, too

There are two things wrong with that.  First, <wow> is not a filehandle.
wow itself is the filehandle, and <wow> is the input symbol containing
the filehandle wow.  So the above will input a null string from output
filehandle wow, and then print it as the first argument to STDOUT,
because the second thing you did wrong was to put a comma after the
filehandle, which makes it not a filehandle.  What you wanted to say was

    printf(wow "%s\n", $header[$i]);

or

    printf wow "%s\n", $header[$i];

Better yet, make wow upper case so that if I add a reserved word "wow"
you don't have to rewrite your script.

And you probably don't want to waste a printf on a format as simple as "%s\n".
I'd just say

    open(WOW, ">wow") || die "Couldn't create wow: $!";
    ...
    print WOW $header[$i], "\n";

This is much more efficient and I think clearer.  Admittedly a little less
like C...

Larry

allbery@NCoast.ORG (Brandon S. Allbery) (03/10/90)

As quoted from <257@cmic.UUCP> by garvey@cmic.UUCP (Joe Garvey):
+---------------
| It sends information to the screen instead of the file, and then core
| dumps (ie stops dead). The requested file is created, but has 0 bytes.
| Any ideas? Any ideas on where to look in the perl sources. It passed all its
| tests ok. I'm still kinda new to perl, have I done something wrong?
+---------------

Yup.  You aren't using the filehandle right.

+---------------
|    printf(<wow>, "%s\n", $header[$i]);             #this goes to the screen, too
+---------------

The syntax <filehandle> reads or writes a line from/to the file.  Functions
like print, printf, close, etc. take unadorned filehandles:

	printf(wow, "%s\n", $header[$i]);	# works

+---------------
| close (<wow>);
+---------------

Same bug.  Try "close(wow);".

However, I do admit that a core dump is not the best of diagnostics....

Just another Perl diver,						;-)
++Brandon
-- 
Brandon S. Allbery (human), allbery@NCoast.ORG (Inet), BALLBERY (MCI Mail)
ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), B.ALLBERY (GEnie)
BrandonA (A-Online) ("...and a partridge in a pear tree!" ;-)

allbery@NCoast.ORG (Brandon S. Allbery) (03/10/90)

As quoted from <7335@jpl-devvax.JPL.NASA.GOV> by lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall):
+---------------
| :    printf(<wow>, "%s\n", $header[$i]);           #this goes to the screen, too
| 
| because the second thing you did wrong was to put a comma after the
| filehandle, which makes it not a filehandle.  What you wanted to say was
+---------------

Yuck!!!!!  (Quite aside from your proving my response to his posting wrong. ;-)
Why does the comma matter?

Just another Perl diver,						;-)
++Brandon
-- 
Brandon S. Allbery (human), allbery@NCoast.ORG (Inet), BALLBERY (MCI Mail)
ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), B.ALLBERY (GEnie)
BrandonA (A-Online) ("...and a partridge in a pear tree!" ;-)

composer@bucsf.bu.edu (Jeff Kellem) (03/11/90)

In article <1990Mar10.031524.25799@NCoast.ORG> allbery@NCoast.ORG (Brandon S. Allbery) writes:
   [..referring to print and printf, and the optional FILEHANDLE argument]

 > Why does the comma matter?

If a comma is placed after the FILEHANDLE, then the "intended" FILEHANDLE
becomes the beginning of a LIST to be printed, and, as such, prints the
LIST to stdout (or the currently selected FILEHANDLE).

As in ...
	print STDERR "looks fine";   # this is okay

	print STDERR, "uh-oh";       # should produce a compilation error
				     # because of the comma after STDERR
	
Keep hackin'...

				-jeff

Jeff Kellem
INTERNET: composer@cs.bu.edu  (or composer@bu.edu)
UUCP: ...!harvard!bu-cs!composer