in reply to returning filehandles

My comments will be in two parts, each regarding a different possible suggested solution.
IO::File, For that Matter, Modules in General
  1. How do I know if this module is installed?
  2. If it isn't installed and I get it from CPAN, how do I install it?
  3. The IO::File node shows how to open, close, and read from a IO::File object, but not how to write to one opened for writing. Where can I get more complete information?
Typeglobs
The filehandle and passing stuff nodes seems to imply that once such a typeglob is declared in a subroutine it must be passed in to every function that wishes to use it. So I have to do it like this?
my *bighandle = openafile(); useafile(*bighandle); sub openafile { local *handle; open handle, "file" or die; return *handle; } sub useafile { local *handle = shift; # do stuff with *handle }
This is a little inconvenient if I want to use this handle everywhere. Which leads me to a scoping question. Since I declared my *bighandle; at the global level, will *bighandle be visible in useafile even if I don't pass it in? How do you force a variable to be global?

Thanks again

Some people drink from the fountain of knowledge, others just gargle.

Replies are listed 'Best First'.
Re: Re: returning filehandles
by chipmunk (Parson) on Jan 31, 2001 at 02:06 UTC
    1. How to tell if a module is installed: perldoc Module or perl -MModule -ce 1
    2. How to install a module from CPAN: use the CPAN module (perl -MCPAN -e shell) or follow the instructions that come with the downloaded module.
    3. How to write to a filehandle opened for writing: the IO::File node here on PerlMonks appears to be corrupted. Use perldoc to read the documentation on your local system; it does include examples of writing to an IO::File object.

    Regarding your code question... Globs cannot be declared with my. However, if you change the first line to: *bighandle = openafile(); then everything in the same package can access bighandle directly. (And code in other packages can access it as, for example, main::bighandle.)

Re: Re: returning filehandles
by arturo (Vicar) on Jan 31, 2001 at 02:05 UTC

    Re: the first set of questions:

    1a. perl -M[MODULE NAME] -e '1' will die with an error message.
    1b. IIRC, IO::File and members of its clan are standard with recent versions of Perl (v. >= 5.00503?)

    2. You can open a file for writing by passing "> [filename]" to the constructor. You then pass the object thus created around.

    3. The fine manual is for reading ... but the overall best way is to configure the CPAN module and it will do most things for you automagically, i.e. once it's configured, a simple perl -MCPAN -e 'install [MODULE NAME]' will suffice for most modules.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor