[comp.sources.d] perl filehandle question

bin@rhesus.primate.wisc.edu (Brain in Neutral) (07/06/88)

If I have a perl subroutine such as

sub x
{
local ($fileh);		# file handle

	...
}

how do I reference the file handle?  E.g., should I say

	open ($fileh, "/tmp/junk");
or
	open (fileh, "/tmp/junk");

I presume the latter from the documentation, but it seems inconsistent
to have to declare it with the dollar sign and use it without...

lwall@devvax.JPL.NASA.GOV (Larry Wall) (07/06/88)

In article <336@rhesus.primate.wisc.edu> bin@rhesus.primate.wisc.edu (Brain in Neutral) writes:
: 
: If I have a perl subroutine such as
: 
: sub x
: {
: local ($fileh);		# file handle
: 
: 	...
: }
: 
: how do I reference the file handle?  E.g., should I say
: 
: 	open ($fileh, "/tmp/junk");
: or
: 	open (fileh, "/tmp/junk");
: 
: I presume the latter from the documentation, but it seems inconsistent
: to have to declare it with the dollar sign and use it without...

$fileh can never be a filehandle.  It CAN be a variable pointing to a
filehandle, however.

 	open ($fileh, "/tmp/junk");
and
 	open (fileh, "/tmp/junk");

are equivalent if and only if $fileh eq 'fileh'.  Anywhere you can use
a filehandle (without the $) you can use an indirect filehandle (by
substituting the name of any variable (with a $) that contains the name of
that filehandle).  If you're going to use $fileh to hold your filehandle,
just use $fileh everywhere and make sure $fileh is initialized to the
filehandle you want.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

bin@rhesus.primate.wisc.edu (Brain in Neutral) (07/07/88)

> $fileh can never be a filehandle.  It CAN be a variable pointing to a
> filehandle, however.
> 
>  	open ($fileh, "/tmp/junk");
> and
>  	open (fileh, "/tmp/junk");
> 
> are equivalent if and only if $fileh eq 'fileh'.

So then it is impossible to have a local-only filehandle, since
	local (fileh);
is illegal?

lwall@devvax.JPL.NASA.GOV (Larry Wall) (07/08/88)

In article <337@rhesus.primate.wisc.edu> Brain in Neutral writes about perl:
: So then it is impossible to have a local-only filehandle, since
: 	local (fileh);
: is illegal?

That is correct.  Of course, there are ways of simulating it by having a
local variable point to a generated filehandle.

Larry