[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] bad file handle



charles wrote
>Era Eriksson wrote:
>>
>> On Tue, 19 Mar 1996 10:41:42 -0800 (PST),
>> Jeffrey Dawkins <batcomp@teleport.com> wrote:
>>  > open($PRICE_FILE, "../sys2.txt");
>>         ^
>> Scratch the dollar sign.
>
>Er, no.
>
>To quote the Camel book on open(): "FILEHANDLE may be a directly
>specified filehandle name, or an expression whose value will be used
>for the filehandle. The latter is called an indirect filehandle.
>Typically it is just a variable containing the name of a filehandle."
>
>So there are contexts in which you might want to use a scalar as
>a filehandle.

That's true, but you can't load the filehandle directly into the scalar
variable.  You have to do something like:
  open(PRICE_FILE, "some.file");
  $cur_handle   = select(PRICE_FILE);
  $price_handle = select($cur_handle);
$price_handle is now the indirect filehandle.

In Perl 5 getting an indirect filehandle is easy with the FileHandle::new
routine in the POSIX.pm module:
  require POSIX;
  $handle = new FileHandle ("some.file","r");
This returns an open filehandle for reading some.file.  Unfortunately, this
routine doesn't return an error if the file can't be opened.

I guess at this point this thread is no longer MacPerl specific.

- Craig