"John W. Krahn" <[EMAIL PROTECTED]> writes:

> [EMAIL PROTECTED] wrote:
>> How can I check inside a script to see if a file has already been
>> opened early and is still open?  Does the perl interpreter keep track
>> of that kind of stuff.
>> 
>> It seems error prone to set a variable to TURE or something on open
>> since you then have to remember to null it out on close.  I'd like it
>> better if there was something I could check that is builtin.
>
> perldoc -f fileno

That looks like the ticket and I've posted a couple snipped for anyone who
searchs on this subject:

       fileno FILEHANDLE
               Returns the file descriptor for a filehandle, or undefined if
               the filehandle is not open.  This is mainly useful for con-
               structing bitmaps for "select" and low-level POSIX tty-handling
               operations.  If FILEHANDLE is an expression, the value is taken
               as an indirect filehandle, generally its name.

               You can use this to find out whether two handles refer to the
               same underlying descriptor:

                   if (fileno(THIS) == fileno(THAT)) {
                       print "THIS and THAT are dups\n";
                   }

               (Filehandles connected to memory objects via new features of
               "open" may return undefined even though they are open.)

Adriano Ferreira <[EMAIL PROTECTED]> writes:

> On 12/22/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>> How can I check inside a script to see if a file has already been
>> opened early and is still open?  Does the perl interpreter keep track
>> of that kind of stuff.
>
> Maybe what you want is the function C<openhandle> from C<Scalar::Util>
> module. See
>
>     perldoc Scalar::Util
>
> if you use 5.8 or install the module from CPAN first and then use perldoc.
>

That also looks like a good technique:

       openhandle FH
           Returns FH if FH may be used as a filehandle and is open, or FH is
           a tied handle. Otherwise "undef" is returned.

               $fh = openhandle(*STDIN);           # \*STDIN
               $fh = openhandle(\*STDIN);          # \*STDIN
               $fh = openhandle(*NOTOPEN);         # undef
               $fh = openhandle("scalar");         # undef



Do either of you know if this second technique has the same small
liability mentioned for the first one above:
(From `perldoc -f fileno')

   `(Filehandles connected to memory objects via new features of
     "open" may return undefined even though they are open.)'


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to