[comp.lang.perl] Weirdness with <> input operator

marty@wuphys.wustl.edu (Marty Olevitch) (03/01/91)

Well, now that "Programming Perl" is available, I have learned
enough to get quite confused! I've been converting a few
of my little C utilities into perl, and ran into this problem.

Say I have an array of (open) file handles @handle. Then if I try

	for($i = 0; $i <= $#handle; $i++) {
		$line = <$handle[$i]>;
	}

it doesn't put the next line from the file into $line, but
rather the name of the file handle (and seems to miss every other
one). However, the following:

	for($i = 0; $i <= $#handle; $i++) {
		$filehandle = $handle[$i];
		$line = <$filehandle>;
	}

does read the next line into $line. Is this the way it is supposed to
work? This is using perl 3.0 at patchlevel 44.

Also, I found that using the <> operator with an array argument,
for example <@foo> , reads the next item in the foo array. But I didn't
see this documented in the perl book, at least where the main discussion
of the input operator takes place. Going back to the first example,
does perl think that $handle[$i] is supposed to be an array?

Marty Olevitch				Internet: marty@wuphys.wustl.edu
Washington University			UUCP:     uunet!wugate!wuphys!marty
Physics Department, Campus Box 1105	Bitnet:   marty@wuphys.WUSTL
St Louis MO 63130 USA			Tel:      (314) 889-6285

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/02/91)

In article <1991Feb28.171434.6125@wuphys.wustl.edu> marty@wuphys.UUCP (Marty Olevitch) writes:
: Also, I found that using the <> operator with an array argument,
: for example <@foo> , reads the next item in the foo array. But I didn't
: see this documented in the perl book, at least where the main discussion
: of the input operator takes place. Going back to the first example,
: does perl think that $handle[$i] is supposed to be an array?

No, it thinks it's a glob.  The only kind of indirect filehandle allowed
is a bare scalar variable with no intervening whitespace:

	<$indirect_filehandle>

Anything else is a glob (interpreted like a double-quoted string first).

It could be argued that <> is too heavily overloaded, but it's nice to be
able to say:

	while (<$dir/*.bak>) { ...

Larry